I'm sorry, but I'm very new to this and I don't have anyone who can help me.
This may be basic but I've been researching this most of the night and I can't figure out how I'm supposed to code this.
I have developed a user control to use on several tab pages.
This user control has 11 listboxes for which I will need to capture the selectedItems based upon the tabpage and what is binded to the listboxes.
Once I have the UserControl onthe tabpages of my form, how do I retrieve the various selected items of each listbox
Also, I wanted to have a button on the UserControl that handled this, but I can't figure out that either. I guess I'll have individual buttons on each tabpage.
Sorry if this is a stupid question.

UserControl with multiple listboxes
Crane101
Kushan
Do you know what I should look for
I stepped through the code and the original UserControl (State) has the values listed in the properties (listBox.SelectedItem or SelectedValue) - however, when I'm using another control (County) and referencing the State properties in it, the property has no item or value anymore.
I'm not sure where its losing the reference.
Thanks.
Daniel Mart&#237&#59;n
In the user control, the user must first select the type of SQL comparative operator that they want to use when selecting states from the database, then the select states from the listboxes.
Here's some code from the UserControl (StateUC)
In this sample I've shown 1 label & 1 comboBox and in 1 groupBox, 2 labels & 2 listboxes:
private void TabListBoxControls_Load(object sender, System.EventArgs e)
{
try
{
DataView dvCompOP = new DataView(SqlHelper.ExecuteDataset(cnIPDDEV01, CommandType.StoredProcedure, "gsp_FilterApp_DropDown_CompOp").Tables[0]);
cbxStateSqlComp.DataSource = dvCompOP;
cbxStateSqlComp.DisplayMember = "CompOp";
cbxStateSqlComp.ValueMember = "ComparisonOperatorID";
}
catch(Exception er)
{
MessageBox.Show(er.ToString() + "\n Error in Zip Sql Comp Op");
}
GetState();
loaded = true;
}
//these have been declared globally - actually above
DataSet dsState = new DataSet();
DataTable dtState;
DataTable dt1;
DataTable dt2;
private void GetState()
{
try
{
SqlHelper.FillDataset(cnIPD, CommandType.StoredProcedure, "gsp_State", dsState, new string[] {"States"});//).Tables[0]);
dtState = dsState.Tables["States"];
}
catch (Exception err)
{
MessageBox.Show(err.ToString() + "\n ERROR State COMBO BOX" );
}
}//end of GetState()
private void cbxStateSqlComp_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(loaded == true)
{
fillLbx = false;
dt1 = dtState.Copy();
dt2 = dtState.Copy();
lbxState01.DataSource = dt1;
lbxState01.DisplayMember = "State";
lbxState01.ValueMember = "State";
lbxState02.DataSource = dt2;
lbxState02.DisplayMember = "State";
lbxState02.ValueMember = "State";
if(cbxStateSqlComp.SelectedIndex == 1)//there are other choices - including this for sample
{
StateEqual.Visible = true;
}
}//end of cbxStateSqlComp_SelectedIndexChanged
public string LbxState01Item
{
get{return (((DataRowView)lbxState01.SelectedItem)["State"]).ToString();}
//set{(((DataRowView)lbxState01.SelectedItem).ToString()) = value;}
}
public string LbxState02Item
{
get{return (((DataRowView)lbxState02.SelectedItem)["State"]).ToString();}
//set{(((DataRowView)lbxState02.SelectedItem).ToString()) = value;}
}
Then I made the user control a dll and referenced it in my Form1; here is some code from the form I'm trying to use it in:
protected StateUC stateUC;
private System.Windows.Forms.Label test;
private System.Windows.Forms.Button showSelectedState;
private void showSelectedState_Click(object sender, System.EventArgs e)
{
test.Text = stateUC.LbxState01Item;
}
When I run it and click on the button on the form, I receive the error "Object reference not set to an instance of an object". I guess its because I don't have the properties set properly in the UserControl - but I'm not sure how.
Thanks for loooking at this - I hope I set the tags like you wanted!
(My project is due Monday! :-( ) I appreciate the help!!
Adiavn
//SelectedItem (DisplayMember)
public string MyListbox01Item
{
get{return (((DataRowView)myListbox01.SelectedItem)["SpecialtyCategoryName"]).ToString();}
set{myListbox01.SelectedItem = value;}
}
//SelectedValue(ValueMember):
public string MyListbox01Value
{
get{return myListbox01.SelectedValue.ToString();}
set{myListbox01.SelectedValue = value;}
}
Just incase anyone else would want this!