0

Hi i have code that create new variable for textbox that not exist yet, but can be created on runtime. it work great, see code bellow

public void btnApagar_Click(object sender, EventArgs e)
{
    TextBox txtAcessorio4 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio4", false).FirstOrDefault();

    if (txtAcessorio4 != null && txtAcessorio4.Text == "" && lblAcessorio4.Name == "lblAcessorio4")
    {
        MessageBox.Show("Perfect");
    }
}

problem that i would like to use this created variable on another places in code too, i i tried:

public partial class cad_produto_acessorios_novo : Form
{
    TextBox txtAcessorio4 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio4", false).FirstOrDefault();
}

public void btnApagar_Click(object sender, EventArgs e)
{    
    if (txtAcessorio4 != null && txtAcessorio4.Text == "" && lblAcessorio4.Name == "lblAcessorio4")
    {
         MessageBox.Show("Perfect");
    }
}

but I had bellow error on public partial class(gpbCategoria is my groupbox name):

Error   1   A field initializer cannot reference the non-static field, method, or property 'InfoEarth_Cad_Cliente.cad_produto_acessorios_novo.gpbCategoria

Somebody know how to solve it?

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
felipe Salomao
  • 313
  • 2
  • 8
  • 21
  • what is the `gpbCategoria` object? – Chris May 02 '13 at 22:02
  • @Chris Bain As i said on top of error, gpbCategoria is my groupbox name (all labels and textbox created in runtime are inside this groupboxname. Thanks – felipe Salomao May 02 '13 at 22:06
  • If the control is created at run-time, then how can "txtAcessorio4" possibly find it...since that line declares the variable and attempts to assign the value to it as soon as the form is created. Obviously since you create the control at run-time, it can't find it. Furthermore, the "gpbCategoria" container hasn't been initialized and added to the form yet, which is what the error is really complaining about. To make that code work you would just declare the variable: `TextBox txtAcessorio4 = null;` and then assign it later AFTER the control has actually been created. – Idle_Mind May 02 '13 at 22:09
  • Understood, have another way to create variable in partial class for future textbox that will be created in runtime ? – felipe Salomao May 02 '13 at 22:28
  • Could you solve the problem with my reply? The first part of the question asks how to set a value for a textbox and in the first line you said that the textbox will be created on runtime. Do you need to use controls.find or is it sufficient if you can set the value for a control that is created at runtime. If you need more help or if i understood the question wrong just let me know. – surfmuggle May 07 '13 at 19:39

1 Answers1

0

Many of your recent questions go into the same direction.

If you want to access a control (like a label or a textbox) you have created at runtime you need some kind of container (an array or a list). This way the .net framework checks during runtime if the control is present in the container.

void BtnApagar_ClickClick(object sender, EventArgs e)
{
    // test if textbox 4 exist by counting the number of added textboxes
    if(textBoxList.Count ==4 || textBoxList.Count > 4)
    {
        // List and array are 0 based --> index 3 is the 4th textbox
        MessageBox.Show("Perfect we have " 
                        + " at least 4 boxes and the name is: " 
                        + textBoxList[3].Name);     
    }else {
        MessageBox.Show("Number of textboxes is not enough - add more");
    }
}

Instead of using (TextBox)gpbCategoria.Controls.Find("txtAcessorio4", false).FirstOrDefault(); we can access the list by index to get a certain "future" instance for textbox_4

You may take a look at my example project and play around with it. It can be openend with sharpdevelop 4.3.

surfmuggle
  • 5,527
  • 7
  • 48
  • 77