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 ( );
private
RadioButtonoptionListBox =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" );
}
}

privatevoidClearForm ( )
{
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

[14318 byte] By [Rhubarb] at [2007-12-25]
# 1
this.Controls.Remove(this.thisListBox)
JRQ at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

Thank you JRQ, your response cleared up one facet of my dilemma. Only, when I build the app, because I create the Control in it's method (i.e. private void OnOptionListBox_Click), I get this error:

Error 1 'GenTestInCsharp.formControls' does not contain a definition for 'thisListBox'

This leads me to believe that if one wishes to create controls at runtime, all controls should be created at the 'class level' and not in individual methods?

Thanx;

Greg

Rhubarb at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

You can add a new control inside a method to the form/container control collection, but the class/IDE will not see it; however, the object reference will persist (as long as you didn't do a dispose inside the same method).

MyMethod()
{
new myControl

myControl.Name = "SomeName"
this.Controls.Add(myControl) // this will persist the reference outside the method
}

You won't be able to access myControl outside the method, but this.Controls will have the reference as one of the entries. You will have to use the Name attribute to get the object back from the control collection by iteration or for each statement.

JRQ at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms General...