-4

I am writing a C# Windows Forms program in Visual Studio. I have a button that creates and shows (opens) a new Form (window) called VideoWindow. I can edit the MainWindow in the Design workspace in Visual Studio which allows me to visually edit its contents. However, I can't find a way to do the same thing with the VideoWindow. I have tried right clicking on VideoWindow and clicking View Designer, but it just takes me to the MainWindow designer. How do I open the designer for the second VideoWindow? Is this possible? Below is the code that creates and opens the new form:

    private void ButtonWindow(object sender, EventArgs e)
    {
        Form VideoWindow = new Form();
        VideoWindow.Size = new Size(500, 300);
        VideoWindow.Show();
    }

Edit: I know you can (and usually should) access the Designer when you create a form through the Visual Studio wizard via Project -> Add Form. However my question was for if you manually write a form class like NewForm.cs. In that case there would be no auto-generated NewForm.Designer.cs file.

Samwise Ganges
  • 395
  • 6
  • 11
  • 1
    In the designer, you just add a new form and name it VideoForm and add the controls, etc. In your code, you initialize it `VideoForm vf = new VideoForm();` `vf.Show();` – LarsTech Jun 28 '22 at 22:43
  • @LarsTech so if you don't create the form through the Visual Studio GUI and instead just manually write a class.cs file, is there then no way to invoke the Designer for that new form class? – Samwise Ganges Aug 03 '22 at 18:13
  • I don't know what you mean by `manually write a class.cs file` in reference to creating a new form. – LarsTech Aug 03 '22 at 18:49
  • @LarsTech I mean you can make a new form in Visual Studio by clicking Project -> Add Form which creates the new form.cs file and populates it with boilerplate and presumably connects it to a GUI designer. Instead, one could just create text files called form.cs (and I supposed form.Designer.cs) and manually write the required code to create the class that inherits from Form and add those to the project via Project -> Add existing item. If you do this, you cannot access the designer for that form class – Samwise Ganges Aug 03 '22 at 21:11
  • OK I just tested it out and I've answered my own question. If you manually create something like TestForm.cs and have the class inherit from System.WindowsForms Form, then hit Shift F7, Visual Studio will start a GUI designer for that form even without a TestForm.Designer.cs file. It will then automatically create the InitializeComonent method in TestForm.cs and add the SuspendLayout, window size, name, and ResumeLayout properties in that method. In this arrangement, the designer elements will not be separated from the class definitions in a separate form.Designer.cs file like usual – Samwise Ganges Aug 03 '22 at 21:27
  • I'm not sure I can figure out what you're accomplishing here. The designer code use to be in the same file some 20 years ago. Microsoft separated them out in order to make it easier on the developer. – LarsTech Aug 03 '22 at 21:43
  • I just like to be able to understand exactly what the IDE is actually doing when you use the GUI or wizard, so it helps me if I can go through the process of doing it manually. I learned coding using bare-bones editors like Notepad++ and Sublime. I just like to know what the more 'intelligent' IDEs are doing behind the scenes. – Samwise Ganges Aug 04 '22 at 18:48

2 Answers2

1

You can customize a new form, and then create the corresponding object after modification. Here are the relevant steps:

1.Create a new form videoform enter image description here

2.Relevant code:

.Show(); and .ShowDialog(); Note the difference between the two.

private void button1_Click(object sender, EventArgs e)
{
    VideoWindow videoWindow = new VideoWindow();
    videoWindow.Show();
    //videoWindow.ShowDialog();
}

3.Ouput: enter image description here

Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21
  • Thank you, this worked but I have one more question. Why doesn't the new Videoform class inherit from the Form class the way MainWindow does? – Samwise Ganges Jul 01 '22 at 20:45
0

After some testing I found that it is possible to access the Visual Studio GUI-based Designer for a form class that was created manually (i.e. not created through the usual Project -> Add Form wizard).

If you create a CustomForm.cs file in your project and, importantly, have that class inherit from Form (System.Windows.Forms), then hit Shift F7, Visual Studio will make a GUI Designer for that form.

It will also automatically create the InitializeComonent method in CustomForm.cs and add the SuspendLayout and ResumeLayout calls and Client size, and Name properties in that method.

In this arrangement, the designer elements will not be separated from the class definitions in a separate form.Designer.cs file like usual. Any change through the Designer GUI will directly effect CustomForm.cs. Of course this is less than ideal because there's a higher likelihood of a developer breaking the form since the auto-generated code is mixed in with the manually written code.

Samwise Ganges
  • 395
  • 6
  • 11