0

I want to create a form dynamically and than add a new button to form(also dynamically). now I want to write code on button click event , how I can use form properties. (It generates scope issue. ), should I declare my form as public...? [code of program ][1]

    private void button3_Click(object sender, EventArgs e)
    {
         Form myform = new Form();
        myform.Show();
        myform.Text = "New Dynamic Window";
        Button hide = new Button();
        hide.Text = "Hide";
        hide.Show();
        hide.Location = new Point(50, 50);
        hide.Click += hide_Click;
        myform.Controls.Add(hide);
    }

    void hide_Click(object sender, EventArgs e)
    {

                   // myform.Hide();

    }

when I write myform.Hide(); in my click event of button it generates error.

3
  • coul you please post your code ? instead of image. Commented Dec 24, 2013 at 6:38
  • I write code kindly check... Commented Dec 24, 2013 at 6:41
  • YOu have to declare myForm outsite the button3_click event Commented Dec 24, 2013 at 6:45

4 Answers 4

2

In your event handler you have access to the sender parameter. You can use this parameter to access the parent form. Here is the sample code:

  void hide_Click(object sender, EventArgs e)
    {

         ((sender as Button).Parent as Form).Hide();

    }
Sign up to request clarification or add additional context in comments.

Comments

2

You are creating your myForm in your click event. The variable has method level scope. Try declaring it as a class scoped variable and new it up in your click event.

 Form myform; //Notice that I have removed your declaration from your click event
              //this variable now has visibility throughout the entire class.

private void button3_Click(object sender, EventArgs e)
{
    myForm = new Form();  // I have instantiated it here
    myform.Show();
    myform.Text = "New Dynamic Window";
    Button hide = new Button();
    hide.Text = "Hide";
    hide.Show();
    hide.Location = new Point(50, 50);
    hide.Click += hide_Click;
    myform.Controls.Add(hide);
}

void hide_Click(object sender, EventArgs e)
{

               // myform.Hide();

}

Or You can leave it the way you have it and cast the sender object in your hide_Click event to a Button since it is the object the originated the event and its parent is the Form you can then cast it to Form then call its Hide method.

void hide_Click(object sender, EventArgs e)
{
    ((Form)((Button)sender).Parent).Hide();
}

Comments

2

Try this:

void hide_Click(object sender, EventArgs e)
{
     ((Control)sender).FindForm().Hide();
}

Comments

0

Your form is not accessible in Button click . you should declare your form as a class member.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.