473,396 Members | 1,853 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.

Having trouble with custom combobox control

Hello,

I am beginning to write a custom combobox control. Right now it doesn't do
anything special. I created a class that extends ComboBox:

public class TimeSelector : System.Windows.Forms.ComboBox

and in its constructor I call Populate:

public TimeSelector()
{
try
{
if(!DesignMode)
{
Populate();
}
}
catch(Exception ex_)
{
MessageBox.Show(ex_.Message);
}
}

where Populate is as follows:

private void Populate()
{
Items.Clear();
string[] times = new String[48];
DateTime dt = new DateTime(1,1,1,23,30,0,0);
for(int i = 0; i < 48; i++)
{
dt = dt.Add(new TimeSpan(0,30,0));
times[i] = dt.ToString("h:mm tt");
}
DataSource = times;
}

Now, when I start an empty form that contains this control, the combobox
correctly contains all of the items that I populated it with. However,
whenever I do anything to the control while I am looking at the form that
contains it in design mode, it gets messed up.

Specifically, when I go back to look at InitializeComponent in the form's
code, it seems to have added all of the times to the combobox thru a
DataSource and then it manually adds each time to the combobox's Items
collection right after that:

this.ts.DataSource = new string[] {
"12:00 AM",
"12:30 AM",
"1:00 AM",...

this.ts.Items.AddRange(new object[] {
"12:00 AM",
"12:30 AM",
"1:00 AM",...

This causes an error because you can't modify the combox's Items while it is
data bound. And this happens no matter what I do while in design mode (if I
move the control, set Enabled to false, whatever).

Can someone tell me what I am doing wrong?

Thanks,
-Flack
Nov 17 '05 #1
3 2180
I can see one thing immediately wrong.

You are testing to see whether you are in DesignMode, and populating
the combo box, in its constructor. The DesignMode property only words
after you have a window context for your control, and the constructor
is too early for that. You should be doing all of this work in the
control's OnLoad method, not in the constructor:

protected override void OnLoad(object sender, System.EventArgs e)
{
base.OnLoad(sender, e);

try
{
if(!DesignMode)
{
Populate();
}
}
catch(Exception ex_)
{
MessageBox.Show(ex_.Message);
}
}

Nov 17 '05 #2
Thanks for the reply.

I tried to do what you suggested but the combo box has no OnLoad method for
me to override.

"Bruce Wood" wrote:
I can see one thing immediately wrong.

You are testing to see whether you are in DesignMode, and populating
the combo box, in its constructor. The DesignMode property only words
after you have a window context for your control, and the constructor
is too early for that. You should be doing all of this work in the
control's OnLoad method, not in the constructor:

protected override void OnLoad(object sender, System.EventArgs e)
{
base.OnLoad(sender, e);

try
{
if(!DesignMode)
{
Populate();
}
}
catch(Exception ex_)
{
MessageBox.Show(ex_.Message);
}
}

Nov 17 '05 #3
Silly me. I was thinking of Form when I wrote that.

The closest thing I can find to try is OnCreateControl(). According to
the doc, this is called when the control handle is first created, so
that should be the right place to test DesignMode and have it be
meaningful.

I've never tried overriding OnCreateControl, but it's worth a shot.

Nov 17 '05 #4

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

Similar topics

10
by: Judy | last post by:
How do I create a combobox in a custom menubar to display months like: 1 January 2 February 3 March etc and then assign the month number (1, 2, 3, etc) of the selected month to a...
0
by: metamedia | last post by:
How do I get a datagrid to register a data change from a custom control cell (combobox) I've got a Windows Application with a Windows Form Datagrid that has a custom combobox column. When the user...
0
by: bell | last post by:
I was playing around with making a custom control very similar to the standard ComboBox: a TextBox combined with a ListBox. After trying it out, I ran into the following problem: When drawing my...
2
by: John B | last post by:
Hello I want to create a set of controls that have some common methods and properties. I was thinking that I should subclass the UserControl class and add these common features there and then...
1
by: philipl | last post by:
hi, i have a datagrid which display custom controls when user clicks edit, changing data. I am using customvalidator to validate this control. I can get validation of user input to work no...
2
by: Bernie Yaeger | last post by:
I'm trying to create a custom control that contains both a combobox and a timer. Any ideas how I can accomplish this? I do know how to create a 'one control' inheritance control, but when I add...
1
by: mookid8000 | last post by:
Hello again, group! Can someone tell me a nice way to include a zoom percentage combobox in a toolbar? My first try was a workaround, where I inserted a bunch of separators where I wanted the...
3
by: RSH | last post by:
I am slowly getting the hang of objects and creating my own. I was given some help in assigning an object to a ComboBox collection. I have been looking at it and I get the concept which is very...
8
by: mongphong28 | last post by:
Hi, I'm populating a ComboBox with objects which contain two properties - IntegerValue and StringValue. I display StringValue in the ComboBox form and can retrieve IntegerValue from the...
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
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
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?
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...

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.