2015-02-23 58 views
2

我創建了一個custimazibale提示,只有當「OK」點擊值如何返回

public static class Prompt 
{ 
    public static string ShowDialog(int columnnumber, string columnname) 
    { 
     Form prompt = new Form(); 
     prompt.Width = 500; 
     prompt.Height = 150; 
     prompt.FormBorderStyle = FormBorderStyle.FixedDialog; 
     prompt.Text = columnname; 
     prompt.StartPosition = FormStartPosition.CenterScreen; 
     Label textLabel = new Label() { Left = 50, Top = 20 }; 
     ComboBox comboBox = new ComboBox() { Left = 50, Top = 50, Width = 400 }; 
     comboBox.Items.AddRange(new string[] { "a","b","c" }); 
     comboBox.DropDownStyle = ComboBoxStyle.DropDownList; 
     comboBox.SelectedItem = columnname; 
     Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 80 }; 
     confirmation.Click += (sender, e) => { prompt.Close(); }; 
     textLabel.Text = "Colonne " + (columnnumber + 1).ToString() + " : " + columnname; 
     prompt.Controls.Add(comboBox); 
     prompt.Controls.Add(confirmation); 
     prompt.Controls.Add(textLabel); 
     prompt.AcceptButton = confirmation; 
     prompt.ShowDialog(); 
     prompt.AcceptButton = confirmation; 

     return comboBox.Text; 
    } 
} 

然後我把它在我的主要形式被點擊

private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) 
{ 
    dt.Columns[e.ColumnIndex].ColumnName = Prompt.ShowDialog(e.ColumnIndex, dataGridView1.Columns[e.ColumnIndex].Name); 
} 

問題的標題時,即使點擊按鈕關閉,我的文字也會變化。 但我希望它只在用戶點擊「確定」按鈕時才能更改。

回答

1

您可以設定當確認一個布爾值,並用它來返回null如果沒有被證實,像這樣:

public static string ShowDialog(int columnnumber, string columnname) 
{ 
    Form prompt = new Form(); 
    prompt.Width = 500; 
    prompt.Height = 150; 
    prompt.FormBorderStyle = FormBorderStyle.FixedDialog; 
    prompt.Text = columnname; 
    prompt.StartPosition = FormStartPosition.CenterScreen; 
    Label textLabel = new Label() 
    { 
     Left = 50, 
     Top = 20 
    }; 
    ComboBox comboBox = new ComboBox() 
    { 
     Left = 50, 
     Top = 50, 
     Width = 400 
    }; 
    comboBox.Items.AddRange(new string[] { "a", "b", "c" }); 
    comboBox.DropDownStyle = ComboBoxStyle.DropDownList; 
    comboBox.SelectedItem = columnname; 
    Button confirmation = new Button() 
    { 
     Text = "Ok", 
     Left = 350, 
     Width = 100, 
     Top = 80 
    }; 

    bool confirmed = false; 

    confirmation.Click += (sender, e) => 
    { 
     prompt.Close(); 
     confirmed = true; 
    }; 
    textLabel.Text = "Colonne " + (columnnumber + 1).ToString() + " : " + columnname; 
    prompt.Controls.Add(comboBox); 
    prompt.Controls.Add(confirmation); 
    prompt.Controls.Add(textLabel); 
    prompt.AcceptButton = confirmation; 
    prompt.ShowDialog(); 
    prompt.AcceptButton = confirmation; 

    return confirmed ? comboBox.Text : null; 
} 

調用代碼將需要檢查返回值零,只有在返回的值不爲空時纔會執行某些操作。

+0

謝謝你很多,它很完美 – 2015-02-23 10:17:17

5

你可以評估DialogResult,並返回null如果不是OK

public static class Prompt 
{ 
    public static string ShowDialog(int columnnumber, string columnname) 
    { 
     using (Form prompt = new Form()) 
     { 
      // other code 
      return prompt.DialogResult == DialogResult.OK ? comboBox.Text : null; 
     } 
    } 
} 

,然後在你的另一種方法:

private void dataGridView1_ColumnHeaderMouseClick(object sender, EventArgs e) 
{ 
    var result = Prompt.ShowDialog(e.ColumnIndex, 
            dataGridView1.Columns[e.ColumnIndex].Name); 
    if (result != null) 
     dt.Columns[e.ColumnIndex].ColumnName = result; 
} 

而且你裏面prompt你應該相應地設置DialogResult

confirmation.Click += (sender, e) => 
    { 
     prompt.DialogResult = DialogResult.OK; 
     prompt.Close(); 
    }; 

建議:而不是result != null也可以使用!String.IsNullOrWhiteSpace(result)更新列名稱,如果輸入了內容。

+0

這可能比根據我的答案設置局部變量更好。 – 2015-02-23 10:26:50

+0

一旦DialogResult被設置,表單將被隱藏,但是如果表單在使用內部,那麼在你完成處理DialogResult之後,如果我沒有弄錯,它將被處理掉。 – 2015-02-23 10:27:05

+0

@MeAndSomeRandoms:只要你使用'ShowDialog',就是真的。使用'Show'(不是這裏的情況)它只會設置屬性。此外,該表格僅被隱藏,並且在這種情況下不會自動調用「關閉」,因此例如「手動呼叫」(例如,有人正在傾聽關閉事件... – ChrFin 2015-02-23 10:33:11

3

我會去這樣的事情:

using(Form prompt = new Form()) 
{ 
    //Initialize the components of your form 

    DialogResult result = prompt.ShowDialog(); 
    if(result == DialogResult.OK) 
    { 
     //return whatever it is you want to return 
    } 
} 

內,您的表格,您可以通過prompt.DialogResult = DialogResult.OK設置的DialogResult和一些更多的選擇(DialogResult.Cancel,DialogResult.Retry等)