473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

user control, which way is better and How InitializeComponent() {} is generated? What to do if I need change code in InitializeComponent(){}?

I think there are two way to define user control.

For example, I want to define a ComoBox with contains only AM, PM items.

a.. One way I define a class extends System.Windows.Forms.UserControl, and put a ComboBox on it.
b.. Another is Ithat I define a class (TimeSegmentComboBox ) extends ComboBox.
Either way, I have code to populate AM, PM to Combox.

In the second case:
public class TimeSegmentComboBox : ComboBox
{
public TimeSegmentComboBox() //constructor
{
InitializeComponent();

if(!this.populated)
{
this.AddItemsCore(new object[] {TimeSegment.AM , TimeSegment.PM});
this.SelectedIndex = 1;
this.populated = true; //this flag does not help
}

}
}

I prefer this second way, but it has a problem. When I put this user control on a form in Visual Studio 2003, it generated code including
code add AM PM to Combox again.

private void InitializeComponent()
{
.......
//
// timeSegmentComboBox1
//
this.timeSegmentComboBox1.Items.AddRange(new object[] {TimeSegment.AM,TimeSegment.PM}); //need manually remove this line later
this.timeSegmentComboBox1.Location = new System.Drawing.Point(136, 184);
this.timeSegmentComboBox1.Name = "timeSegmentComboBox1";
this.timeSegmentComboBox1.Size = new System.Drawing.Size(80, 21);

.......
}


This way, the code populate Combox with AM, PM will be shown and executed twice. I can only manually delete the code generated by Visual Studio in Form1.cs. But the code in InitializeComponent() should not be manually edited.

So my first question, is what is the mechanism for Visual Studio to auto-generate InitializeComponent() for (user) controls?

The second question is, when there is a need to modify InitializeComponent(), what is the better solution? If I manually edit anything, I lost it when I go to design view.

Thanks a lot!
Ryan

Jul 21 '05 #1
0 1686

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
0
by: Ryan Liu | last post by:
I think there are two way to define user control. For example, I want to define a ComoBox with contains only AM, PM items. a.. One way I define a class extends...
0
by: Ryan Liu | last post by:
I have a user control, it must set a property when it is just created, so I set it in its constructor, which has an parameter to specify the property . For example: public class ExecQuest :...
7
by: moondaddy | last post by:
I have a user control being used instead of a frame page. when the user clicks on a menu item I need to send the ID (integer value) of that menu as a parameter in the postback of the user control...
5
by: Marcel Gelijk | last post by:
Hi, I am trying to create a User Control that is located in a seperate class library. The User Control contains a textbox and a button. The page generates an exception when it tries to access...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
2
by: mharness | last post by:
Hello, I've tried a number of examples showing how to read the properties of a user control from an aspx file where the code is on the html view of the form but I can't figure out how to read...
9
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.