首页 新闻 会员 周边 捐助

怎么得到我所点击button的所在父控件

0
[已解决问题] 解决于 2013-03-11 17:53

自定义一个UserControl,在其他窗体实例化一个UserControl,这个UserControl上面有一个Button

我在调用这个UserControl的窗体上写,这个Button的单击事件,在这个事件里面怎么得到我实例化的这个UserControl?

侠梦千年的主页 侠梦千年 | 初学一级 | 园豆:6
提问于:2013-03-11 15:28
< >
分享
最佳答案
0

循环form控件,再循环其包含控件,接着递归获取其子控件……

奖励园豆:5
滴答的雨 | 老鸟四级 |园豆:3660 | 2013-03-11 16:35
其他回答(1)
0

public static void ClearCtrlValue(Control parContainer)
        {
            for (int index = 0; index < parContainer.Controls.Count; index++)
            {
                // 如果是容器类控件,递归调用自己
                if (parContainer.Controls[index].HasChildren)
                {
                    ClearCtrlValue(parContainer.Controls[index]);
                }
                else
                {
                    switch (parContainer.Controls[index].GetType().Name)
                    {
                        case "TextBox":
                            parContainer.Controls[index].Text = "";
                            break;
                        case "RadioButton":
                            ((RadioButton)(parContainer.Controls[index])).Checked = false;
                            break;
                        case "CheckBox":
                            ((CheckBox)(parContainer.Controls[index])).Checked = false;
                            break;
                        case "ComboBox":
                            try
                            {
                                ((ComboBox)(parContainer.Controls[index])).SelectedIndex = 0;
                            }
                            catch
                            {

                            }
                            break;
                        case "DateTimePicker":
                            ((DateTimePicker)(parContainer.Controls[index])).Text = "1900-1-1";
                            break;
                        case "DataGridView":
                            DataTable dt = (DataTable)(((DataGridView)(parContainer.Controls[index])).DataSource);
                            if (dt.Rows.Count > 0)
                            {
                                dt.Clear();
                                ((DataGridView)(parContainer.Controls[index])).DataSource = dt;
                            }
                            break;
                        case "CheckBoxComboBox":
                            parContainer.Controls[index].Text = "";
                            foreach (CheckBoxComboBoxItem Item in ((CheckBoxComboBox)(parContainer.Controls[index]))._CheckBoxComboBoxListControl.Items)
                            {
                                Item.Checked = false;
                            }
                            break;
                    }
                }
            }
        }

这个是清空页面上所有控件的值的函数,所有控件都能找到,那就不在乎你的父控件的问题了

茂茂 | 园豆:2894 (老鸟四级) | 2013-03-11 17:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册