1

I am trying to implement a button that deletes a text in a textbox. However, the catch here is that I will have multiple textbox that have multiple button that deletes the text respectively. I've tried a weird approach to this by having a switch case that gets the button name to deal with different textboxsuch as follows:

switch(btn.Name)
{
    case "button1": textbox1.Clear();
        break;
    case "button2": textbox2.Clear();
        break;
}

Although this solves the issue, but I find this really not robust and not practical in terms of the design. So I would like to ask if there is anyway to do this in a much better way? Thank you!

AGB
  • 2,230
  • 1
  • 14
  • 21
Pow4Pow5
  • 501
  • 2
  • 8
  • 22
  • you can save all your control name and find them later. see this post http://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls – Dr. Stitch May 13 '16 at 02:21
  • Hey Dr. Stitch, thank you for answering, but I am trying to look for maybe a better design such as a way to bind the button to the textbox so that I don't need to have a switch case like this. – Pow4Pow5 May 13 '16 at 02:27
  • Controls have a property called Tag of type Object. You could set the Tag property of each Button to the TextBox it is supposed to clear – matt.dolfin May 13 '16 at 02:59
  • Is your logic always a 1:1 = 1 `Button` for 1 `TextBox`? Will a `Button` ever have to clear a different `TextBox`? – Mikanikal May 13 '16 at 03:06
  • if it is a one to one ie Button1 = textbox1 then you can use the controls collection: ((TextBox)Controls["textbox" + btn.Name.Replace("button", "")]).Clear() – Sorceri May 13 '16 at 03:15
  • How your design look like? if you have both button and textbox pair inside a container control then you can find that textbox relative to the clicked button. – Bharadwaj May 13 '16 at 04:39

3 Answers3

0

If your logic is a 1:1; You could just use the button click event for each of your buttons and put the specific textbox you want to clear. If your logic is not 1:1; please be more specific in your question.

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Clear();
}
Mikanikal
  • 1,082
  • 8
  • 12
0

You can extend Button class like this

public partial class ButtonHasList : Button
{
    public List<Textbox> List { get; set ;  }
    public  ButtonHasList() 
    {
         List = new     List<Textbox> ();
         this.Click += CleanTextOfList;
    }
    private void  CleanTextOfList ( object sender,EventArgs e)
   {
          foreach ( var item in List)
          {
                   item.Text = "";
           }

    }  
}

Add Textboxs that you want to clear their text to ListOfTextbox and text of them will be clean

mohsen
  • 1,763
  • 3
  • 17
  • 55
0

it really should be as simple as extracting the number part if it is a one to one using the controls collection. Then you can assign all buttons to the click event without the need for x amount of event handlers. Once Click event handler for all buttons....

private void allButtons_Click(object sender, EventArgs e)
{
    try
    {
        Button btn = sender as Button;
        ((TextBox)Controls["textbox" + btn.Name.Replace("button", "")]).Clear();
    }
    catch (Exception ex)
    {
        //do something with the error
    }
}
Sorceri
  • 7,870
  • 1
  • 29
  • 38