簡體   English   中英

我如何重構下面的C#代碼?

[英]How can I refactor this C# code below?

我的Form1中有12個按鈕,每個按鈕旁邊都有一個文本框。 button事件調用一個名為dialogOpen的方法,該方法處理從form2獲取對象並將字符串值放置在文本框中的情況。

如何根據用戶單擊的按鈕將返回的值放在文本框中? 因此,如果是用戶單擊的是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);
    }

編輯:我只是注意到您的事件處理程序。 隨之而來的是更多的重構:

就在這里。 您需要以某種方式將文本框關聯到按鈕。 例如,創建一個像這樣的字典:

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

1您在所有button上使用same delegate

Nota (Thank's to Marty) :在表單設計器中時,選擇所有按鈕,然后為所有按鈕依次單擊“ 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")
     {
        ....
     }


}

替換您的事件處理程序以

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

我首先只對按鈕使用一個事件處理程序,如下所示:

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

我在這里所做的是使用一種模式來命名文本框,例如,如果您具有ID為:button_1,button_2,...,button_n的按鈕,則可以推斷出相應的文本框是什么。

如果單擊button_1,則通過拆分其ID將知道其對應的文本框是ID為textbox1的文本框。

然后,對話框打開函數將如下所示:

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並更新其值。

您可以在所有按鈕單擊上使用字典和一種事件方法

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

我認為您可以做的第一件事是通過消除對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(由其他兩個人建議)針對所有按鈕使用字典(由另外兩個人建議)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM