开发者

How can empty all textboxes by click when 1 check box is checked in C#

开发者 https://www.devze.com 2023-04-12 17:23 出处:网络
In my app I have a 85 textboxes. All textboxes in my app like this : void textBox1s(string input) { this.textBox1.ForeColor = Color.Black;

In my app I have a 85 textboxes. All textboxes in my app like this :

void textBox1s(string input)
{
    this.textBox1.ForeColor = Color.Black;
    this.textBox1.Text = input;
}

private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
    textBox1s(Clipboard.GetText());
}

and now I want any textbox when checkbox is checked by Click/MouseClick on textbox1 >>>only empty textbox1.text if click on textbox2 >>> only empty textbox2.text and .....

and in my app i have tabControl1 and 18 groupbox

h开发者_运维问答ow can set all textbox when checkbox is checked for empty by Click/MouseClick on textbox ?

if my answer is use user control please give me 1 sample code

Thanks.


Not exackly sure what do you mean but if you want to iterate through the collection of textbox you could do this like this:

    foreach (Control cntrl in this.Controls)
    {
        if (cntrl is TextBox)
        {
            cntrl.Text = "lold";
        }
    }

where "this" is container like form


To reset the Text this portion of code below will reset any text box which is clicked :

first declare the event Delegate

 private void AllTextBoxes_MouseClick(object sender, MouseEventArgs e)
        {
            if(sender is TextBox)
                ((TextBox)(sender)).Text = "";
        }

than assign this to each Textbox, you can use VisualStudio Property Box to do that by selecting all Textboxes and assign that or do it manualy foreach one :

this.textBox2.MouseClick += new System.Windows.Forms.MouseEventHandler(this.AllTextBoxes_MouseClick);

or on FormLoad_Event you can do this

 foreach (Control ctrl in this.Controls)
            {
                if (ctrl is TextBox)
                {
                    ctrl.MouseClick += new System.Windows.Forms.MouseEventHandler(this.AllTextBoxes_MouseClick);
                }
            }

and you can reset the TextBox text within a GroupBox like this

foreach (Control ctrl in groupBox1.Controls)
            {
                if (ctrl is TextBox)
                {
                    MessageBox.Show(ctrl.Name);
                }
            }

for all GroupBoxes you can:

foreach (Control control in this.Controls)

    {
        if(control is GroupBox)
        {
        foreach (Control ctrl in groupBox1.Controls)
                    {
                        if (ctrl is TextBox)
                        {
                            MessageBox.Show(ctrl.Name);
                        }
                    }
        }
    {


neurotix was heading down the right track, but that won't catch your textboxes in child containers, nor will it handle calling the right function, to set the right textbox's properties. best way to handle it without an user control is to use a recursive function to assign your event handler.

public void SetTextBoxClickHandler(Control control)
{
    foreach (Control childControl in control.Controls)
    {
        if (childControl is TextBox)
        {
            childControl.Click += this.MyClickHandler;
            continue;
        }
        if (item.Controls == null)
            continue;
        SetTextBoxClickHandler(childControl);
    }
}

private void MyClickHandler(object sender, MouseEventArgs e)
{
    typeof(Form).InvokeMember(string.Format("{0}s", ((Control)sender).Name), BindingFlags.InvokeMethod | BindingFlags.NonPublic, null, null, new object[] { Clipboard.GetText() });
}

private void Form1_Load(object sender, EventArgs e)
{
    SetTextBoxClickHandler(this.Controls);
}

If you use an user control, that's really easy, just inherit from TextBox, and set everything to use the new control.

public class ClickableTextBox : TextBox
{
    public ClickableTextBox()
    {
        this.Click += MyClickHandler;
    }
    void setText(string input)
    {
        this.ForeColor = Color.Black;
        this.Text = input;
    }

    private void MyClickHandler(object sender, MouseEventArgs e)
    {
        setText(Clipboard.GetText());
    }
}

I recommend creating the customer user control for the following reasons:

  1. Avoid the performance hit of using reflection to invoke a method dynamically based on its name
  2. Avoid the performance hit of converting the object sender to a Control on every click
  3. Avoid the performance hit during the initialization loop of inspecting every control, and child control on the form, regardless if it's a TextBox or not
  4. Less code to maintain
  5. Higher reusability, the form code, would have to be present on every form, if you need to reuse this on a different project, or different form, it'd be a lot easier to reuse the custom user control rather than copying/pasting code to every form that might need to change

Of course, if all you need to do, is set the text, and nothing else in that function, you could simplify it a bit...

public void SetTextBoxClickHandler(Control control)
{
    foreach (Control childControl in control.Controls)
    {
        if (childControl is TextBox)
        {
            childControl.Click += this.MyClickHandler;
            continue;
        }
        if (item.Controls == null)
            continue;
        SetTextBoxClickHandler(childControl);
    }
}

private void MyClickHandler(object sender, MouseEventArgs e)
{
    TextBox textBox = sender as Textbox;
    if (textBox == null)
        return;
    textBox.ForeColor = Color.Black;
    textBox.Text = Clipboard.GetText();
}

private void Form1_Load(object sender, EventArgs e)
{
    SetTextBoxClickHandler(this.Controls);
}

or using the user control:

public class ClickableTextBox : TextBox
{
    public ClickableTextBox()
    {
        this.Click += MyClickHandler;
    }

    private void MyClickHandler(object sender, MouseEventArgs e)
    {
        this.ForeColor = Color.Black;
        this.Text = input;
    }
}

For many of the above reasons, I'd still recommend the custom user control.


From what I can ascertain, you want a checkbox to act as a toggle - when it's checked, delete the contents of a textbox, when it's unchecked, copy the clipboard into it.

The code for that would be:

void textBox_MouseClick(object sender, MouseEventArgs e)
{
    textBox tb = (TextBox)sender;
    if (checkBox1.IsChecked)
    {
        tb.Text = "";
    }
    else
    {
        tb.Text = Clipboard.GetText();
    }
}

Then you can just make all of the Click() events on the textboxes point to this one method - hooray for code re-use :D

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号