2012-09-19 19 views
1

我在Form1中有12個按鈕,每個按鈕旁邊都有一個文本框。按鈕事件調用一個名爲dialogOpen的方法,該方法處理從form2獲取對象並將字符串值放入文本框中。我該如何重構下面的C#代碼?

如何根據用戶點擊的按鈕來放置文本框中返回的值?因此,如果是button1用戶點擊,則返回的文本應放置在textbox1中,如果是button2,則用戶單擊,然後返回的文本應置於textbox2中。重點是避免使用字符串名稱來檢查按鈕可以被稱爲「瀏覽」。

現在我的代碼下面這樣做,但它是相當重複的是有更好的做到這一點?

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); 
    } 

回答

2

編輯:我只注意到你的事件處理程序。隨後出現了更多的重構:

是的,有。你需要以某種方式將文本框關聯到按鈕。例如,創建像這樣一本字典:

Dictionary<Button, TextBox> _dict; 

_dict[button1] = textBox1; 
_dict[button2] = textBox2; 
... 

使用所有事件的一個事件處理程序:

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

變化dialogueOpen接受按鈕而不是一個字符串和

_dict[btn].Text = form2Result.getValue(); 
+0

這將工作,如果所有按鈕的文本說「瀏覽」的例子。會否說明[button1]仍然有效? –

+0

是的,請注意我指的是C#中按鈕的名稱,而不是它的文本。 – zmbq

+0

+1是迄今爲止最簡潔的答案(代碼方面),而沒有做任何奇怪的事情。 –

1

1,您對所有button

Nota (Thank's to Marty) :使用same delegate當你在表單設計器,選擇所有按鈕,然後assing然後「Generic_Click」爲所有的人,也可以使用代碼下面。

this.btn1.Click += new System.EventHandler(Generic_Click); //the same delegate 
this.btn2.Click += new System.EventHandler(Generic_Click); 
this.btn3.Click += new System.EventHandler(Generic_Click); 
.... 


private void Generic_Click(object sender, EventArgs e) 
{ 
    var control = (Button)sender; 
    if( control.Name == "btn1") 
    { 
     .... 
    } 
    else if( control.Name == "btn2") 
    { 
     .... 
    } 
    else if( control.Name == "btn3") 
    { 
     .... 
    } 


} 
+0

當您在窗體設計器中時,選擇所有按鈕,然後爲所有按鈕分配「Generic_Click」。 – Marty

+0

感謝Marty爲你補充,你很好 –

+0

可能更適合做一個演員,而不是在每一個if語句中施放。 –

1

取代你的事件處理器來

private void ButtonClick(object sender, EventArgs e) 
{ 
    var button = sender as Button; 
    if (button == null) return; 
    String name = button.Text;// Tag, name etc 
    dailogueOpen(name); 
} 
+0

這意味着生病仍然必須檢查什麼名字傳遞到dailogueOpen,如果所有的按鈕被命名爲「瀏覽」? –

+0

我不知道你的應用程序的具體情況,因此寫在評論替代prorypties –

0

你可以在所有按鈕點擊時具有詞典和一種事件方法

Dictionary<Button, TextBox> dx = new Dictionary<Button, TextBox>; 

private void ButtonClick(object sender, EventArgs e) 
{ 
    var button = sender as Button; 
    if (button == null) return; 
    dx[button].Text = form2result.getValue(); 
} 

和構造是這樣的:

public ClassName() 
{ 
    dx.Add(button1, textBox1); 
    dx.Add(button2, textBox2); 
    dx.Add(button3, textBox3); 
} 
+0

這個工作,如果所有的按鈕的文字說「瀏覽」的例子。 DX會繼續工作嗎? –

+0

是的,因爲按鈕是具有不同哈希碼的不同對象 –

0

我覺得你可以做的第一件事就是通過去除switch語句需要提高可讀性:

private void dailogueOpen(TextBox textBox) 
{ 
    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) 
     { 
      textBox.Text = form2result.getValue(); 
     } 
    } 
} 


private void button1_Click(object sender, EventArgs e) 
{ 
    dailogueOpen(textBox1); 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    dailogueOpen(textBox2); 
} 

private void button3_Click(object sender, EventArgs e) 
{ 
    dailogueOpen(textBox3); 
} 

private void button4_Click(object sender, EventArgs e) 
{ 
    dailogueOpen(textBox4); 
} 

private void button5_Click(object sender, EventArgs e) 
{ 
    dailogueOpen(textBox5); 
} 

這就給你一個合理的方法簽名引入字典(由另外兩個人建議)將Button映射到TextBox,從而允許您爲所有按鈕使用單個事件處理程序(由另外兩個人建議)。

0
private void button_Click(object sender, EventArgs e) 
     { 

      Button button = sender as Button; 
      if (button == null) return; 
      String name = button.Text;// Tag, name etc 


      dailogueOpen(name); 
     } 


    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) 
       { 
        SetTxt(btnName,form2result.getValue()); 
       } 
      } 
    } 



     private void SetTxt(string btnName, string value) 
      { 
       int lenght = "Button".Length; 
       string index = btnName.Substring(lenght); //remove Button 
       TextBox t = (TextBox)this.Controls.Find("textBox" + index, true)[0]; 

       if (t != null) 
        t.Text = value; 
      } 
1

我先只使用一個事件處理程序的按鈕,它應該是這樣的:

protected void ButtonClick(object sender, EventArgs e) 
{ 
    Button clickedButton = (Button) sender; 
    string selectedId = clickedButton.ID; 
    string[] idParameters = selectedId.Split('_'); 
    string textBoxId = "textbox" + idParameters[1]; 
    dailogueOpen(textBoxId); 
} 

我做什麼這裏是使用文本框的名稱的模式,因此例如如果你有按鈕,如:button_1,button_2,...,button_n,你可以推斷出相應的文本框是什麼。

如果你點擊button_1,通過分割它的id你就會知道它對應的文本框是id爲textbox1的文本框。

然後dialogueOpen功能應該是這樣的:

private void dailogueOpen(string textBoxId) 
{ 
    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) 
     { 
      TextBox textBox = (TextBox)this.Form.FindControl("MainContent").FindControl(textBoxId); 
      textBox.Text = resulOfForm2.getValue(); 
    } 
} 

在哪裏搜索Maincontent是容器的id其中的文本輸入框是。

總而言之:

  • 我會用按鈕和texboxes ID的模式。
  • 根據被點擊的按鈕,我推斷其對應的texbox id。
  • 然後找到texbox並更新它的值。