0

I have created a UserControl which has a picture and two labels: labelName labelUsername

I have also created a DataSet and DataTable which gets the data from a SQL CE database. This bit is working fine as I managed to loop through all the DataTable rows and display the information in a MessageBox.

Now I want to display the UserControl in a FlowLayoutPanel for all rows in the DataTable and populate the two labels with the Name and Username values from the DataTable. This is where I am stuck as I don't know what to code in the UserControl and what to code in the Form that contains the FlowLayoutPanel.

Can someone help me out please?

hshah
  • 842
  • 4
  • 14
  • 35

1 Answers1

1

You'd code this in both your Form and your in UserControl.

In your UserControl expose the Text property of each of your two Labels in a property or a method. If you choose a property, it might look something like this for your Label labelUsername:

public string Username {
    set { labelUsername.Text = value; }
}

In your Form loop through all DataRows in all DataTables in your DataSet and for each DataRow create an instance of your UserControl and add each to your FlowLayoutPanel. Use the appropriate column values in your DataRows to set your UserControl Label values:

foreach (DataTable dt in ds.Tables) {
    foreach (DataRow row in dt.Rows) {
        var uc = new YourUserControl { Username = row["usernameColumn"].ToString(), 
                                       Name = row["nameColumn"].ToString() };
        flowLayoutPanel1.Controls.Add(uc);
    }
}
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • Would you happen to know why the .MouseClick I have implemented only seems to work when I select a white space on the UserControl rather than a label or image in the UserControl? – hshah Sep 25 '12 at 22:24
  • Glad it works. I bet you're seeing the mouse click behavior you describe because the MouseClick event is registered only for the UserControl and not its child controls. One solution is to register the UC's MouseClick event handler with each if your child controls. This is easy enough to do in the designer: select a child control, view its events in the properties window and in its MouseClick event dropdown select the event you've already created for the UC. – Jay Riggs Sep 25 '12 at 23:24
  • If I have created the event in the Form, I don't see it in the UserControl. Is that right? – hshah Sep 26 '12 at 07:35
  • @hshad In that case you'd create an event in your UC that fires when any part of your UC raises the MouseClicked event. You'd still create the MouseClicked event handler in your UC like I described in my previous comment and in MouseClicked you'd fire your UC's event. Now when you create instances of your UC in your Form subscribe to your UC's custom event using code like this: `uc.MyUCMouseClick += UC_MyClickEvent;` If I'm not making myself clear here I can update my answer with more code. – Jay Riggs Sep 26 '12 at 19:33
  • If you could comment on this question please it would be great: http://stackoverflow.com/questions/12608300/select-usercontrol-from-flowlayoutpanel/ – hshah Sep 26 '12 at 21:29