Windows Forms designer and custom CodeDomSerializer - c#

I am currently putting together a prototype for a custom tab control we are trying to implement in c# for .Net 2.0. As the control itself works fine, we are now trying to integrate full design-time behaviour. This does include a custom CodeDomSerializer at a certain point to tell that the tabs have to be added as well. This does work fine now (after a longer process of trial and error).
But there is a problem left that seems to have to do with Deserialization. The designer code is created just fine and the designer looks just the same. But when I hit rebuild project, the forms designer sometimes show this error:
The variable 'piTabControl1' is either undeclared or was never assigned
http://msdn.microsoft.com/en-us/library/4008y84t(v=vs.80).aspx
The error does not appear every time, but it always happens when I have just added a new tab. The error points to this line:
this.Controls.Add(this.piTabControl1);
But I know that the variable is assigned, as this is in the code a few lines before:
this.piTabControl1 = new pdfforge.PdfCreator.Controls.PiTabControl();
The application also does compile and run with everything shown properly. If I close the Designer and reopen it, it is also shown without that error. So I suspect that there is something going wrong when Deserializing the code or updating the designer.
I have uploaded the full solution here, because it is just too much code to paste here:
http://www.chinery.de/dateien/TabControlTest.zip
To reproduce this in the project, you will first have to recompile the whole solution for the custom controls to be compiled. Then you can open Form1 and hit rebuild. If it does not produce the error, you can try adding or deleting tabs from the tab control.
I really don't have a clue how to approach this, because I can't tell the inner state of the Forms designer. I am very thankful for any hint. I can also add specific code snippets here if requested.

Related

VS crashes and winforms data lost

I am developing an application using Visual Studio 2019 and I am designing winforms.
Visual Studio commonly crashes when I am creating the forms by freezing, and recently my designed winform has somehow disappeared (even though I saved it). When i open it it shows as the default winform that is shown when a new one is created:
When I browse the solution explorer it seems that I can still see all the components I had added to the form:
Finally, when I try to restart and manually add the components back, I get an error message saying that the component can't be added due to duplication - which is probably because they are somehow still there (as shown in the solution explorer) but the data has been corrupted somehow:
If anybody has any idea of how to recover the data and the winform, I would very much appreciate it. If any additional information is required, please let me know. Thanks!
According to your description, first, the controls actually exist, but they are not
displayed in the design window.
You can try to check whether the. add statement of each control added to the window still
exists in the .Design.cs file.
If not, you can add it manually.
Second, solution explorer shows the missing components, but there is an error in adding
components. It means that only the declaration of this control in the .Design.cs file does
not assign a value to this control. You can delete the declaration of the component in the
.Design.cs file, and then add the component in the design interface.
Result:

Adding a form seemed to cause a 'lockout' of my other forms

Working on a fairly simple database application in visual studio 2017 and c# with a login screen to unlock all the other forms. Working just fine.
Tried adding another form for another feature and suddenly this caused the login screen to be 'locked out', as in it appears on startup like it should but nothing is selectable, everything is greyed out.
I have tried removing every single reference I can find to the newly created form but still my entire program is basically broken from just adding a form. I had added forms before this with no problems.
I'm not expecting anyone to wave a magic wand and fix it but if anyone has any pointers of what to check or look for? Happy to answer questions and go into more specifics if needed.

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.

Form Designer unable to find type

In a windows form I am populating a combo box with the following code.
this.JackStudTypeComboBox.Items.AddRange(ViewController.GetJackStudMaterialType().ToArray());
When I attempt to open the Form Designer I get the message that the ViewController type is not found.
The ViewController class is located in the same project as the form. Compiling the project has no issues, and running the project results in the expected behaviour.
So, how do I convince the Form Designer to locate this particular class, or to ignore the fact that it is unable to find it or do anything useful with it when in static design mode?
Don't do that.
You should never put any custom logic in InitializeComponent().
Move that to the constructor and everything will work fine.

Visual Studio isn't generating code to instantiate instance of custom control

I've created a custom control using C#, .Net3.5, and Visual Studio 2008.
I'm then adding that control to another control by dragging it from the toolbox.
After doing this when I try to compile I get an error as follows:
Error 1 Warning as Error: Field 'MyNamespace.MyControl._myCustomControl' is never assigned to, and will always have its default value null
Looking at the Designer.cs file this is due to the fact that VS never generates the following code:
this._rgReportGallery = new MyNameSpace.MyCustomControl();
The field itself is there, and the code to add it to the control I'm putting it in, just the instantiation code is missing.
I can add that code manually and everything seems to work, but as soon as the designer.cs file is regenerated it goes missing again.
I've even successfully added a different custom control and had that code get generated.
Does anybody know what could be causing this?
I was having this issue and a reply here helped me find the issue, I was using the "internal" keyword for both my component accessibility and the constructor accessibility. As Brent mentions in a couple posts above mine, VS designer apparently has issues generating an instantiation line with anything except a public constructor, so never make a custom user control where the class accessibility and constructor(s) are anything but "public."
By the way, this occurred for me on Visual Studio 2005, and it doesn't surprise me that it also would happen on Visual Studio 2008. It likely happens on earlier versions of VS as well.
Have you adjusted the constructor of the custom control in any way? The default constructor for controls is a parameterless one, and I know I've had trouble in the past when creating a constructor for my controls that requires parameters. In those cases I always provide two constructors. That way the parameterless one remains for the design mode in VS.
Also make sure your UserControl's constructors are public. I've had issues with internal UserControls and the VS2008 SP1 WinForms designer code generation. It failed to generate the allocation line like you mentioned.
Even though the .Designer.cs files look like simple code, playing around in that code is very risky because it can easily break the Windows Forms Designer. I've heard a lot of people just abandoning the WinForms designer because they say it does not work. It's indeed easy to break, but if you don't try to play in the .Designer.cs file, it works like a charm.
If you have edited the Designer.cs file manually, it might explain why it keeps rewriting itself wrong. It's hard to know how to fix it though. If your UI is not too hard to redo, I would just redo it (whatever is in your main .cs file will be untouched). Otherwise you can look at a similar form to see a model of how exactly it should be formatted (the order of the statements is important).
Do you have the latest service pack for Visual Studio 2008? This usually happens when you don't have the latest service pack.

Categories