-4

Im trying this code to affect all the textbox's in another form

//From form2
Principal FrmPrincipal = new Principal();
private void button1_Click(object sender, EventArgs e)
{
    foreach (var control in FrmPrincipal.Controls)
    {
        if (control is TextBox)
        {
            ((TextBox)control).Enabled = true;
            ((TextBox)control).Text = String.Empty;
        }
    }
}

But it isnt working, what else do I have to do. Thanks.

@DJ KRAZE Before oppening form2, this code disables all the textboxs of my Principal form and it does work

    foreach(var control in this.Controls)
    {
        if (control is TextBox)
        {
            ((TextBox)control).Enabled = false;
        }
    }
  • What is `Principal`? – Yair Nevet Oct 15 '14 at 16:49
  • by default aren't the controls set as private..? change the controls manually to public and then try it.. – MethodMan Oct 15 '14 at 16:51
  • Principal is the name of the first form where the textbox's I want to change are, this piece of code is in form2 – Isaac Tuncar Cedron Oct 15 '14 at 16:51
  • Can you define "not working"? I'm guessing it _is_ setting the properties but on a form you've just created and aren't doing anything with. – petelids Oct 15 '14 at 16:51
  • Look at this solution: http://stackoverflow.com/a/10704066/952310 – Yair Nevet Oct 15 '14 at 16:52
  • @DJKRAZE Yes, the controls I want to change its properties are public (Modifiers --> Public) – Isaac Tuncar Cedron Oct 15 '14 at 16:53
  • @YairNevet Im not familiarized with get-set, I would appreciate if you post an answer with that to solve my problem – Isaac Tuncar Cedron Oct 15 '14 at 16:55
  • I would suggest changing the code inside of your if condition to do something like this `if (control is TextBox) { TextBox tb = control as TextBox; tb.Enbled = true; tb.Text = string.Empty; }` I would use some sort of recursion as well to make sure that you are setting all of the Textboxes on that form properly – MethodMan Oct 15 '14 at 16:59
  • @DonBoitnott My Principal form is opened all the time and all the TextBox's must be changed when pressing the button from form2 while it is running – Isaac Tuncar Cedron Oct 15 '14 at 16:59
  • In your click event you are creating a _new instance_ of `Principal` and setting the properties on the controls in _that_ instance. At the end of the method `FrmPrincipal` goes out of scope. Have you loaded a different instance of `Principal` somewhere else in `form2` that you are expecting to be updated? – petelids Oct 15 '14 at 17:00
  • You are only working on a *new* instance of a Principal object, which goes out of scope at the end of the method and is never shown. I suspect you need a reference to the *existing* form that you currently have on the screen. BTW, if any of those TextBoxes are in a parent container, like a Panel, they won't be reached. You would have to recursively check any container's Controls collection for more TextBoxes. – LarsTech Oct 15 '14 at 17:01
  • @DJKRAZE Still not working – Isaac Tuncar Cedron Oct 15 '14 at 17:04
  • show the code of the other form.. you are doing something wrong apparently.. – MethodMan Oct 15 '14 at 17:06
  • @petelids Well in fact Principal FrmPrincipal = new Principal() is outside button1_Click, I made a mistake putting it there, in my code it is at the beggining after public partial class form2 : Form, so i dont think that is the mistake – Isaac Tuncar Cedron Oct 15 '14 at 17:06
  • you are confusing us take a look here ...it's as simple as `1 2 3` http://stackoverflow.com/questions/9787350/change-text-property-of-all-items-in-form – MethodMan Oct 15 '14 at 17:08
  • The process is simple, I fill some textboxes in Principal form then a button opens form2 but disables all the textboxes first. When pressing a button on form2, enables the textboxes in Principal and clears th texts of all those textboxs – Isaac Tuncar Cedron Oct 15 '14 at 17:15

1 Answers1

0

You're calling form2 from Principal and you want to Disable all the TextBoxes of Principal on a button click of form2.

To perform this, you must pass the Principal reference to form2 in the Constructor.

Example (Calling form2 from Principal form):

Form2 form2 = new Form2(this);  //passing "Principal" form reference to "form2"
form2.Show();

Changes you have to do in form2:

Principal FrmPrincipal;             //new object
Form2(Principal principalRef)       //"form2" constructor
{
    FrmPrincipal = principalRef;    //assignment of "Principal" form reference to the new object
}

Now you can simply call FrmPrincipal anywhere in form2 and make whatever changes you want.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66