Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass Parameter to UserControl

Status
Not open for further replies.

eb24

Programmer
Dec 17, 2003
240
US
I have a main form (frmMain) that displays a listbox (lboData) with values from a datasource. Upon a user double-clicking on a value from this list, another form, frmFeature, is displayed. This frmFeature contains a UserControl, ucFeature that is suppose to get the ID from the lboData from the frmMain.

This is what I have for frmMain:
Code:
private void lboData_DoubleClick(object sender, EventArgs e)
{
     string myID = lboData.SelectedValue.ToString();
     frmFeature frm = new frmFeature();
     frm.UC.LoadData(myID);
     frm.ShowDialog();
}
This is what I have for frmFeature:
Code:
public frmFeature()
{
     InitializeComponent();
}
public ucFeature UC
{
     get 
     {
          return ucFeature1;
     }
}
This is what I have for ucFeature:
Code:
public partial class ucFeature: UserControl
{
     public ucFeature(string myID)
     {
          string myID = null;
          InitializeComponent();
          DisplayID(myID);

     }
     public void DisplayID(string myID)
     {
          lblID.Text = myID.ToString();
     }
...
When I run the code, I get the following: Message="Object reference not set to an instance of an object.". Any and all assistance will be greatly appreciated.
 
Code:
     public ucFeature(string myID)
     {
          [COLOR=red]// explicitly setting myId to null; why?[/color]
          // did you mean this to be a member variable?
          string myID = null;
          InitializeComponent();
          DisplayID(myID);

     }
     public void DisplayID(string myID)
     {
          [COLOR=red]// why are you calling ToString() on a string?[/color]
          lblID.Text = myID.ToString();
     }

mr s. <;)

 
Code:
public ucFeature UC
{
     get { return ucFeature1; }
}
is ucFeature1 initialized in the designer (view or code)? if not this may fix the problem.
Code:
public ucFeature UC
{
     get { return new ucFeature("ID"); }
}


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
the code as provided will throw an error on [tt]lblID.Text = myID.ToString();[/tt] because myID is being set to null.

take out the line [[tt]string myID = null;[/tt] and it should run.

mr s. <;)

 
OK, I'm still a bit confused:

This is what I basically want (call-order):

frmMain --> frmFeature --> ucFeature

I want my ucFeature to get a parameter from frmMain so it can be used within my ucFeature. Maybe my initial code was a bit confusing. I added DisplayID just so I could see if it was being passed. Since then, I have deleted some code.

So, basically, I want the following line from frmMain:
Code:
frm.UC.LoadData(myID);
To be received by my UserControl, ucFeature:
Code:
public void LoadData(string myID)
{...}
Does this make better sense? Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top