Question

I have multiple forms and was wondering if it would be good practice to add panels to a form, copy across some of the other forms into the main form and display. For example, I could have Form1, Form2, Form3 and Form4. I would then remove Form2 and Form3 - beforehand copy elements into 2 new panels in Form1, then simply toggle between the different 'views' using location, visibility and size.

I have created a real-life example below using the same aspects:

private void Form1_Load(object sender, EventArgs e)
{
    this.Width = 385;
    this.Height = 243;
}

private void ButtonSearch_Click(object sender, EventArgs e)
{
    this.Width = 541;
    this.Height = 226;

    panelSearch.Visible = false;
    panelFileInfo.Visible = true;
    panelFileInfo.Location = new System.Drawing.Point(0, 2);
    label4.Text = textBox1.Text;
}

private void labelSearchAgain_Click(object sender, EventArgs e)
{
    this.Width = 385;
    this.Height = 243;

    textBox1.Text = string.Empty;
    checkBox1.Checked = false;
    checkBox2.Checked = false;
    panelSearch.Visible = true;
}
Était-ce utile?

La solution

I tend to componentize creating custom components/usercontrols that aggregate 2 or 3 controls. I then work on these new components to make them highly reusable (at least inside the same project) with design time support and so on...

I suggest you to try to convert your forms in controls, then drag and drop your custom controls in your main form

Autres conseils

Okay, we've got a lot of legacy code at work where someone did just what you're trying to do. They combined a dozen distinct forms into one huge form with panels everywhere that were dynamically shown/hidden, moved around and manipulated.

It has become a maintenance nightmare!

Keep the forms separated according to their functionality. Give them good descriptive names. And if you're just starting out a project, you might consider putting your efforts into learning WPF, as it is the natural successor to WinForms.

This is all based on my opinion of course, but it's also based on 5 years of experience maintaining some spaghetti code, and I'd recommend you try to avoid it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top