i have a window form that has usename and a password but i cant get my 2 buttons submit and cancel to work dose anyone know where i could look to learn more on how to make my buttons work ty all

Dani AI

Generated

A concise troubleshooting and how-to summary for ’s login form. Common causes for “buttons not working” are: no Click event handler attached, testing a different form instance than the one shown, or misunderstandings about modal vs modeless forms. ’s tip is correct that double‑clicking a button in the designer will generate a Click handler in the form that contains the control; ’s DialogResult approach is appropriate when the form is shown modally.

A reliable pattern for a modal login dialog is to validate inside the Submit button’s Click handler and only set the form’s DialogResult after successful validation. Example Click handler (illustrative—replace Authenticate with the actual check):

private void submitButton_Click(object sender, EventArgs e)
{
    if (string.IsNullOrWhiteSpace(txtUsername.Text) || string.IsNullOrWhiteSpace(txtPassword.Text))
    {
        MessageBox.Show("Please enter username and password.");
        return;
    }

    if (Authenticate(txtUsername.Text, txtPassword.Text))
    {
        this.DialogResult = DialogResult.OK;
        this.Close();
    }
    else
    {
        MessageBox.Show("Invalid credentials.");
    }
}

For convenient keyboard behavior, set the form’s AcceptButton and CancelButton (either in the designer or in code):

this.AcceptButton = submitButton;
this.CancelButton = cancelButton;

Key cautions and quick checks: avoid assigning a DialogResult directly on the Submit button in the designer if validation must run first—doing so closes the dialog immediately. Confirm the handler is actually wired (check InitializeComponent or the control’s Events pane), place a breakpoint or temporary MessageBox inside the handler to verify it fires, ensure the shown form instance is the same one being edited, and remember that DialogResult only applies to ShowDialog(); use events/callbacks for modeless Show().

Recommended Answers

All 5 Replies

You need to have a DialogResult item such as

DialogResult dr = newForm.ShowDialog();
            if (dr == DialogResult.OK)
            {//do something}

Then in your prompt you need to set the buttons to have the correct dialogue result values. You can either set these by code or by the property panel.

Hope that helps.

usually double clicking the buttons in the ide form design mode will automatically generate code for button events :)

usually double clicking the buttons in the ide form design mode will automatically generate code for button events :)

But that will be in the form CONTAINING the button rather than the form that called the dialogue box.

sorry all he asked was how to make a button to work and the window part was really irrelavant, so how can you say my answer is wrong :). and you forgot to mention you actually have to set how the buttons will return by changing the dialog results property of the button. '

Oops my bad. Answering what I thought was there not what is actually there.

Ignore my reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.