-2

Ok so I have two forms Form1 & form2

  • in "form1" I have a "TextBox" and a "Button" that is set to open "form2".
  • in "form2" I have a "Button" which is set "visible" to "false".

then I want in "form1" when the "TextBox" enters the text "Sample" it will make the button in "form2" "visible" set to "true".

Keep in mind that only "form1" is opened!

The thing I have is a login form where I want the user I've added to get a button visible when the "form2" is opened, but if that user isn't logged in it won't be visible - Thanks :3

  • 2
    You can either expose the Button control with a property (or change the accessibility from protected to public) in form2, or create a method to control the Button's visibility. `form2.SetButtonVisibility(true);` or something similar. – itsme86 Feb 27 '18 at 16:19
  • Additionally to @itsme86 - to get the reference of "form2" have a look at the following https://stackoverflow.com/questions/17514251/find-form-instance-from-other-class – Rand Random Feb 27 '18 at 16:23

2 Answers2

0
         Form form = Application.OpenForms[0]; // OR Application.OpenForms["FormName"];

        textBox.TextChanged += (sender, args) =>
        {
            textBox.Text == "Sample" ? form.button.Visible = true : return;
        };
0

In Form1 do the following:

Form2 form2;
public void button1_Click(Object sender, EventArgs a){
    //show form2
    this.form2 = new Form2();
    this.form2.Show();
}

//This event is up to you
this.textBox.KeyUp += (s,a) => { if(this.textBox.Text == "Sample") {
    this.form2.ShowButton();
}};

And in Form2:

public void ShowButton(){
    this.button.Visible = true;
}

This should do.

sk1tt1sh
  • 198
  • 1
  • 2
  • 11