Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this button I want to click after the form is shown because a dialog box pops up when its clicked but the dialog box gets displayed even before the form is shown meaning the button gets clicked before the form is shown to the user.
private void Form1_Shown(Object sender, EventArgs e)
       {
            Button.PerformClick();
       }


What I have tried:

How do i stop this from happening.
Posted
Updated 25-Oct-21 22:55pm

Is there a reason for the perform click? What is the rest of your code above this?
You should just need...

C#
private void Form1_Shown(Object sender, EventArgs e)
       {
            MessageBox.Show("Hello");
       }

Also, Check your event in the designer. Make sure you haven't accidentally assigned the "Shown" event to the "Load" event.
 
Share this answer
 
v2
Is this a standard WinForm app ?

I can't observe the behavior you describe in a WinForms app with a Button and an OpenFileDialog:
namespace testbuttonload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            button1.PerformClick();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();

            Console.WriteLine(result);
        }
    }
}
Something else in your code is causing the Button to be clicked before the Shown event ... check:

1) your Form Load handler is not calling PerformClick

2) Form click handler, or other events, are not calling PerformClick
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900