Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
C#
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...

C#
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....

C#
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
Updated 12-May-12 7:35am
v2

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 [^]
 
Share this answer
 
Sorry Mika, I still think I'm being thick (and thanks for formatting my code - I worked out how to do that now ;-)) - 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.

C#
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...
C#
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....

C#
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
 
Share this answer
 
v2
If you want to get state of checkboxes, catch the correspond event!

How to: Add an Event Handler Using Code[^]

Example (VB.NET):
VB
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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900