runtime controls
I want to be able to create a ListBox or ListView at run time to enable the user to view available data. As they click on an option, the results are displayed in a ListView of related subject matter. However, when the ListBox is clicked, I need to remove it from the form or, at least make it's visibility false.
My problem is that I can't seem to find out how to identify the control after it's added to the form. I've read the online help and so far can't make heads or tails of it!
I add controls at run time:
privatePanelpanelOptionSelect =newPanel ( );privateRadioButtonoptionListBox =newRadioButton ( );
privateRadioButtonoptionListView =newRadioButton ( );//*
//* Panel
//*
panelOptionSelect.Anchor =AnchorStyles.Left;
panelOptionSelect.Dock =DockStyle.Left;
panelOptionSelect.AutoSize =true;
panelOptionSelect.BorderStyle =BorderStyle.Fixed3D;//*
//* Option ListBox
//*
optionListBox.Text="ListBox Control";
optionListBox.AutoSize =true;
optionListBox.Click +=newSystem.EventHandler (this.OnOptionListBox_Click );//*
//* Option ListView
//*
optionListView.Text ="ListView Control";
optionListView.AutoSize =true;
optionListView.Location =newPoint (optionListBox.Location.X,optionListBox.Location.Y +optionListBox.Height );
optionListView.Click +=newSystem.EventHandler (this.OnOptionListView_Click );panelOptionSelect.Controls.AddRange (newControl[ ] {optionListBox,
optionListView} );privatevoidOnOptionListBox_Click (objectsender,EventArgse )
{
ClearForm ( );
try
{
ListBoxthisListBox =newListBox ( );
thisListBox.Size =newSize (232,342 );
thisListBox.Location =newPoint (140,10 );
this.Controls.AddRange (newControl[ ] {thisListBox } );
this.textBoxSelected.Text ="ListBox";
}
catch(Exceptionex)
{
MessageBox.Show ("Display ListBox Error" +"\n\nError message.....: " +ex.Message,"Show List BOx" );
}
privatevoidOnOptionListView_Click (objectsender,EventArgse )
{
ClearForm ( );
try
{
ListViewthisListView =newListView ( );
thisListView.HeaderStyle =ColumnHeaderStyle.Nonclickable;
thisListView.View =View.Details;
thisListView.Location =newPoint (140,39 );
thisListView.Size =newSize (555,237 );
this.Controls.AddRange (newControl[ ] {thisListView } );
}
catch(Exceptionex)
{
MessageBox.Show ("Display ListView Error" +"\n\nError message.....: " +ex.Message,"Show List View" );
}
}
{
switch (this.textBoxSelected.Text )
{
case"ListBox":
this.Controls.Clear ( );
break;
}
}
I get the controls to display just fine, until I try to access them outside of their individual "Click Events". For instance, if the ListBox is enabled on the form and I click on the ListView Option, I want to remove the ListBox and Display the ListView. But, nothing I've done so far (except this.Controls.Clear) seems to remove just the control. I need the Panel and it's contents to remain in view.
Any help is appreciated.
Greg

