I am working with windows form since a few days, but having some question about the good way to play with the windows form designer. If I use "external" class with object data list and I want to use use them with a ComboBox for example:
this.comboBox1.FormattingEnabled = true; /*this.comboBox1.Items.AddRange(new object[] { "01", "02", "03" }); * */ Data myData = new Data(); this.comboBox1.Items.AddRange(myData.m_data);
then If I add modification to the form with the Designer all my added code is reset. Does that mean that If I insert code to the Windows Form Designer generated code I can't no more use the Designer ? or is there any secret tricks ? or is it better to not use the designer ?
thanks for you help.
VulpesPosted Sep 19, 2011, 3:47 PM
Basically, you should just use the designer to place controls on the form, set their properties and open up 'skeleton' eventhandlers for their events.
The best place to add items to your combobox is in the Form_Load eventhandler which fires before the form shows for the first time. Just double clicking anywhere on the form in the designer will bring up a skeleton handler for this event to which you can add your code.
rafPosted Sep 21, 2011, 4:56 PM
Sam HobbsPosted Sep 19, 2011, 6:51 PM
Note that C# has "partial" class definitions just so you can do that. The InitializeComponent() method is created in the designer.cs file, which is generated by the designer in VS, but the other file has the other portion of the class for you to modify as needed. It is designed so you don't need to modify the designer.cs file.
There is however a secret trick that object-oriented programmers use but the designers (programmers) of the C# IDE have hidden from C# beginners. You can derive a class from a control. That technique is probably not appropiate for this situation, but it can be very useful in many situations.
rafPosted Sep 19, 2011, 3:59 PM
rafPosted Sep 19, 2011, 3:28 PM
thanks for your reply. Actually I well understood that it is not recommanded to edit the generated code by designer and also not recommanded to write manually all code for my windows form application. So it seems that there is a third alternative but I don't really understand how to process. Does 'out of kilter" mean that I have to write two codes at the same time? a kind of template code generated with the designer and then I have to copy the template to my "current/real" code ?
VulpesPosted Sep 19, 2011, 5:02 AM