Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6670327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:15:18+00:00 2026-05-26T03:15:18+00:00

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

  • 0

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

how 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:15:19+00:00Added an answer on May 26, 2026 at 3:15 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an ASP.NET/C# app. I have 2 text boxes and I need
This is a continuation of the question here: JBoss - does app have to
On my rails app I have a list of items (like a task list)
Hobbyist Cocoa programmer here. Have been looking around all the usual places, but this
I would like to have a textbox in a Silverlight app where the user
I'll start off by saying I'm a noob with all of this, I have
I have a search screen in a Visual Basic .Net App that has text
Its a simple config app with 4 checkboxes and 5 textboxes, and all values
I have a forms app and when it starts up I would like it
hi I have a C# app that features a canvas. I'd like to programmatically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.