c# winform groupbox动态添加控件(个人向。不喜勿喷)

/// 
/// 在groupbox里动态添加控件
/// 
public class Add_Control
{
    //int _led_height = 79;//LED控件的高
    //int _led_width = 130;//LED控件的长
    int row_padding = 0;//行间隔
    int Column_padding = 10;//列间隔
    int row_count = 10;//行数
    int Column_count = 3;//列数
    int left_All_Pad = 10;//左边距
    int right_All_Pad = 0;//右边距
    int top_All_Pad = 10;//上边距
    int info_Count = 20;//信号总数
    Color Border_Color = Color.DarkGray;//边框颜色
    Color LED_ForeColor = Color.Red;//字体颜色
    GroupBox Recent_groupBox = new GroupBox();
    /// 
    /// 动态添加LED灯
    /// 
    /// 要添加数据的Groupbox
    /// 数据个数
    /// 行数
    /// 列数
    public void  Add_LED(GroupBox groupBox1,int info_count,int row_count, int Column_coun

 /// 
    /// 调用动态添加控件方法
    /// 
    public void Test_for_Add()
    {
        Add_Control add_Control = new Add_Control();
        add_Control.Add_LED(groupBox10, 4, 2, 2);
        add_Control.Add_LED(groupBox11, 4, 2, 2);
        add_Control.Add_LED(groupBox12, 4, 2, 2);
        add_Control.Add_LTL(groupBox13, 19, 2, 1, 16, 2, 9);
        add_Control.Add_LEDandTextbox(groupBox14, 9, 4, 5, 5, 2);
    }

绑定数据
if (Main.dbcFile.ListSig[i].LinkMessage == groupBox13.Text.Split(‘(‘)[0])//MCU_2
{
try
{
//从锁定的数据源中判别数据的类型(我这边因为需要区分数据是数值型还是文本型(文本也分两种一种是多文本枚举的用textbox,如果只有两个的用灯泡闪烁显示))
if (int.TryParse(Main.dbcFile.ListSig[i].ValueTable.ValueList.Count.ToString(), out emuncounter))
{//判断是否为双文本枚举
if (emuncounter < 3)
{
for (int x = 0; x < groupBox13.Controls.Count; x++)
{
if (groupBox13.Controls[x] is Round_AddLabel.Round_AddLabel)
{
if (groupBox13.Controls[x].Text == “”)
{
groupBox13.Controls[x].Text = Main.dbcFile.ListSig[i].Comment;

        #region 冒泡排序解决 Round_AddLabel下面的textbox太长遮挡下一行的灯泡的显示
        string contain_string = "";
        int bigger_counter = 0;
        List Contain_lampName = new List();
        for (int i = 0; i < groupBox13.Controls.Count; i++)
        {
            if (groupBox13.Controls[i] is Round_AddLabel.Round_AddLabel)
            {
                Contain_lampName.Add(groupBox13.Controls[i].Text);
            }
        }
        bool ok_Flight = true;
        while (ok_Flight)
        {
            bool changge_flight = false;
            for (int i = 0; i < Contain_lampName.Count - 1; i++)
            {
                if (Contain_lampName[i].Length > Contain_lampName[i + 1].Length)
                {
                    contain_string = Contain_lampName[i + 1];
                    Contain_lampName[i + 1] = Contain_lampName[i];
                    Contain_lampName[
数据更新

for (int i = 0; i < groupBox13.Controls.Count; i++)
{
if (groupBox13.Controls[i] is LedNumbe_AddLabel.LedNumbe_AddLabel)
{
if (comment == groupBox13.Controls[i].Text)
{
((LedNumbe_AddLabel.LedNumbe_AddLabel)groupBox13.Controls[i]).Newvalue = sig_val;
}
}
else if (groupBox13.Controls[i] is Round_AddLabel.Round_AddLabel)
{
if (comment == groupBox13.Controls[i].Text)
{
if (sig_val == “0”)
{
((Round_AddLabel.Round_AddLabel)groupBox13.Controls[i]).Show_Color = Color.LightGreen;
}
if (sig_val == “1”)
{
((Round_AddLabel.Round_AddLabel)groupBox13.Controls[i]).Show_Color = Color.Red;
}
}
}
else
{
if (comment == groupBox13.Controls[i].Text)
{
((Textbox_AddLable.Textbox_AddLable)groupBox13.Controls[i]).Newvalue = sig_inf;
}
}

你可能感兴趣的:(c# winform groupbox动态添加控件(个人向。不喜勿喷))