1

Hi I am trying to set this RichTextBox variable name from TextBox to string name is this possible?

public void AddTab(string name)
    {
        TabPage NewPage = new TabPage(name);
        RichTextBox TextBox = new RichTextBox();
        TextBox.GetType().GetProperty("Name").SetValue(TextBox, name,null);
        this.tabControl1.TabPages.Add(NewPage);
        NewPage.Controls.Add(TextBox);
        TextBox.Location = new System.Drawing.Point(0, 0);
        TextBox.Name = name;
        TextBox.Size = new System.Drawing.Size(790, 460);
        TextBox.TabIndex = 0;
        TextBox.Text = "";

    }
ProofTheEngi
  • 87
  • 1
  • 7

3 Answers3

0

I don't think this is possible with local variables, but it's possible in member variables of an object using reflection. If you go that route, there are lots of articles in the MSDN knowledge base about setting properties with reflection.

However, reflection is expensive to do, so there might be better ways to implement your design. One possibility that springs to mind is to use a Dictionary with String keys.

Jason Baker
  • 2,471
  • 3
  • 23
  • 28
0

read this

you can if you do like this using Dictionary :

Use aDictionary<string, Variable>.

var vartable = new Dictionary<string, Variable>();
vartable[strLine] = new Variable(input);
Community
  • 1
  • 1
bumbumpaw
  • 2,522
  • 1
  • 24
  • 54
0

I believe RichTextBox has a Tag property, you could set that, and check it later. Again it really depends what you want to do with that name. The other issue is how would ever use that variable in a statically compiled language? It maybe be possible to use it through reflection but again I ask, why?

nagates
  • 620
  • 13
  • 40