Reputation: 13
I'm trying to learn arrays and I cannot understand why after inputting all my arrays when I get it to show in my text box it says that they are all 0 ?
Here is my code
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();
}
Here is the mydialogs code
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;
}
}
}
////////////////////////////////////////////////////////////////////////////
Upvotes: 1
Views: 54
Reputation: 4151
EDIT You need to add the following to your MyDialog class right before the return statement:
value = textBox.Text;
This works just fine for me: (assuming the input is in fact an int.)
Without knowing whats in My_Dialogs.InputBox
Its hard to tell what the problem is. What is the value of input immediately following the dialog box?
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]);
}
}
}
Upvotes: 1
Reputation: 17390
Are you sure you got this code for the inputbox from your instructor? It never assigns the inserted text to value
thus your input
variable will always be null. You should either insert
value = textBox.Text;
in your InputBox code
or use
input = My_Dialogs.InputBox("User Input Request!", "Please enter a number to be stored in element " + place + " : ");
Furthermore you should check the result of TryParse
if the conversion succeeded.
Upvotes: 0