2012-03-05 12 views
16

我想爲我的C#項目創建一個自定義對話框..我想在這個自定義對話框中有一個DataGridView,並且還會有一個按鈕..當用戶單擊此按鈕時,一個整數值被返回給來電者和對話框然後自行終止。最簡單的方法來創建一個返回值的自定義對話框?

我該如何做到這一點?

+1

@rcdmk這是Windows窗體 – Bas 2012-03-05 15:46:16

+2

我已經upvoted你回0。我看不出什麼毛病題。 – Joel 2013-05-08 10:43:35

回答

28

C#中沒有提示對話框。您可以創建一個自定義提示框來代替。

public static class Prompt 
    { 
     public static int ShowDialog(string text, string caption) 
     { 
      Form prompt = new Form(); 
      prompt.Width = 500; 
      prompt.Height = 100; 
      prompt.Text = caption; 
      Label textLabel = new Label() { Left = 50, Top=20, Text=text }; 
      NumericUpDown inputBox = new NumericUpDown() { Left = 50, Top=50, Width=400 }; 
      Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70 }; 
      confirmation.Click += (sender, e) => { prompt.Close(); }; 
      prompt.Controls.Add(confirmation); 
      prompt.Controls.Add(textLabel); 
      prompt.Controls.Add(inputBox); 
      prompt.ShowDialog(); 
      return (int)inputBox.Value; 
     } 
    } 

然後使用叫它:

int promptValue = Prompt.ShowDialog("Test", "123"); 
+0

代碼看起來很好..唯一的問題是int值會立即返回'int promptValue = Prompt.ShowDialog(「Test」,「123」);'在主函數中被調用。這是不希望的。我希望對話框在用戶按下按鈕時返回值。然後按鈕也關閉窗體.. – Ahmad 2012-03-06 06:20:12

+5

@Ahmad這就是發生了什麼。 – Bas 2012-05-30 19:40:02

15
  1. 在您的按鈕設置DialogResult屬性來DialogResult.OK
  2. 在您的對話框中設置的屬性的AcceptButton你的按鈕
  3. 創建在您的表單中稱爲int類型結果的公共屬性
  4. 在您的單擊事件中設置此屬性的值按鈕
  5. 打電話給你的對話中這樣

    using(myDialog dlg = new myDialog()) 
    { 
        if(dlg.ShowDialog() == DialogResult.OK) 
        { 
         int result = dlg.Result; 
         // whatever you need to do with result 
        } 
    } 
    
-1
//combo box dialog c# 
// 
public static string DialogCombo(string text,DataTable comboSource,string DisplyMember,string ValueMember) 
    { 
     //comboSource = new DataTable(); 


     Form prompt = new Form(); 
     prompt.RightToLeft = RightToLeft.Yes; 
     prompt.Width = 500; 
     prompt.Height = 200; 
     Label textLabel = new Label() { Left = 350, Top = 20, Text = text }; 
     ComboBox combo = new ComboBox { Left = 50, Top = 50, Width = 400 }; 
     combo.DataSource = comboSource; 
     combo.ValueMember = ValueMember; 
     combo.DisplayMember = DisplyMember; 
     Button confirmation = new Button() { Text = "تایید", Left = 350, Width = 100, Top = 70 }; 
     confirmation.Click += (sender, e) => { prompt.Close(); }; 
     prompt.Controls.Add(confirmation); 
     prompt.Controls.Add(textLabel); 
     prompt.Controls.Add(combo); 
     prompt.ShowDialog(); 

     return combo.SelectedValue.ToString(); 
    } 
2
public static DialogResult InputBox(string title, string promptText, ref string value,bool isDigit=false) 
    { 
     Form form = new Form(); 
     Label label = new Label(); 
     TxtProNet textBox = new TxtProNet(); 

     if (isDigit == true) 
      textBox.TypeNumricOnly = true; 

     textBox.Width = 1000; 
     Button buttonOk = new Button(); 
     Button buttonCancel = new Button(); 

     form.Text = title; 
     label.Text = promptText; 
     textBox.Text = value; 

     buttonOk.Text = "OK"; 
     buttonCancel.Text = "Cancel"; 
     buttonOk.DialogResult = DialogResult.OK; 
     buttonCancel.DialogResult = DialogResult.Cancel; 

     label.SetBounds(9, 20, 372, 13); 
     textBox.SetBounds(12, 36, 372, 20); 
     buttonOk.SetBounds(228, 72, 75, 23); 
     buttonCancel.SetBounds(309, 72, 75, 23); 

     label.AutoSize = true; 
     textBox.Anchor = textBox.Anchor | AnchorStyles.Right; 
     buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; 
     buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; 

     form.ClientSize = new Size(396, 107); 
     form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel }); 
     form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height); 
     form.FormBorderStyle = FormBorderStyle.FixedDialog; 
     form.StartPosition = FormStartPosition.CenterScreen; 
     form.MinimizeBox = false; 
     form.MaximizeBox = false; 
     form.AcceptButton = buttonOk; 
     form.CancelButton = buttonCancel; 

     DialogResult dialogResult = form.ShowDialog(); 
     value = textBox.Text; 
     return dialogResult; 
    } 
+0

請解釋一下,這是什麼? – Lrrr 2015-02-14 10:09:57

相關問題