Seeking better way to load user controls in form

R

Ronald S. Cook

I'm developing a Windows application that will have an ListBar like in
Outlook. When the user clicks on a ListBar item, I would like to load the
associated User Control. The below code works fine, but I'm looking for
something more dynamic (since we'll have dozens if not hundreds of user
listbar items/user controls).

private void ulbListBar_ItemSelected(object sender,
Infragistics.Win.UltraWinListBar.ItemEventArgs e)
{
splContainer.Panel2.Controls.Clear();

switch (e.Item.Key)
{
case "keyTesting_CountryList":
uctTesting_CountryList _uctTesting_CountryList = new
uctTesting_CountryList();
splContainer.Panel2.Controls.Add(_uctTesting_CountryList);
break;

case "keyTesting_StateList":
uctTesting_StateList _uctTesting_StateList = new
uctTesting_StateList();
splContainer.Panel2.Controls.Add(_uctTesting_StateList);
break;
}
}

Thanks for any suggestions.
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ronald,

Well, most, if not all controls have a parameterless public constructor.
Combine that with the fact that you know all the controls are going to be
passed to the Add method on the controls collection exposed by the Controls
property on the Panel2 control, and you can easily use reflection (along
with a dictionary or some other lookup mechanism to determine the type to
create) to create the instance and add it to the panel.

Hope this helps.
 
G

gshzheng

Hi,

All your list should inherite a same baseclass,and the main form only deal
with
the base calss.

Your sub-class,uctTesting_CountryList..,should be created by a
xxxFactory,then
added into a list in main-form.

Main-form control the 'list' using abstract interace,and 'list' do all the
concrete things.

gshzheng
20070320
 
R

Ronald S. Cook

Thanks Nicholas.. any chance you'd have a code snippet to throw my way?

Thanks,
Ron


Nicholas Paldino said:
Ronald,

Well, most, if not all controls have a parameterless public
constructor. Combine that with the fact that you know all the controls are
going to be passed to the Add method on the controls collection exposed by
the Controls property on the Panel2 control, and you can easily use
reflection (along with a dictionary or some other lookup mechanism to
determine the type to create) to create the instance and add it to the
panel.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
I'm developing a Windows application that will have an ListBar like in
Outlook. When the user clicks on a ListBar item, I would like to load
the associated User Control. The below code works fine, but I'm looking
for something more dynamic (since we'll have dozens if not hundreds of
user listbar items/user controls).

private void ulbListBar_ItemSelected(object sender,
Infragistics.Win.UltraWinListBar.ItemEventArgs e)
{
splContainer.Panel2.Controls.Clear();

switch (e.Item.Key)
{
case "keyTesting_CountryList":
uctTesting_CountryList _uctTesting_CountryList = new
uctTesting_CountryList();
splContainer.Panel2.Controls.Add(_uctTesting_CountryList);
break;

case "keyTesting_StateList":
uctTesting_StateList _uctTesting_StateList = new
uctTesting_StateList();
splContainer.Panel2.Controls.Add(_uctTesting_StateList);
break;
}
}

Thanks for any suggestions.
Ron
 

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