2012-03-05 30 views
16

Tôi muốn tạo hộp thoại tùy chỉnh cho dự án C# của mình .. Tôi muốn có một DataGridView trong hộp thoại tùy chỉnh này và cũng sẽ có nút .. Khi người dùng nhấp vào nút này, giá trị nguyên được trả về người gọi và sau đó hộp thoại tự kết thúc ..Cách dễ nhất để tạo hộp thoại tùy chỉnh trả về giá trị?

Làm cách nào để tôi có thể đạt được điều này?

+1

@rcdmk đây là Windows Forms – Bas

+2

Tôi đã bỏ phiếu tán bạn trở lại 0. Tôi không thấy bất cứ điều gì sai với câu hỏi. – Joel

Trả lời

28

Không có hộp thoại nhắc trong C#. Bạn có thể tạo hộp nhắc tùy chỉnh để thực hiện việc này thay thế.

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

Sau đó gọi nó là sử dụng:

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

mã có vẻ tốt .. Vấn đề duy nhất là giá trị int sẽ được trả về ngay sau khi 'int promptValue = Prompt.ShowDialog (" Test "," 123 ");' được gọi trong hàm main. Điều này là không mong muốn. Tôi muốn hộp thoại trả lại giá trị khi người dùng nhấn nút. Và sau đó nút cũng đóng biểu mẫu .. – Ahmad

+5

@Ahmad đó là những gì sẽ xảy ra. – Bas

15
  1. Mở nút của bạn thiết lập thuộc tính DialogResult để DialogResult.OK
  2. Trên hộp thoại của bạn thiết lập thuộc tính AcceptButton để nút của bạn
  3. Tạo tài sản công khai trong biểu mẫu của bạn được gọi là Kết quả của loại int
  4. Đặt giá trị của thuộc tính này trong sự kiện nhấp chuột của bạn nút
  5. Gọi thoại của bạn theo cách này

    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

hãy giải thích, đây là gì? – Lrrr

Các vấn đề liên quan