2012-03-05 14 views
16

Voglio creare una finestra di dialogo personalizzata per il mio progetto C#. Voglio avere un DataGridView in questa finestra di dialogo personalizzata, e ci sarà anche un pulsante .. Quando l'utente fa clic su questo pulsante, viene restituito un valore intero a il chiamante e la finestra di dialogo termina automaticamente.Il modo più semplice per creare una finestra di dialogo personalizzata che restituisce un valore?

Come posso ottenere questo risultato?

+1

@rcdmk questo è Windows Forms – Bas

+2

ti ho upvoted torna a 0. Non vedo nulla di sbagliato con il domanda. Il codice – Joel

risposta

28

Non c'è una finestra di dialogo di richiesta in C#. Puoi invece creare una casella di richiesta personalizzata per farlo.

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

Quindi chiamare utilizzando:

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

sembra corretto .. L'unico problema è che il valore int sarebbe restituito non appena 'int promptValue = Prompt.ShowDialog (" Test "," 123 ");' viene chiamato nella funzione principale. Questo non è desiderato. Voglio che la finestra di dialogo restituisca il valore quando l'utente preme il pulsante. E poi il pulsante chiude anche il modulo .. – Ahmad

+5

@Ahmad ecco cosa succede. – Bas

15
  1. nel pulsante impostare la proprietà DialogResult per DialogResult.OK
  2. Sul dialogo impostare la proprietà AcceptButton al pulsante
  3. Creare un proprietà pubblica nel modulo chiamato Result of int type
  4. Imposta il valore di questa proprietà nell'evento click del tuo Pulsante
  5. zero Chiamate finestra in questo modo

    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

per favore spiega, cos'è questo? – Lrrr

Problemi correlati