0

I have a WinForm listbox selection of a list of forms that the user can go into.

I have a function that should open the new form

private void sendToDB_Button_Click(object sender, EventArgs e)
{
    string selected_item = listBox1.SelectedItem.ToString();
    Form _secondForm = new Form();

    if (selected_item == "389")
        _secondForm = new Forms._389_Form();
    else if ( selected_item == "120" )
        _secondForm = new Forms._120_Form();
    //... Repeat for 30 more forms ...

    this.Hide();
    _secondForm.Show();
}

When running the application and I select "389" the current form closes as it should but nothing is opened in a new form. Curious if having the forms in a folder called Forms is the issue here. The line in question is _secondForm = new Forms._389_Form(); and is this breaking the application?

Travis
  • 657
  • 6
  • 24
  • Do things work if you don't `Hide` the first form first? – AKX Mar 02 '20 at 15:25
  • That said, no, having a namespace called `Forms` should not be the issue at all. – AKX Mar 02 '20 at 15:26
  • Things do work if `this.Hide();` is removed @AKX – Travis Mar 02 '20 at 15:26
  • When Show() is called, what is `_secondForm`? Are you sure its just not a new'ed up `Form`? Maybe your if-then's are not getting hit. – mxmissile Mar 02 '20 at 15:28
  • side note: you should be able to use a little bit of reflection to get rid of your 30+ if thens, another topic though. – mxmissile Mar 02 '20 at 15:31
  • @mxmissile _secondForm is only a mentioned in the function above. The original form is made from a parental form `this.Hide(); DR f1 = new DR(); f1.ShowDialog(); this.Close();` – Travis Mar 02 '20 at 15:31
  • @Travis work w/ switch case C# – ger Mar 02 '20 at 16:36
  • What is the point of the initialization part of this line of code `Form _secondForm = new Form();`? If all you want to do is declare `_secondForm` to be of type `Form`, then just use `Form _secondForm;`. Don't initialize it - creating a System.Windows.Form base class instance is nearly useless. Having your code fail with a null ref exception is better than having behave weirdly by trying to open a base class Form instance (exceptions are *very* noticeable, and they do get fixed). It the compiler complains that _secondForm isn't initialized, initialize it to null. – Flydog57 Mar 02 '20 at 17:11

2 Answers2

0

Instead of _secondForm.Show(); I changed it to _secondForm.ShowDialog(); and I got the expected results.

According to this link: https://stackoverflow.com/a/20859112/8149159 , The author of the answer states that:

The Show function shows the form in a non modal form. This means that you can click on the parent form.

ShowDialog shows the form modally, meaning you cannot go to the parent form

Travis
  • 657
  • 6
  • 24
-2

try

this.Hide();
_secondForm.ShowDialog();
//if you want close precedent form
this.Close();

Hide is for "hide" all action at the user ShowDialog is for open the form and Close is for close the precedent form

Vale
  • 3
  • 5
  • 1
    While this code might answer the question, can you consider adding some explanation for what the problem was you solved, and how you solved it? This will help future readers to understand your answer better and learn from it. – dan1st Mar 03 '20 at 11:25
  • @dan1st now is ok? I'm new, and idk how write completely the answer – Vale Mar 03 '20 at 14:38