Combobox

  • Thread starter Thread starter Michel Racicot
  • Start date Start date
M

Michel Racicot

I have a custom control derived from ComboBox that I need to load itself
depending on one of its parameter.

What event can I use for that? It doesn't seem to have a "OnLoad" event...
 
I have a custom control derived from ComboBox that I need to load itself
depending on one of its parameter.

What event can I use for that? It doesn't seem to have a "OnLoad"
event...

Technicality: There is no OnLoad event. OnLoad is the name of a method. Load
is the name of an event.

Anyways, the way to do this is to handle it in the constructor for your
custom control. This is, of course, assuming I understand what you meant by
"load itself" correctly.
 
The DataSource property should be set by
the caller to load the ComboBox with data.
I think that is the right way.

But if you have an overridden Constructor
maybe the Load can be done there.

Roger
 
The problem is that my custom property isn't set yet in the constructor....
I don't understand why...
 
The problem is that my custom property isn't set yet in the
constructor.... I don't understand why...

Can you explain what that means? Do you mean that you need a property
to be set "within" the constructor, so that you need a non-parameterless
(if that is a word) constructor, something like below? I don't think
this is possible for a control given that a user may drop it onto a form.

public class MyComboBox : ComboBox
{
public string ConnectionString {get; set;}
public MyComboBox (string connectionstring)
{
ConnectionString = connectionstring;
// open a dataset, for example...
}
}
 
Far more easy to understand ;)

I've added a string property (named "TokenType" to the custom combobox...
dropped the customcombo on a form and set the new property in the property
inspector in design time.

But if I try to access the value in the constructor of the ComboBox (to load
its value), it's not set! The string is empty... So I can't use it to load
the combo correctly.
 
Obviously, from the code in the designer of the form, the constructors of
child controls are called before setting the properties... That makes sense.

But I need an event that will be called after the properties are set... like
a custom "OnLoad" event.
 
Obviously, from the code in the designer of the form, the constructors
of child controls are called before setting the properties... That makes
sense.

But I need an event that will be called after the properties are set...
like a custom "OnLoad" event.

Assuming you want to fill the combobox items when the property changes,
in the designer or at runtime, then I would code it like this:

public class MyComboBox : ComboBox
{
string _TokenType;
public string TokenType
{
get { return _TokenType; }
set
{
_TokenType = value;
Items.Clear();
Items.Add(TokenType);
SelectedIndex = 0;
}
}
}
 
Back
Top