Can't View Designer When Coding a Form in C#

Can't view designer when coding a form in C#

How does the Windows Forms Designer work?

When you open a Form in windows forms designer, the designer looks into the first class in the file. If the file has a Designer.cs containing the other partial part of the class, also includes it and tries to deserialize those file contents. In the process of deserialization and loading the design time of your form, it creates an instance of the base class of your form and looks in those files for component declarations and InitializeComponents method. If find them creates components and sets properties of them using deserialized codes and add components to the instance of base class which created.

Some useful facts:

  • Codes in constructor of your Form will not execute at design-time, but the constructor of base class of your form will execute in design-time.
  • Codes in InitializeComponent will not execute at design-time, but those codes will be deserialized and will be used to create designer of the form.
  • The designer can not show a form which has an abstract base class. (solution)
  • The designer can not show a form which has generic class. For example it can not show MyForm:SomeForm<SomeClass>, but it can show SomeForm<T>:Form. (solution)
  • If you define a new property for your form, the properties will not show in properties window. The properties window, shows the properties of base class but with values of your form.
  • When a file contains 2 class, if the form was not the first class the designer can not load and you receive a warning that says the form should be first class to show in designer.
  • Above rules will apply also to UserControls.

Example

Take a look at below code, which has some serious problems:

  • The class has different constructor than class name
  • The statement int i="x";
  • There is no semicolons while this is a C# class
  • The InitializeComponent method didn't call in constructor

But the interesting news is you can see the form in designer, even with those errors!

Just create a file in your project and put below codes in the file and save the file and close it. Then without trying to build the solution, open the form in designer. Here is code:

using System
using System.Windows.Forms
namespace SampleApplication
{
public class MyForm:Form
{
public NotMyForm()
{
}
public void InitializeComponent()
{
int i="x";
textBox1 = new TextBox()
textBox1.Text = "Hi"
this.Controls.Add(textBox1)
}
private TextBox textBox1
}
}

And here is screenshot of designer:

Sample Image

More information

To find more information, take a look at this link:

  • How does the Windows Forms designer in Visual Studio load a Form? (The original website is off - You can find the archive here)

Solution for your question

As a solution, it is enough for you to move private Numeric txtbox; and put it your seccond file in Exercise class.

Can't open design view on Visual Studio 2019 C# Windows Form Application

To check the target framework of your project, you can follow Project -> Properties... Application. And then you can see the target in the page.

Sample Image

If the target is .Net Framework, you can follow the steps provided by Flydog57.

If it is a Windows Forms App(.Net Core), there is no Designer by default.

The .NET Core Windows Forms designer is available with the Visual Studio 16.5 Preview 1. So you can try to update Visual Studio to Visual Studio 16.5 Preview 1 or a later version.

For more details, you can refer to this blog.

Updates to .NET Core Windows Forms designer in Visual Studio 16.5 Preview 1

Cannot Open Form [Design] View in Windows Desktop Form .NET Core Application

Edit: At the time the question was asked Microsoft hadn't released the visual designer for WinForms in .NET Core in Visual Studio 2019. It needed a separate preview install. By May 2020, Visual Studio version 16.6, the designer was still in preview but could be enabled from Tools/Options/Environment/Preview Features/'Use the preview Window Forms designer for .NET Core apps' without needing an install.

As of November 2020 the designer is still in preview, but is enabled by default in projects in Visual Studio 2019 version 16.8 and later. It's still not complete, particularly re data binding, but the number of issues is much smaller. It can now be disabled via the Tools/Options menu as discussed above.

Can't see the designer view in Visual Studio 2008 C# Windows Forms due to weird error

When you change the namespace or other item inside a partial class (like Forms) directly from the code editor you are making an invitation for madness. As the name suggest a partial class is a class that is defined "partially" on the code view, but there is another part which is generated automaticall by VS and that is the other part of the class. In that part it contains the definition of all UI elements, fonts, default values, etc. When you change the name space in one part of the class the other part don't know what do and then the interesting errors start. When changing namespaces, class names, event methods names always use the Refactor option in VS.

In your case, I would probably go back to the old name it had, and then use Refactor option VS provides (Highlight the component name, Ricgh click, refactor->rename)

Hope this help.

Why don't I see the Designer View in Microsoft Visual Studio 2019?

The provided code looks correct. But when you are using Windows forms with .Net Core you can't open the designer. This is a known limitation in the current version of Visual Studio. See here:

You could use .Net Framework

Visual Studio C# form's design view

You will have to move your public class Str class to be after your public partial class Form1: Form. Then:

Navigate to your Solution Explorer , find the Form.cs you're looking for, right click on the form, click View Designer.


The reason it is important to have your public partial class Form1: Form first is because that lets Visual Studios know that that file is for a Form rather than just another .cs file. Thus, letting you view the file in designer view.

It may also be helpful to add a new Code File to the project, and have your other classes within it, creating references as needed (for organization). This can help with Form.cs files as it avoids this kind of mix up from happening !

Hope this helps!



Related Topics



Leave a reply



Submit