Controls on Form Not Shown in Designer - c#

I am working on a WinForms project, and I have a form where I have a DataGrid, a TextBox, and 2 button controls (btnNew and btnSearch). The click event of a btnSearch is supposed to perform a search on the DataGrid.
I deleted the event handler for the search button and have saved my work. It now appears that all other controls have been deleted and the form is back to the default state. The application works fine though when run, with some errors. I have resolved the error but the designer view is still in the default state. How do I go about reinstating my form's design view?

i had your problem and solved it in this way:
just make another form with another name(NewForm.cs) and copy the InitializeComponent() content from YourFirstForm.designer.cs and paste it into
NewForm.designer.cs InitializeComponent() function. but be careful when copy and paste the function content change all YourFirstForm keywords to NewForm . finally Remove YourFirstForm and just work with your NewForm....

With the control selected in the properties window.
Right click on an empty space on the form,
Click cut
Right click / then paste into a cell somewhere in a form control.

I've run into errors like this before where simply closing all the form's files (code view + design view) re-open the code view and then Shift-F7 to reload the design view would fix it.
If that doesn't work perhaps fixing the error you mentioned in the designer view caused something to get out of whack. Try comparing the structure in the YourForm.Designer.cs file with a new form to see if an inadvertent edit was made.

i had your problem in Visual Studio 2017 and solved it in this way:
-select controls by Properties
-view Location is -
-change the Location
-drag the control to another location

Make sure all errors related to form are removed and your code compiles successfully.
If Form has been copied from an existing project make sure Form.Designer.cs and Form.resx are under same parent Form.cs and not separate. Check Form tree in Solution explorer. If they are not under the same tee, load the designer.cs and .resx file again in the project.

Related

Designer error when redirecting a button click within a custom user control

I've created a custom user control, that is essentially a custom button within my windows form app. I managed the redirect of the click event to using the following code:
Control[] customButtonControls = button.Controls.Find("buttonInUserControl", false);
Button nestedButton = (Button)customButtonControls[0];
nestedButton.Click += new System.EventHandler(this.button_click_handling_function);
I've appended this to the Window_Name.Designer.cs file below the generated code for the control with my button_click_handling_function being defined in my Window_Name.cs file.
The issue is that when I then click back to the Window_Name.cs[Design] page, I am met with an error page. I will include screen shots to better show the errors. Basically it is a super unhelpful page. It tells me that I have an index out of range error on my Array, but the stack call makes no sense.
If I try to build my Solution, I am met with NO compile errors and my program acts exactly as intended. The click event triggers the function just as before.
Thanks in Advance.
Portions of the designer code are run at design-time. The index out of range error is probably because at design-time there are no controls found yet by that Find call so the array is empty. You are not checking for 0 length so when you de-reference it you get the error.
It works at run-time because at that point the controls have been instantiated.
The secondary problem though is you should not put things into the Designer.cs files since that code is auto generated by the designer and could be regenerated at some point and your added code lost. Put that code in the Window_Name.cs after the InitializeComponent call.

Visual Studio C# form's design view

Hi,
Where can i open form1.cs's design view? I've never had this problem before, and i searched everywhere. It hidden. I have 2 forms, i can open the second (name is AddNew) design view, but the primary form's design view is hidden.. Shift+7 not working. In the souliton bar only have the second form: AddNew.cs, and with right click the 'Open Design View' is appear. But the Form1??? I can't found, please help.
Many Thanks!
Mark
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!

ParentForm is null (in a Button inside a UserControl inside a Form)!

I got this code working!
I have a button inside a my UserControl dropped at design time in a my Form. All worked well and when in a button's event I called this.ParentForm it correctly returned to me the (only) parent Form.
After a refactoring, I moved the UserControl with the button to another NameSpace and the same piece of code no longer works. this.ParentForm now is NULL!
I read the MSDN site and it says that only when the control is hosted in IE or another context this.ParentForm returns null. But I moved only the namespace!
Anyone has an idea?
I cannot use a different constructor to pass it the parent form because at design time Visual Studio wouldn't render the Form.
See if you're accessing the control's parent form before the control has been added to the form.
Your code my be triggering upon the user control class initialization, but the control hasn't been added to the Parent form.
Try putting this code in the Control Load event.
Look at the code-behind file that contains the designer-generated code. During the renaming, you may have confused the Visual Studio designer and it may have "orphaned" an instance of your control in the Form.designer.cs file.
In particular, look at the code in the InitializeComponent method and see if you can spot any code that creates an instance of your UserControl but does not add it to a container, or adds it to a container that is not added to the form.

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?
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.
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.
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.
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.

C# root form inheritance, changes cause broken child forms

Hi I've setup a few subforms all inheriting from a root form that sets up a few buttons and logos etc to then filter down to the children.
I used the visual studio wizard to add the subforms inheriting from the root and they are automatically declared with:
public partial class WelcomeForm : MynetInstaller.rootForm
I've now been asked to resize the subforms and move a button, things I hope would be done by simply changing the root form and allowing the changes to filter down.
The two problems I have are:
1/ I change the size, this does not affect the children at all, it seems after the initial setup the size is not inherited.
2/ I change the location of a button, this causes all subforms to break showing an error that:
To prevent possible data loss before loading the designer, the following errors must be resolved:
'child' is not a child control of this parent.
Instances of this error (1)
1. Hide Call Stack
at System.Windows.Forms.Control.ControlCollection.GetChildIndex(Control child, Boolean throwException)
at System.Windows.Forms.Control.ControlCollection.SetChildIndexInternal(Control child, Int32 newIndex)
at System.Windows.Forms.Control.ControlCollection.SetChildIndex(Control child, Int32 newIndex)
at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.SetChildIndex(Control child, Int32 newIndex)
If i press continue it will load the page, but wont have moved the button and if i try and run it i get an error in:
this.Controls.SetChildIndex(this.btnNext, 0);
Saying:
'child' is not a child control
I noticed that when i move the control it stops becoming locked, but changing this manually doesn't help.
The base form's Size is inherited, the designer normally prevents setting the size again in the derived form. But mishaps can happen, a sub form might have been subtly altered once. Or it might have been designed on a machine that had a different video DPI setting from the machine on which the base form was designed.
There is only workaround for this that I can think of, edit the designer-generated code. In the Solution Explorer window, open the node next to the sub-form and double-click the Designer.cs file. Expand the region labeled "Windows Form Designer generated code" and locate the assignment to the ClientSize property. Delete it.
No idea what's going on in your second problem, this is not a normal issue. Take a look at what happens to the designer generated code when you manipulate the button.
For "'child' is not a child control" problem you must remove from .designer.cs file .setChildIndex command lines.

Categories