带有用户控件的流布局面板-如何获取用户控件值? [英] Flow layout panel with user controls - how to get at user control values?

查看:90
本文介绍了带有用户控件的流布局面板-如何获取用户控件值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我希望我不会过分陷入困境-希望有人可以指出正确的方向...我有一个流程布局面板,可以在其中放置许多用户控件实例.该控件(目前)仅具有一个复选框和几个文本字段.

用户控件表单中的代码如下所示:

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
});


...并且在流布局面板中填充了我的用户控件的新实例,并在其中设置了我设置的值.

我的问题是我找不到合适的语法来使用"get"例程从用户控件的每个实例返回值.我可以遍历流控制面板中的控件.例如....


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

    }
}



但是,如果我这样做,看来我所能做的就是在用户控件中获取控件的集合-而不是它们的值.

我希望我只是想念一些明显的东西....如果是的话,对不起打扰您了...

非常感谢,

Aero



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

推荐答案

问题是您想要在遍历控件时获取不同属性的不同值吗?

如果是这样,则可以将每个控件(代码中的c)转换为实际控件,然后读取属性,或者可以使用例如反射来获取属性,然后获取其值.例如,请参见 Type.GetProperties方法 [
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 [^]


对不起,我还是Mika "(我很胖(感谢您格式化我的代码-我现在已经解决了;-))-您的假设是正确的.我要做的是解析用户控件的所有实例,并根据其内容的值或状态(对于复选框而言)采取各种措施.这可能包括更改用户控件中的显示值.

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.

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();
    }
}



如果这样做,checkprop将为用户控件中的每个控件填充一整堆属性...作为替代......



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");
...



执行相同的操作,但仅针对当前我感兴趣的控件-这是一个CheckBox控件,因此我希望能够读取或写入CheckedState属性.但是我仍然看不到在返回的属性列表中可以访问该属性的位置.

再次感谢.

航空.


找出控件由Mika先前提出的建议....这对我有用....



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();
        }
    }
}




感谢所有人.

问候,

Aero




Thanks to all.

Regards,

Aero


如果要获取复选框状态,请捕获对应事件!

如何:使用代码添加事件处理程序 [
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



有关更多信息,请访问:
codeproject .com [



More at: codeproject.com[^]


这篇关于带有用户控件的流布局面板-如何获取用户控件值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆