9

I have written a WinForms program in C#.Net to click a button programmatically within a password form.

Form1 loads and shows Form2 as a dialogue box.

The application will close if DialogResult is anything other that DialogResult.OK.

So far I have a button click event, which is coded as follows:

 if (txtpass.Text == "")
            {
                MessageBox.Show("You need to enter a password", "Password", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                txtpass.Focus();
            }
            else
            {
                if (txtpass.Text == "1234")
                {
                    radButton1.DialogResult = DialogResult.OK;
                    radButton1.PerformClick();
                }
                else
                {
                    MessageBox.Show("Password Incorrect", "Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtpass.Text = "";
                    txtpass.Focus();
                }
            }

I use radButton1.PerformClick();, but running the program gives me the following message:

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

I'm unsure what is causing this exception to throw.

WonderWorker
  • 8,539
  • 4
  • 63
  • 74
Crazyd22
  • 783
  • 5
  • 10
  • 24
  • Forgot to add, this code is inside the button that it is trying to simulate – Crazyd22 Feb 16 '10 at 09:51
  • Can you post some more about what you are trying to do? Essentially it looks like you're tacking the problem in the wrong way, hence your problem. Might be able to suggest a better way if we know what you are trying to do. – Ian Feb 16 '10 at 09:59

5 Answers5

7

Edit Not a guess. Telling the button to click itself from within itself is most definitely causing an infinite loop. This causes the method to get called over and over, filling up the stack and causing it to overflow.

My guess is that calling PerformClick() is causing the current method you posted to get called again, thus causing an infinite call loop and resulting in a StackOverflowException.

To prevent this, you need to fix the logic somewhere in your code so that:

if (txtpass.Text == "1234")

evaluates to false and the click method doesn't get called over and over. You can probably achieve this by setting txtpass.Text = "" right before you cause it to click itself again.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • Ah I see, any ideas on how to get around this? – Crazyd22 Feb 16 '10 at 09:53
  • "You can probably achieve this by setting txtpass.Text = "" right before you cause it to click itself again." But then that will throw the "invalid password" message box - so what's the point? – Andy Shellam Feb 16 '10 at 09:59
  • @Andy I see that, the logic in the button needs to be reworked a lot it seems. – jjnguy Feb 16 '10 at 10:01
3

Normally you would manually call the event that you are trying to run.

E.g. if you have a method

button1_Click(object sender, ButtonEventArgs e)
{
}

Then you would call the following in your code:

button1_Click(this, new ButtonEventArgs());

I think maybe you need to explain some logic in your code though, as it's not clear what you're trying to do. The StackOverflow probably because you're doing

PerformClick() -> PerformClick() -> PerformClick() because your "1234" text never changes between calls.

Ian
  • 33,605
  • 26
  • 118
  • 198
1

To call the event handler again from inside you could use the following code:

if (txtpass.Text)
{
    case "1234":
        radButton1.DialogResult = DialogResult.OK;

        txtpass.Text = "12345";

        radButton1.PerformClick();

        break;

    default:
        case "12345":
        break;

}
WonderWorker
  • 8,539
  • 4
  • 63
  • 74
herzmeister
  • 11,101
  • 2
  • 41
  • 51
  • This is because I need to set the dialogue button up, but I dont want to do this without them entering the password, but this means that they have to click it twice – Crazyd22 Feb 16 '10 at 09:55
1

Is the PerformClick() inside the button's click event? If so, that's where you're going wrong because you're throwing your application into an infinite loop.

User clicks button,
.NET runs Click() handler,
Button clicks PerformClick(),
.NET runs Click() handler,
Button clicks PerformClick(),
.NET runs Click() handler,
Button clicks PerformClick(),

etc.

Is form1 definitely calling ShowDialog() on form2, and not just Show()?

Instead of radButton1.DialogResult, try setting this.DialogResult == DialogResult.OK.

The DialogResult property on a button tells .NET which DialogResult to assign to the Form when the Button is clicked.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Andy Shellam
  • 15,403
  • 1
  • 27
  • 41
0

A stack overflow happens usually because method is indefinitely calling itself, because each time a method is called, an entry is added to the stack to the point where there is no more stack left.

To stop the recursion, remove the line radButton1.PerformClick();

WonderWorker
  • 8,539
  • 4
  • 63
  • 74