Home > OS >  How to link textbox and checkbox in C#
How to link textbox and checkbox in C#

Time:11-21

I started making a small program. The form contains checkbox1,2,3,4,.... and textbox1,2,3,4,5.... there is a code that looks at which of the checkboxes are marked. If there is any possibility to link textbox and checkbox. So that when a code marked with a checkbox is detected, the text is taken from the textbox given to it and transferred to the RichTextBox, using AppendText. Below is a sample code with a cyclic check of all the checkboxes on the form for the presence of checked on my form.

foreach (Control control in this.tabControl1.TabPages[0].Controls) //цикл по форме с вкладками
            {
                if (control as CheckBox != null) // проверка на пустое значение
                {
                    if (control.Visible == true)// проверка на видимость
                    {
                        if ((control as CheckBox).Checked)// проверка на чек
                        {
                            
                        }
                        else if ((control as CheckBox).Checked == false)
                        {
                            
                        }
                    }
                }

CodePudding user response:

Use the following method to get CheckBox controls.

public static class ControlExtensions
{
    public static IEnumerable<T> Descendants<T>(this Control control) where T : class
    {
        foreach (Control child in control.Controls)
        {
            if (child is T thisControl)
            {
                yield return (T)thisControl;
            }

            if (child.HasChildren)
            {
                foreach (T descendant in Descendants<T>(child))
                {
                    yield return descendant;
                }
            }
        }
    }
}

In the form, use a Dictionary to pair CheckBox to TextBox. You can also check for visibility in the Where.

public partial class Form1 : Form
{
    private readonly Dictionary<string, TextBox> _dictionary = 
        new Dictionary<string, TextBox>();
    public Form1()
    {
        InitializeComponent();

        _dictionary.Add("checkBox1", textBox1);
        _dictionary.Add("checkBox2", textBox2);
        _dictionary.Add("checkBox3", textBox3);
        _dictionary.Add("checkBox4", textBox4);
        _dictionary.Add("checkBox5", textBox5);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        var list = tabControl1.Descendants<CheckBox>().ToList();
        var @checked = list.Where(x => x.Checked).ToList();
        var notChecked = list.Where(x => !x.Checked).ToList();

        foreach (var checkBox in @checked)
        {
            TextBox textBox = _dictionary[checkBox.Name];
        }
    }
}

CodePudding user response:

Create a UserControl with CheckBox and TextBox components. Create properties Checked and TextForAdd:

namespace Sort.UserPanel
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        public bool Checked { get { return checkBox1.Checked; } }
        public string TextForAdd { get { return textBox1.Text; } }
    }
}

On the main form we add UserControl1 the necessary number of times.

private void testCheckBoxes(object obj)
{
    if (obj is UserControl1 control)
    {
        
                string text = control.TextForAdd;
                // .....
            
        
    }
}

private void button1_Click(object sender, EventArgs e)
{
    foreach (Control control in this.LeftPanel.Controls)
    {
         if (control as UserControl1 != null)
            {
                if (control.Visible == true )
                {
                    testCheckBoxes(control);

                }
            
            }
    }
}
  •  Tags:  
  • c#
  • Related