Sev
Sev

Reputation: 15699

Winforms App Not Displaying Graphical Elements in Design Mode

I wrote a bunch of code in the .cs file in c# for a winforms application. The application runs fine, and everything is in it's place.

Something like this:

using..

namespace Temp
{
  public class Temp : Form
  {
    Button b1;
    TextBox t1;

    Temp()
    {
       b1.Text = "Some Text";
       b1.Size = new Size(50,20);
       ...
    }

    void function1()
    {
       // stuff
    }

    static void Main()
    {
       Application.Run(new Temp());
    }
  }
}

How can I modify my code (or fix it somehow) so that the design view displays the elements in their correct positions and view so that I can visually edit them instead of having to trial/error everything.

Edit for Clarification

My application runs fine. The problem is, that I didn't use designer to create the application and so in the designer view, the app is empty. But not empty when I run it, since everything is positioned programmatically in the .cs file. My question is, how can I fix this, so that the designer shows the objects correctly.

There is no quick fix other than to redesign everything?

Upvotes: 1

Views: 4886

Answers (4)

Paul Talbot
Paul Talbot

Reputation: 1603

Looks like this file was hacked from a class file instead of being generated by the system when you create a new winform.

You need at least an InitializeComponent(); call in your constructor. However you are missing a lot of other code that is generated for you when you create the file such as Dispose().

Best bet would be to right click your project in the solution explorer and click Add Windows Form then start over.

Upvotes: 0

Oliver
Oliver

Reputation: 45071

So to get this shown within the designer you have to know how the designer works.

For every MyForm.cs there will automatically be a file called MyForm.Designer.cs be created. Within this Designer file there will be only one function called InitializeComponents(). This function will be called within the constructor of your MyForm.cs file.

The design viewer itself is responsible for the Designer file, so any change to this file while the design view is open would normally be discarded. Also if you put some code into the designer file that is not needed be the designer will be truncated.

So the next question is, when will this truncation happen? When you freshly open the design viewer of a form, it will read in everything from the Designer.cs file without making any changes. If you make any changes onto the form by the designer the complete file will be rewritten with all the settings already read in including your latest changes.

This behaviour can be monitored if you open the designer file also as source code view, make some little changes in design mode and afterwards take a close look at the left of the source file. There will be the changes marked with a yellow or a green marker.

Now after all this stuff of informations, you can try the following procedure to get your code into the designer:

  • Open the design view and put some simple control onto your form (e.g. TextBox)
  • Save and close the design view and open the Designer.cs file as source file
  • Copy all your variables name of your controls at the end of the file, right below the textBox1 line
  • Copy all your control property settings within the InitializeComponent() function right below the property settings of the TextBox
  • Copy all your control constructors to the top of the file, right below the constructor of the TextBox
  • Save the file and open your form in design view
  • Select the dummy TextBox on the design view and delete it
    • This change within the DesignView leads to a complete rewrite of the designer.cs file, ordering all your manually added stuff the right way.

So this is the way to go. Last but not least another little trick:
Every programmer uses the using-statement to not write the whole path to every class (like System.Windows.Forms.TextBox), but the designer writes always the whole path. To make it a little easier for your copy and paste session you can also add a using statement at the top of the file. After saving and changing something in Design View all this stuff will be re-written automatically. So you don't need to add all this paths manually while your adding your stuff to the Designer.cs file.

Upvotes: 3

Ram
Ram

Reputation: 11644


In that case, you need to copy the designer code from CS to designer.cs. So that you can use designer. I think this is the simplest approach.

Upvotes: 0

Eltariel
Eltariel

Reputation: 1298

Your best option is probably to use the properties panel in the designer to set the positions etc (or maybe just drag them?).

You could go digging around in the designer file for the form (something.Designer.cs), but this isn't a fantastic idea because it can be pretty sensitive to changing things in ways the designer doesn't expect. Having said that, it looks like you're not actually using the designer to make your form (the class would be partial, for one thing), in which case you're SOL.

Upvotes: 0

Related Questions