Derived UserCcontrol base controls design time behavior

G

Guest

I have building a winforms control that inherits from another user control.
For instance Control FeesBase is derived from usercontrol and contains a
combobox.
I have exposed get and set property to access the comboboxes SelectedValue
property.
The combobo is prepopulated in control FeesBase contructor

Control FeeLookup inherits from control FeesBase.
I place a Label and TextBox in control FeeLookup
At design time if I change the name of the TextBox or label in control
FeeLookup via the properties screen an exception is thrown in control
FeesBase.

The error is:
Code generation for property 'FeeTypeID' failed. Error was: 'Property
accessor 'FeeTypeID' on object 'FeeLookup' threw the following
exception:'Object reference not set to an instance of an object.''

I have tried checking to see if the combobox is null but this still does not
work.

What is the best way to deal with this problem?
Is there something like the webform EnsureChildControls method?

FeesBase
code snippet
public struct ListControlItem
{
private int item;
private string value;
public ListControlItem(int i, string v)
{
item = i;
value = v;
}
public int Item
{
get { return item; }
}
public string Value
{
get { return value; }
}
}

public partial class FeeBase : UserControl
{

public FeeBase()
{
InitializeComponent();
PopulateFeeChoices();
}


private void PopulateFeeChoices()
{

ArrayList cList = new ArrayList();
cList.Add(new ListControlItem((int)FeeType.Bundled,
FeeType.Bundled.ToString()));
cList.Add(new ListControlItem((int)FeeType.Calculated,
FeeType.Calculated.ToString()));
cList.Add(new ListControlItem((int)FeeType.Lender,
FeeType.Lender.ToString()));
cList.Add(new ListControlItem((int)FeeType.Lookup,
FeeType.Lookup.ToString()));
cList.Add(new ListControlItem((int)FeeType.Zone,
FeeType.Zone.ToString()));

FeeTypeSelector.DataSource = cList;
FeeTypeSelector.ValueMember = "Item";
FeeTypeSelector.DisplayMember = "Value";
FeeTypeSelector.SelectedIndex = -1;
}

public int FeeTypeID
{
//TODO: Find the best way to deal with this. Get a design time
error that object does not exist
// this is a design time problem. need somthing like
EnsureCholeControls from web forms
get { return (this.FeeTypeSelector.Items.Count > 0 ?
(int)FeeTypeSelector.SelectedValue : -1); } //OK
set { if (this.FeeTypeSelector.Items.Count > 0 ?)
FeeTypeSelector.SelectedValue = value); } }
}

FeesLookup has no code other than that generated by the IDE
 
S

Stoitcho Goutsev \(100\)

Hi,

The controls are created fine. The exception that you get comes form the
FeeTypeID setter not because the combobox is not created, but rather because
there is no selected element in order to set its value. You explictly remove
combobox selection in PopulateFeeChoices - FeeTypeSelector.SelectedIndex
= -1;
change the line to be FeeTypeSelector.SelectedIndex = 0; and everything will
be OK

You cannot set the SelectedValue if there is no selected item in the
combobox
 
G

Guest

Thanks
--
EqDev


Stoitcho Goutsev (100) said:
Hi,

The controls are created fine. The exception that you get comes form the
FeeTypeID setter not because the combobox is not created, but rather because
there is no selected element in order to set its value. You explictly remove
combobox selection in PopulateFeeChoices - FeeTypeSelector.SelectedIndex
= -1;
change the line to be FeeTypeSelector.SelectedIndex = 0; and everything will
be OK

You cannot set the SelectedValue if there is no selected item in the
combobox
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top