按名称引用复选框

时间:2014-01-18 17:28:20

标签: c# winforms

如何在C#Windows窗体中按名称(字符串)引用复选框?我正在尝试

CheckBox cb = (CheckBox)Controls["checkBox" + id];
if (cb.Checked)
{
    MessageBox.Show(id);
}

我正在

Error   2   Cannot convert type 'System.Windows.Forms.Control' to 'Vts_SI.CheckBox' C:\Users\Administrator\Documents\Visual Studio 2010\Projects\My2\My2\GlavniProzor.cs    67  27  Vts_SI
Error   3   'Vts_SI.CheckBox' does not contain a definition for 'Checked' and no extension method 'Checked' accepting a first argument of type 'Vts_SI.CheckBox' could be found (are you missing a using directive or an assembly reference?)   C:\Users\Administrator\Documents\Visual Studio 2010\Projects\My2\My2\GlavniProzor.cs    68  20  Vts_SI

4 个答案:

答案 0 :(得分:4)

您可能有一个名为CheckBox的类,您应明确指定名称空间

System.Windows.Forms.CheckBox cb
    = (System.Windows.Forms.CheckBox)Controls["checkBox" + id];

答案 1 :(得分:1)

使用this

public T Control<T>(String id) where T : Control
{
   foreach (Control ctrl in MainForm.Controls.Find(id, true))
   {
      return (T)ctrl; // Form Controls have unique names, so no more iterations needed
   }
   throw new Exception("Control not found!");
}

答案 2 :(得分:0)

Controls.Add被(dynamicCheckBox);

为什么不尝试使用动态复选框。

system.Windows.Forms.dynamiccheckBox cb1 =(dynamiccheckBox)Controls [“dynamiccheckBox”+ id];

答案 3 :(得分:-3)

键入:System.Windows.Forms.CheckState CheckState枚举值之一。默认值为“未选中”。 尝试这个但有点长

using System;
using System.Windows.Forms;

namespace DynamicCheckBoxes
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            // Create example Form (normally you would create it with the VS Forms Designer)
            Form form = new Form();

        // Use a Panel to add the CheckBoxes during runtime
        Panel panel = new Panel();
        panel.Dock = DockStyle.Fill;
        panel.Parent = form;

        // Add a button to the form for creating the CheckBoxes during runtime
        Button button = new Button();
        button.Text = "Add a new CheckBox";
        button.Dock = DockStyle.Bottom;
        button.Parent = form;

        // On Button.Click we add a new CheckBox - we use a inline delegate here
        // Remark: Notice how the "outer" scope variables are captured.
        int iCount = 0;
        button.Click += delegate(object sender, EventArgs e)
        {
            iCount++;
            CreateCheckbox("cbDynamic" + iCount, "Dynamic" + iCount, panel, SampleCheckChangedHandler);
        }; 

        // Now let's create some Checkboxes in code 
        // I created a helper function to do the job.
        CreateCheckbox("cbActs", "Acts", panel, SampleCheckChangedHandler);
        CreateCheckbox("cbLaw", "Law", panel, SampleCheckChangedHandler);
        CreateCheckbox("cbHouses", "Houses", panel, SampleCheckChangedHandler);
        CreateCheckbox("cbCar", "Car", panel, SampleCheckChangedHandler);           

        Application.Run(form);
    }

    /// <summary>
    /// Helper function to create a CheckBox with common properties and adding it to the given parent control.
    /// </summary>
    /// <param name="Name"> Name for the new CheckBox </param>
    /// <param name="Text"> Text of the new CheckBox </param>
    /// <param name="ctrlParent"> Parent control for the new CheckBox (can be null if parent is set through other logic later) </param>
    /// <param name="handlerCheckChanged"> Handler for the CheckChanged event (can be null if no handler is needed) </param>
    /// <returns> The new Checkbox </returns>
    static CheckBox CreateCheckbox(string strName, string strText, Control ctrlParent, EventHandler handlerCheckChanged)
    {
        CheckBox cb = new CheckBox();
        cb.Name = strName;
        cb.Text = strText;
        cb.Dock = DockStyle.Top; // give a thought on how to do the dynamic layout,
        // maybe use a container control..
        cb.Parent = ctrlParent; // same as ctrlParent.Controls.Add(cb)
        // add some EventHandler (use this code as template if other handlers are commonly needded,
        // but add/set handlers/properties for specific CheckBoxes created with this function to the 
        // returned CheckBox object
        cb.CheckedChanged += handlerCheckChanged;

        return cb;
    }

    /// <summary>
    /// Sample eventhandler for the CheckChanged event
    /// </summary>
    static void SampleCheckChangedHandler(object objSender, EventArgs ea)
    {
        CheckBox cb = objSender as CheckBox; // get a reference to the checked/unchecked CheckBox

        // Do something just for demo
        if (cb.Checked)
            MessageBox.Show(cb.Text + " checked");
        else
            MessageBox.Show(cb.Text + " unchecked");
    }
}

}