2012-03-05 24 views
16

私のC#プロジェクト用のカスタムダイアログボックスを作成したいのですが、このカスタムダイアログボックスでDataGridViewを作成し、ボタンもあります。ユーザーがこのボタンをクリックすると、整数値が返されます呼び出し元、ダイアログボックスが終了します。値を返すカスタムダイアログボックスを作成する最も簡単な方法は?

どうすればいいですか?

+1

は窓が、私はに何か問題が表示されていないバック0にあなたをupvotedている – Bas

+2

を形成しています質問。 – Joel

答えて

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 promptValue = Prompt.ShowDialog(" Test "、" 123 ");がmain関数で呼び出されるとすぐにint値が返されることです。これは望ましくない。ユーザーがボタンを押したときにダイアログが値を返すようにしたい。そして、ボタンはフォームを閉じます。 – Ahmad

+5

@Ahmadそれは何が起こるかです。 – Bas

15
  1. 、ボタンには、あなたのダイアログで
  2. をDialogResult.OKするDialogResultプロパティを設定し
  3. あなたのボタンにAcceptButtonプロパティを設定作成あなたのフォームのパブリックプロパティint型の結果
  4. あなたのクリックイベントでこのプロパティの値を設定しますボタンこの@rcdmk
  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

関連する問題