Controls on WinForm not modifiable in Designer - c#

I've inherited a project that has been modified in a way that is beyond me to undo. Every single control on the form is not modifiable in the designer except through the Properties window and each control has an icon in the area below (see image) in the section usually reserved for non-visible items (e.g. DataSource). How do I undo this and return the designer to a usable version without wiping it all out and starting over?

Could the controls be locked? I have run into legacy code in which the programmers have locked every single control, meaning you cannot move or resize them in the designer. If so, select them all and change the Locked property to false (you should also see a padlock in the designer when you select them, if they are locked.
If not, well maybe you can create a new dialog/form and copy the controls unto it and see if that clears things up. Another trick could be to add a second instance of the user control(s) to see if the newly added control works as expected. If so, go through the code and point all events/logic to the new control (tedious yes, but may work).
Also, you can never edit individual sub-controls of a user control in the designer for the form/control where you instantiated the user control. You must go to the designer of the user control to edit individual (sub)controls of a user control.

If the Infragistics controls are shown in the Component Tray, than there might be version differences. What you could try is to open the licenses.licx file and remove the content from there. Do you have an Infragistics controls installed on your machine? Are there any difference after setting the "Specific Version" property of the Infragistics assemblies to "false"?
Is this happening only in your existing project or the same behavior is reproducible in a new project as well?

You have to load the dll that contains the controls you can't modify in the designer.
the steps are:
right click in the ToolBox in visual studio designer (on General for example).
select Choose Items...
after a while vs load items press browse and select the dll
interested than the componet should appear in the grid. Do this for
all the dll that contains the controls you are interested.
-Make sure you have checked the component (use filter text box if you
have a lot of component loaded).
press ok.
Now you should be able to move controls in designer.

I have also seen behavior this when a user control or form is incorrectly flagged as a 'Component'.
Some manual hacking of the .csproj file may be the answer then. Open the project file in a text editor, and find the references to your control. If you find a subtype defined as:
<Compile Include="MyControl.cs">
<SubType>Component</SubType>
</Compile>
The forms designer will interpret it as such. You can change the 'SubType' to 'UserControl' to fix it.

Related

C# Designer file: Error rendering control. Designer file out of sync

I have been looking at this for days. I have tried every option in the book and on the web, but have no luck.
There were no changes to the code where I am getting the error, but the designer file is out of sync. To me it seems like the NoteEditor.ascx.cs file can't read its control file NoteDetail.ascx.cs.
I am not sure why, I have tried deleting the designer file and recreating it. I tried closing all the project tabs and Visual Studio and reopened it, recreated the Designer files with no luck.
The error is that some controls don't have definitions even though they are all defined.
Is ChangeMode a control within the user control? Is it nested within another control?
When you look at designer.cs, does the control appear there?
If not then perhaps the designer isn't detecting your control. Try moving it to another location on the user control. Edit the ID, which may force the designer to recognize it. Once that happens you can move the control back to where it was.

C# settings form under settings menu

Is there a way to display the projects settings in a form inside the application?
I have added a Menu Item called setting, Now when that is clicked I want a small form to popuo with the applications setings.
Is there a quick way to do this that just displays the settings as you would see them in Visual Studio? Or do I have to manually fetch the settings and display them?
If I need to manually do it. Which type of form must I use so that it will appear inside my main form?
I think you'll manually need to collect them and display them. To display on an area inside your main form you'll want to use a UserControl and place your created UserControl on your main form.
Take a look at PropertyGrid control. It's the one what VS uses itself to edit properties of things (aka settings). Displaying things is pretty easy: add PropertyGrid to a form and assign object to it propertyGrid.SelectedObject. You will be able to see and edit properties of that object (in case of configuration settings this will be a configuration class instance I assume) almost right away! Tuning, adding localization, etc are not trivial tasks, but there are plenty of tutorials.
Otherwise, you could possibly use TableLayoutPanel with 2 columns: labels and respective texboxes (or other edit controls, to example, NumbericUpDown for int values), rows are autosized. But then you will have to add label and textbox for each setting yourself, as well as write some code to implement getting/setting of each configuration setting.

Delete control on form designer

I was wondering what is the best way to delete an unused control (more specifically, a timer) on Visual Studio 2010. I usually go to the control in the designer and delete it from there, but the code for the control still appears on the class form. Is it safe to delete the code associated with the control too?
As long as you don't need the logic, then yes. I assume you mean the handlers that are produced when you double click the control (such as button1_Click()). The link to the handler is removed when you delete the control, but visual studio leaves the functions in your form class in case you are using them for something else.
Delete a control through the visual editor, and the auto-generated code that creates the control and associates it with the form will be deleted as well. Code written by you, as well as event handling code generated through double clicking on the control or its events in the properties pane, will remain.
You should delete code that has to do with an non existent control. Any code that contains invalid references will keep you from building your application. Otherwise leftover code is just dead weight.

Cut,Copy and Paste Any UserDefined UserControl from/on the MDIForm

I am creating many usercontrols in an windows application in C# 3.5. I want to copy any usercontrol and paste it on another location of the MDIForm. Similarly in case of Cut option. I am using these three options in a contextmenustrip. And theses options are visible when I right click on the usercontrol. Can anyone tell me how It will be done at run time?
That requires giving the controls a new Parent. Explicitly supported by Winforms, they can even have no parent, quite a trick. You can do it directly by assigning the Parent property. Or by adding the control to another Controls collection, it will be automatically removed from the one it was in before.
Be careful, this flexibility comes with a price. It is also a source of a nasty leak that can crash your program after a while. That's caused by the no-parent trick, otherwise triggered by a Cut without a subsequent Paste. If you use Controls.Remove() or Controls.Clear() then the control is moved to the 'parking window', an invisible window created by the Winforms plumbing that acts as a temporary host. If you then don't either move the control to another parent or forget to call its Dispose() method then the control will live forever. Until your program runs out of resources or the user terminates the program.
The out of resources bomb ("cannot create window") typically happens after a few hours so is easily missed when debugging. You can see it in TaskMgr.exe, Processes tab. View + Select Columns and tick USER objects. Also tick GDI Objects and Handles to feel good about your program not leaking.
If you put the controls on a Panel then you can move them all together with just a single line of code by moving the panel.
You could remove the control from the ControlCollection in case of cut and cache it to add that control to some other form when pasted like you could do
panel1.Controls.Add(newPanelButton);// To add, you might have to change the control `Location` as per your need
panel1.Controls.Remove(newPanelButton);//To remove
In case of having cut/copy effect on the same form you could just change the Location of the control to the new location where you want to paste that control.

How to put extended WinForms Control on ToolBox

I plan to add functionalities to TextBox with the following:
public class TextBoxExt : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
}
}
The question is how can we use this TextBoxExt? Is there anyway to get this class onto the ToolBox so that we can just drag and drop it onto the form? If not, what is the best way to use the TextBoxExt?
I believe there are a couple of ways to get your control to appear in the toolbox:
The project it's in must be included in your open solution and the project must have been compiled/built into an assembly. This is the route to go if you're working on the control and a project that uses the control at the same time (e.g., building the solution will also re-build the control's project).
You can right-click the toolbox to add new items... in the resulting dialog, you can browse to the assembly containing the control and add it that way (or add it to the GAC, in which case you can pick it right from the list without browsing). This is the route to go if the project containing your control won't be a part of your solution and you're dealing only with the compiled DLL (e.g., building the solution doesn't re-build the control's project).
There is an another simple option to add a control into Toolbox is,
Create a new toolbox tab in the VS Toolbox. Say for e.g. "My Own Control".
Drag the assembly which has your control into the newly created tab and drop it.
You can see your control added in your Toolbox.
Main advantage of this method is, if you have more than a control in your single assembly, you do not need to search in the Add Components dialog and choose them. VS will do it for you and will add all those automatically in Toolbox.
Hope this helps.
Just compile your application - TextBoxExt should then show up in your Toolbox (you'll see it at the top, whenever you have a form designer open), and you can drag it onto your form.
The key here is probably to have a form designer open - otherwise, you won't see your custom user control in the toolbox.

Categories