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

  • Home
  • SEARCH
  • 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 8626771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:06:48+00:00 2026-06-12T08:06:48+00:00

I have 12 buttons in my Form1, and each button has a textbox next

  • 0

I have 12 buttons in my Form1, and each button has a textbox next to it. The button event calls a method called dialogueOpen which handles getting the an object from form2 and placing a string value in a textbox.

How can I place the value returned in a textbox depending on what button the user clicked on? So if it is button1 a user clicked on, then the text returned should be placed in textbox1 and if it is button2 the user clicked on then the text returned should be placed in textbox2. The point is avoid using a string name to check as the buttons can all be called “browse”.

Right now my code below does that but it is quite repetitive is there is a better of doing this?

    private void dailogueOpen(String btnName)
    {
        if (listBox1.SelectedItem == null)
        {
            MessageBox.Show("Please Select a form");
        }
        else
        {
            var selectedItem = (FormItems)listBox1.SelectedItem;
            var form2result = new Form2(myDataSet, selectedItem);
            var resulOfForm2 = form2result.ShowDialog();

            if (resulOfForm2 == DialogResult.OK)
            {
                switch (btnName)
                {
                    case "btn1":
                        textBox1.Text = form2result.getValue();
                        break;
                    case "btn2":
                        textBox2.Text = form2result.getValue();
                        break;
                    case "btn3":
                        textBox3.Text = form2result.getValue();
                        break;
                    case "btn4":
                        textBox4.Text = form2result.getValue();
                        break;
                    case "btn5":
                        textBox5.Text = form2result.getValue();
                        break;
                }
            }
        }
    }


    private void button1_Click(object sender, EventArgs e)
    {
        String name = "btn1";
        dailogueOpen(name);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        String name = "btn2";
        dailogueOpen(name);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        String name = "btn3";
        dailogueOpen(name);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        String name = "btn4";
        dailogueOpen(name);
    }

    private void button5_Click(object sender, EventArgs e)
    {
        String name = "btn5";
        dailogueOpen(name);
    }
  • 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-06-12T08:06:50+00:00Added an answer on June 12, 2026 at 8:06 am

    EDIT: I just noticed your event handlers. More refactoring ensues:

    Yes, there is. You need to somehow associate textboxes to buttons. For example, create a dictionary like so:

    Dictionary<Button, TextBox> _dict;
    
    _dict[button1] = textBox1;
    _dict[button2] = textBox2; 
    ...
    

    Use one event handler for all events:

    private void button_click(object sender, EventArgs e)
    {
        dialogeOpen((Button)sender);
    }
    

    Change dialogueOpen to accept a Button instead of a string and

     _dict[btn].Text = form2Result.getValue();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that has 90 buttons on it, one button for each
I have a bunch of windows forms. Each form has Back and Next buttons
I have 3 GUI forms: NewUser.java ExistingUser.java ForgotLogin.java Each of these has a button
I have a form that has some textboxes (45) with CheckBoxes next to each,
I have a windows form application which has a text box and 3 buttons.
I have five buttons on one form that when clicked enables a bool each
I have a form with a series of questions, radio buttons. Each group is
I have two buttons for my form - a Save changes button and a
I have many buttons and labels on my c# form. I have a button
I have a form with 3 textboxes and 1 button. textbox1 has tab index

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.