0

I'm having a strange problem ... I have two forms (Form1 & Form2). Form1 calls with an old name (string) and the user enters a new name (textbox1) in Form2 which is returned to Form1. Everything works fine if they enter a value or cancel ... however I want to put an error check to insure they enter a value, among other things. The error check works fine, but after the error, when a correct value is entered, form2 closes but nothing happens. I put in some breakpoints and Form1 seems to hold on the using(form2 ...) statement, waiting for Form2 to finish, but after firing the error message, nothing happens. If I remove the ... Form2 F2 = new Form2 ... Form2 just closes and returns to Fomr1. Ideally I'd like to stay on Form2 until a value gets entered or the user cancels. What am I missing?

// Form1 
using(Form2 F5 = new Form2(SelNm))
{
    if(F5.ShowDialog()== DialogResult.OK)
    {
        //Do stuff
    }
}

// Form2
public string newName { get; set; }
public string oldName { get; set; }

public Form2(string oldNm)
{
    InitializeComponent();
    oldName = oldNm;      
}

private void btnOK_Click(object sender, EventArgs e)
{
    if (textbox1.Text.Length > 0)
    {
        newName = textbox1.Text;
        DialogResult = DialogResult.OK;
        Close();
    }
    else
    {
        MessageBox.Show("ERROR: Must enter a new name.");
        DialogResult = DialogResult.Cancel;
        Form2 f2 = new Form2(oldName);
        f2.Show();
        Close();
    }
}
ArtR45
  • 39
  • 5

1 Answers1

2

The reason for this is that you called a new Form2 after the error dialog is shown. This is not the instance of the Form2 which Form1 is waiting for. Instead of calling a new Form2 why not re-use the current Form2?

Instead of this:

    MessageBox.Show("ERROR: Must enter a new name.");
    DialogResult = DialogResult.Cancel;
    Form2 f2 = new Form2(oldName);
    f2.Show();
    Close();

Why not this?

    MessageBox.Show("ERROR: Must enter a new name.");
    // Do not close the form so the user can
    // input again

Update:

As suggested on the comments..

private void textbox1_TextChanged(object sender, RoutedEventArgs e)
{
    btnOK.Enabled = !string.IsNullOrWhiteSpace(textbox1.Text);
}
jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • In addition I like to have a check that disables the OK button if the text is invalid. This check goes into the TextChanged handler for the textbox, and also the Show handler for the form. – smead Apr 15 '16 at 00:23
  • That would be a better approach which is most common in web pages. @ArtR45 take note of this one. – jegtugado Apr 15 '16 at 00:26
  • Thanks ... I typically just have the messagebox ... but when I first set this up I was using the properties 'DialogResult' instead of setting it manually. In any event in now works great. Thanks again. Quick question, I like your 'disable the OK button idea', could you provide a code snippet? I tried making it 'false' when loading the form, then 'true' in the textbox1_TextChanged event ... but nothing happens. – ArtR45 Apr 15 '16 at 15:40
  • In-case you haven't figured it out I updated my answer as reference. – jegtugado Apr 17 '16 at 05:40
  • also when Form2 loads you have to set btnOK.Enabled = false. – jegtugado Apr 17 '16 at 05:42