C#Windows窗体创建复选框 [英] C# Windows Forms Creating Checkboxes

查看:368
本文介绍了C#Windows窗体创建复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


大家好,



我需要动态创建四个复选框,例如checkbox1 =Acts,checkbox2 =Law,checkbox3 =Houses,checkbox4 =Cars。





这是一个代码我写的第一个显示Checkbox,我需要同时加载四个复选框。我怎么能这样做?

CheckBox cb = new CheckBox();

cb.Name =cbActs;

cb.Text =使徒行传;

Controls.Add(cb);



我是C#的新手。任何帮助将不胜感激。如果有人可以向我提供代码或向我解释如何做到这一点,这将有很大的帮助。



提前谢谢你

Hi All,

I need to create four checkboxes dynamically e.g checkbox1 ="Acts",checkbox2 = "Law",checkbox3="Houses",checkbox4="Cars".


This is a code I wrote to display first Checkbox,I need to load four checkbox same time.How can I do that?
CheckBox cb = new CheckBox();
cb.Name = "cbActs";
cb.Text = "Acts";
Controls.Add(cb);

I'm new to C#. Any help will be appreciated. If someone can provide me with the code or explain to me how I can do it,this will be of great help.

Thank you in advance

推荐答案

嗨5Sazi,



看看下面的可运行示例:(您可以创建一个新的WindowsForms项目并替换我的代码默认的Program.cs文件的内容)



Hi 5Sazi,

Have a look at the following runnable example: (you can create a new WindowsForms Project and replace the default Program.cs file's content with my code)

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







代码显示a模式如何在运行时创建CheckBoxes(或其他控件),因为您可以轻松地将Main函数中包含的代码添加到按钮的Click处理程序,或者应用程序所需的任何内容。



但是正如我在评论中所写的那样,你总是要考虑布局和处理事件(看看笔记)。

因此,CreateCheckBox函数应该只是让您了解实现的外观。但是,如果调用者想要设置特定属性或处理其他事件,那么返回创建的Checkbox是一个好主意。



我希望这会有所帮助,可以随意询问如果您还有其他问题。



亲切问候Johannes




The code shows a pattern how to create CheckBoxes (or other Controls) during runtime, because you can easily add the code contained in the Main function to a Click handler of a button, or whatever is needed by your app.

But as I also have written in the comments, you always have to think about layouting and handling of events too (look at the notes).
So the CreateCheckBox function should just give an idea of how your implementation could look. But it's for shure a good idea to return the created Checkbox if the caller want's to set specific properties or handle other events.

I hope this helps, fell free to ask if you have any further questions.

Kind regards Johannes


这篇关于C#Windows窗体创建复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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