Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# User-controls
Dear all,
 
I hope I'm not about to poperly fall at the final fence - hopefully someone can point me in the right direction...I have a flow layout panel into which I'm putting many instances of a user control. This control (for now) just has a check box and a few text fields only.
 
The code in the user control form looks like:
public partial class myfrmList : UserControl
{
    public int Index
    {
        get { return int.Parse(lb_Ref.Text); }
        set { lb_Ref.Text = value.ToString(); }
    }
    public CheckState Displayed
    {
        get { return cb_show.CheckState; }
        set { cb_show.CheckState = value; }
    }
...etc you get the idea...
}
In the code for my main form, when I want to put a new instance of the user control into the flow layout panel, I can do this with...
 
myflowlayoutpanel.Controls.Add(new myfrmList
{
    Displayed = CheckBoxState,
    Index = Indexval
});
...and the flow layout panel is populated with a new instance of my user control with the values I've set in it.
 
My problem is I can't find the right syntax to be able to use the 'get' routines to return values from each instance of the user control. I can iterate through the controls inside the flow control panel..e.g....
 
for (ctr = 0; ctr < myflowlayoutpanel.Controls.Count; ctr++)
{
    foreach (Control c in myflowlayoutpanel.Controls[ctr].Controls)
    {
        c....
 
    }
}
 
but if I do that it looks like all I can do is get the collection of controls in the user control - not their values.
 
I hope I'm just missing something obvious....if so I'm sorry for bothering you...
 
Many Thanks,
 
Aero
Posted 12-May-12 8:33am
Aero72618
Edited 12-May-12 8:35am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Is the question that you want to get different values of different properties when you loop through controls?
 
If that's true, then you can cast each control (c in your code) to the actual control and then read the properties or you can use for example reflection to get the properties and then to get their values. For example, see Type.GetProperties Method [^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Sorry Mika, I still think I'm being thick (and thanks for formatting my code - I worked out how to do that now Wink | ;-) ) - your assumption is correct. What I want to do is to parse through all the instances of my user controls and take various actions depending upon the values or state (in the case of checkboxes) of their contents. This may include altering the displayed values in the user controls.
 
            int ctr;
            PropertyInfo[] checkprop;
 
            for (ctr = 0; ctr < myflowlayoutpanel.Controls.Count; ctr++)
            {
                foreach (Control c in flpselect.Controls[ctr].Controls)
                {                  
                    Type mytype = typeof(myfrmlist);
                    checkprop = mytype.GetProperties();
                }
            }
 
If I do this, checkprop gets filled with a whole stack of properties for each control in the user control...as an alternative...
PropertyInfo checkprop;
...
checkprop = mytype.GetProperty("Displayed");
...
 
Does the same but only for the control I'm interested in at the moment - this is a CheckBox control, so I wish to be able to read or write the CheckedState property. But I still can't see where in the list of properties that is returned where I can access that property.
 
Thanks again.
 
Aero.
 

Figured out what was suggested earlier by Mika by casting the control....here's what worked for me....
 
 
            foreach (Control ctrl in myflowlayoutpanel.Controls)
            {
                myfrmlist c = ctrl as myfrmlist;
                if (c != null)
                {
                    if (c.Displayed == CheckState.Checked)
                    {
                           somestuff();
                    }
                    else
                    {
                        someotherstuff();                    
                    }
                }
            }
 

Thanks to all.
 
Regards,
 
Aero
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If you want to get state of checkboxes, catch the correspond event!
 
How to: Add an Event Handler Using Code[^]
 
Example (VB.NET):
    Private WithEvents btn As Button
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mybtn As Button
        Dim iCount As Integer = 0, i As Integer = 0
 
        Try
            iCount = Integer.Parse(InputBox("Buttons to add", "List (Of Button) wizard...", "3"))
            For i = 1 To iCount
                mybtn = New Button
                With mybtn
                    .Name = "Button" & i.ToString
                    .Left = 8
                    .Top = 32 * i
                    .Width = 72
                    .Height = 24
                    AddHandler mybtn.Click, AddressOf MyButton_Click
                    .Parent = Me
                End With
            Next
 
        Catch ex As Exception
           MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
        End Try
 
    End Sub
 
    Private Sub MyButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        MsgBox("You have clicked: " & sender.Name, MsgBoxStyle.Information, "MyButton_Click event...")
    End Sub
 
More at: codeproject.com[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 13 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100