473,395 Members | 1,462 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,395 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
Nov 16 '05 #1
0 1061

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

Similar topics

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...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.