2016-04-03 12 views
1

Dizileri öğrenmeye çalışıyorum ve tüm dizilerimi girdikten sonra metin kutumda gösterilmesinin neden neden olduğunu anlayamıyorum. İşte Öğelerim, diziye bir sayı girdikten sonra neden 0?

İşte
public Form1() 
    { 
     InitializeComponent(); 
    } 
    int[] a = new int[10]; //global int!!! 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void btn_Enter_Click(object sender, EventArgs e) 
    { 
     //all elements show as 0 but why ??? come back to this later tonight! 


     string input; 

     for (int place = 0; place < 10; place++) 
     { 
      input = null; 

      My_Dialogs.InputBox("User Input Request!", "Please enter a number to be stored in element " + place + " : ", ref input); 
    Int32.TryParse(input, out a[place]); 

     } 
    } 

    private void btn_display_Click(object sender, EventArgs e) 
    { 
     for (int place = 0; place < 10; place++) 
      textBox1.AppendText("Element" + place + " of the array contains " + a[place] + "\n"); 
    } 

    private void btn_quit_Click(object sender, EventArgs e) 
    { 
     Close(); 
    } 

mydialogs kodu

class My_Dialogs 
{ 
    public static string InputBox(string promptText) 
    { 
     string default_value = ""; 

     return InputBox("", promptText, ref default_value); 
    } 
    public static string InputBox(string title, string promptText) 
    { 
     string default_value = ""; 

     return InputBox(title, promptText, ref default_value); 
    } 
    public static string InputBox(string title, string promptText, ref string value) 
    { 
     Form form = new Form(); 
     Label label = new Label(); 
     TextBox textBox = new TextBox(); 
     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; 

     if (form.ShowDialog() == DialogResult.Cancel) 
     { 
      textBox.Text = ""; 
     } 
     return textBox.Text; 
    } 
} 

}

benim kodudur ////////////////// ////////////////////////////////////////////////// ////////

+0

Dönüşüm işleminin başarılı olup olmadığını kontrol etmediniz. Yapar? Kodu ayıklayın ve ne olduğunu görün. –

+0

"Girdi" bir tam sayıya ayrıştırılamıyor gibi görünüyor. Int32.TryParse() 'de bir kesme noktası ayarlayın ve giriş içeriğini değerlendirin. – derpirscher

+0

kodunuz derleniyor mu? Ben endeksleyici 'out' değiştirici kullanamaz düşünüyorum düşünüyorum bir [yer] ' –

cevap

0

Bu kodu giriş kutunuz için eğitmeninizden aldığınızdan emin misiniz? Girilen metni hiçbir zaman value'a atar, böylece input değişkeniniz her zaman boş olacaktır. Ya da Inputbox kodu

yılında

value = textBox.Text; 

eklemek veya dönüşüm başarılı olursa Ayrıca, TryParse sonucunu kontrol etmelidir

input = My_Dialogs.InputBox("User Input Request!", "Please enter a number to be stored in element " + place + " : "); 

kullanmalıdır.

+0

evet kod kesinlikle benim öğretmenimden –

+0

artık giriş = using using = –

1

EDIT Haklısınız iade ifade önce myDialog sınıfa aşağıdaki eklemeniz gerekir:

value = textBox.Text;

Bu benim için sadece iyi çalışır: (girdi varsayarak aslında bir int.) Bilmeden

whats My_Dialogs.InputBox Sorunun ne olduğunu söylemek zor. İletişim kutusunun hemen ardından girişin değeri nedir?

class Program 
    { 
     private static int[] a = new int[10]; //global int!!! 
     static void Main(string[] args) 
     { 
      for (int place = 0; place < a.Length; place++) 
      { 
       Console.WriteLine("Please enter a number to be stored in element " + place + " : "); 
       var input = Console.ReadLine(); 
       Int32.TryParse(input, out a[place]); 
      } 

     } 
    } 
+0

değeri "Int32.TryParse" satırında (giriş, bir [yer]) yapılır; – derpirscher

+0

@CharlesMager [Bu keman] 'a (https://dotnetfiddle.net/T8dzsr) bakın; out out – derpirscher

+0

My_Dialogs.InputBox, öğretmenim tarafından bir giriş kutusu olarak kullanmak için verilmiş olan şeydir –

İlgili konular