本文介绍了通过循环访问winform控件,以编程方式获取checkbox.checked值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的winforms程序,我有一个选项对话框,当它关闭时,我循环遍历所有的对话框控件名称(文本框,复选框等)及其值,并将它们存储在数据库中,从它在我的程序。如下所示,我可以从 Control 组轻松访问 Text 属性,但没有属性文本框的检查值。我需要将 c 转换为复选框吗?

For my winforms program, I have an Options dialog box, and when it closes, I cycle throught all the Dialog Box control names (textboxes, checkboxes, etc.) and their values and store them in a database so I can read from it in my program. As you can see below, I can easily access the Text property from the Control group, but there's no property to access the Checked value of the textbox. Do I need to convert c, in that instance, to a checkbox first?

conn.Open();
foreach (Control c in grp_InvOther.Controls)
{
    string query = "INSERT INTO tbl_AppOptions (CONTROLNAME, VALUE) VALUES (@control, @value)";
    command = new SQLiteCommand(query, conn);
    command.Parameters.Add(new SQLiteParameter("control",c.Name.ToString()));
    string controlVal = "";
    if (c.GetType() == typeof(TextBox))
        controlVal = c.Text;
    else if (c.GetType() == typeof(CheckBox))
        controlVal = c.Checked; ***no such property exists!!***

    command.Parameters.Add(new SQLiteParameter("value", controlVal));
    command.ExecuteNonQuery();
}
conn.Close();

如果我需要先转换 c

推荐答案

是的,您需要转换它:

else if (c.GetType() == typeof(CheckBox))
    controlVal = ((CheckBox)c).Checked.ToString();

您可以使检查更简单:

else if (c is CheckBox)
    controlVal = ((CheckBox)c).Checked.ToString();

这篇关于通过循环访问winform控件,以编程方式获取checkbox.checked值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:18