I can't see my design view on c# - c#

I started working on a c# 2017 project, I closed the file of my program and the next time I opened it this error message showed up, if I try clicking on "omitir y continuar" the desing board is empty. I don't know what to do. If I click on the "iniciar" (start) button the program shows normally. And the line of code that says its wrong is:
this.button1.Click += new System.EventHandler(this.button1_Click);
Below is the image of the error.

This error indicates that you have a registered event for a button that does not exist - you must have deleted button1 from your form, but never removed its registered event.
You need to open your Form.Designer.cs file, where you will see the code line:
this.button1.Click += new System.EventHandler(this.button1_Click);
Remove this line and compile your project again, or switch to your Design view, which should now be fine.

Name of the reference variable (button1) is wrong or name of the method (button1_Click). Did you change any of them by your own? You have to chamge it in *.Designer.cs file accordingly.

Related

cannot add or remove an event handler "method group" but the designer can

When I try to add or remove an event handler the error is "cannot assign … to a "method group". Yet the exact line of code in the designer compiles fine.
I am filling in list boxes so the user can select the items. There are several SelectedIndexChange fire backs that I do not want to fire until I have everything in place. I no no trouble coded a numeric up-down to prevent it firing but I cant get the listbox coded and am forced to use semaphores to prevent unwanted things from happening. I looked at the VS2017 build "xxx.Designer.cs" and copied and pasted the exact line of code into the "xx.cs" but that error shows up.
{
cb_AppNames_SelectedIndexChanged -= new System.EventHandler(this.cb_AppNames_SelectedIndexChanged);
FillAppBox();
cb_AppNames_SelectedIndexChanged += new System.EventHandler(this.cb_AppNames_SelectedIndexChanged);
here is the code from the Designer.cs
// cb_SelProj
//
this.cb_SelProj.FormattingEnabled = true;
this.cb_SelProj.Location = new System.Drawing.Point(86, 25);
this.cb_SelProj.Name = "cb_SelProj";
this.cb_SelProj.Size = new System.Drawing.Size(279, 21);
this.cb_SelProj.TabIndex = 0;
this.cb_SelProj.SelectedIndexChanged += new System.EventHandler(this.cb_SelProj_SelectedIndexChanged);```
fixme1.png shows error messaged and fixme2 shows code that has no err
![1](http://stateson.net/images/fixme1.png)
![2](http://stateson.net/images/fixme1.png)
An event (like SelectedIndexChanged) is like a list of callbacks, which are called when the event occours. If you don't want/need this event before you filled your listbox, then do not add the callback by designer, but in your code after filling the data.
In your example you don't use the same code as in designer. The desigener adds the method b_SelProj_SelectedIndexChanged. In your code you try to remove and add the event cb_AppNames_SelectedIndexChanged itself. This will not work. You can only add and remove a method to or from an event.
You are basically correct but the actual problem was (1) about 3 in morning looking at this, (2) was unable to get .png file to show up at this forum - still do not know what I did wrong, and (3) due to not being able to see the .png in the "big screen" so I did not notice I was using "_" instead of "." when I tried to code the following up
this.cb_AppNames.SelectedIndexChanged -= new System.EventHandler(this.cb_AppNames_SelectedIndexChanged);
I was using small font in VS2017 for code and did not see the problem though it is plain here:
this.cb_AppNames.SelectedIndexChanged -= new System.EventHandler(this.cb_AppNames_SelectedIndexChanged);
this.cb_AppNames_SelectedIndexChanged -= new System.EventHandler(this.cb_AppNames_SelectedIndexChanged);
Maybe it is not so plain. I cant even get the code paragraphs to show up in correct order. I thought I had problems at askubuntu when a moderator sarcastically edited my post but I finally figured it out there. Not sure what is wrong here and it is not 3 in the morning. FINALLY GOT THE CORRECT FORMAT!!! Not going to complain, thankful enough that this forum is available.

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.

What's the correct way to change name of form element in C# Visual Studio?

I'm using Visual Studio, I have generated a form & added a button but unfortunately I have named btn_test1 instead of Btn_test1 (small vs large beginning letter) & double clicked the button to generate the click function and visual Studio happily generated the: btn_test1_Click() function & then complained that the function didn't start with a capital letter.
I thought: "Ok, no problem", went back & renamed the button to Btn_test1 & changed the click function to Btn_test1_Click() and now instead Visual Studio gives me an error that btn_test1_Click() is missing.
My question is: What is the proper procedure I should have used in this case?
If you don't want to write a long answer but have a link to some documentation it's fine too.
Okay, there is no need for a capital letter at the beginning of a name property, I'm also using VS 2017. I'll give some screenshots how to rename a function.
Step 1: Click on the events of the button in the properties panel as shown in the following image
Step 2: Find click event, and Delete the text
Step 3: Go to Properties in the properties panel
Step 4: Select the Name property of the button.
Step 5: Rename it as you like,
Step 6: Double click the button on the designer, it will automatically create a function with the new name.
As for the naming convention, use as I mentioned in the image.
for example, a button to send something can be named as btnSend.
btn -> stands for button. Send -> represents the name.
Note:
After you have successfully renamed and generated the new click event code. The old click event code will just be there. If you need the content of the old event copy it to the new click event code.
Also, if you still stuck with some error states that the old function is missing. Read the Designer.cs file carefully and delete all lines associated with the old function name.
Form1.cs [Design] crashes when you try to delete the event that is bound with button.
While making such changes -
Go to Button properties and remove the function name from click event.
Change the Button name
Again go to click event and do double click it (VS will generate click event for you)
Now you can delete the previous click event from your code without making your Form1.cs(design) crash.

How to modify code in designer.cs

I created a button in WinForms and generated an event using VS. Somehow the button disappeared. I checked the designer.cs code and found code specifying the button had changed to locates the button somewhere out of the visible form. I changed the designer code to the following:
// startButton
//
this.startButton.Location = new System.Drawing.Point(118, 758);
this.startButton.Margin = new System.Windows.Forms.Padding(6);
this.startButton.Name = "startButton";
this.startButton.Size = new System.Drawing.Size(140, 46);
this.startButton.TabIndex = 17;
this.startButton.Text = "Start";
this.startButton.UseVisualStyleBackColor = true;
this.startButton.Click += new System.EventHandler(this.StartButton_Click);
Prior to my attempt to change the button position back to where it was supposed to be the System.Drawing.Pont was set at -256, 758. I used another button position in the file to determine that the value should have been 118. However, when I restart the program the button is still missing. Why is this happening and how do I get the button back.
If I can't get the button back what happens if I delete the button code in designer.cs and the event handler. Can I then add a new button to replace the old. Just want to make sure that I don't make an irreversible change that compromises the entire program.
Thanks for any suggestions.
The preferred way to set the properties of your controls is through the Design view.
In order to do so you should have the "Properties explorer" visible. The easiest way to make it visible is to click in the "Properties" button on top of the "Solution explorer".
Now, on top of the "Properties explorer" you will find a drop-down list in which you can select all the elements of your form. Just find the button you want to edit, modify its Location property and Save.
Concerning your other question, you can delete your button and recreate it, but its bindings to the event handlers will be lost and you will have to recreate them. You can do this through the "Events" tab of the "Properties explorer".

Controls on Form Not Shown in Designer

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.

Categories