473,748 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 InitializeCompo nent 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.DataSou rce = new string[] {
"12:00 AM",
"12:30 AM",
"1:00 AM",...

this.ts.Items.A ddRange(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 2193
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.EventArg s e)
{
base.OnLoad(sen der, 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.EventArg s e)
{
base.OnLoad(sen der, 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
2208
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 textbox on a form? Thanks for all help!
0
1539
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 clicks on a new row in the datagrid and tries to select or enter a new value in the combobox, the datagrid doesn't seem to register the change, so if the user tabs through the columns and onto the next row, the data is cleared and not saved Does...
0
1090
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 custom control at DesignTime, I need to size it so that I give enough room for both the text and list boxes. But the standard ComboBox has the List portion pop up outside of the "size" of the control, overtop of existing controls. How can I...
2
2096
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 base all my custom controls on this new MyUserControl Class. Is this the way to go? The MyUserControl would not actually have any UI component and should be abstract I think. Does this mena that I should not create the control by just selecting...
1
1754
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 problem when user clicks on update in datagrid. My problem is that when I try to get customvalidation to work when user changes the value in the control, as i want this to be checked as soon as user changes the control. The custom control offers an
2
2380
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 the timer, it compiles but I don't get its functionality - and it's not ticking, as I want it to. Thanks for any help. Bernie Yaeger
1
2141
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 combobox to be, and then painted the combobox on top. I'm just thinking, there must be a better way... :o)
3
1349
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 powerful but I'm having a bit of a problem conceptually. I was hoping someone might be able to shed some light on creating this object and assigning it in the manner. Also how would I reference this object by name after it is created in this...
8
3520
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 SelectItem.IntegerValue property. This is all good but now I want to get a little bit more complicated. I want to be able to set the ComboBox's SelectedItem property by setting IntegerValue, so for instance if I have the following items in
0
8995
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9331
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.