Farhad Ahmad

Farhad Ahmad

  • NA
  • 11
  • 2.5k

Arrays & IF Statement

Dec 16 2016 9:29 AM
I have two questions about the codes below. 
 
 1) Code Block One below in Blue Works, but the issue is It does not allow the user to enter a value - it is understandable because Script is in execution stat. please see my comments in the codes to better understand what i am referring to 
 
2) Code Block Two Below in Red - I created a separate click event for reading the values from the file, creating an array and populating the list box. A second click event for finding the value entered by the user in the array created in first click event. The IF statement in this code block does not work, while the same IF in Code Block one works. I used the ShowBox(_) method to verify if InputNumber variable & AccounNumber[Index]  are created, and they are. also they are back int data type. Any suggestions what is wrong?
public partial class Form1 : Form
{
private const int size = 100;
private int Count = 0;
private int[] AccountNumber = new int[size];
public Form1()
{
InitializeComponent();
}
private void ValidateButton_Click(object sender, EventArgs e)
{
try
{
// Read the values from File
StreamReader InPutFile;
InPutFile = File.OpenText(@"C:\Users\ahmadf1\Documents\C Sharp Exercises\Source Code and Chapter Tutorial Files\Chap07\ChargeAccounts.txt");
while (!InPutFile.EndOfStream && Count < AccountNumber.Length)
{
AccountNumber[Count] = int.Parse(InPutFile.ReadLine());
Count++;
}
InPutFile.Close();
//Populate values in List Box
for (int index = 0; index < AccountNumber.Length; index++)
{
DisplayListBox.Items.Add(AccountNumber[index]);
}
//Prompt the user to enter the values to be searched in the array.
MessageBox.Show("Enter a Number in the List");
//How do I make the program to allow the user Entera values.
//In this sequence it does not, scripts keep executing after MessageBox prompt is acknowledged.
// If i enter the value before executing the whole script, then the logic below works.
int InputNumber;
if (int.TryParse(NumbertextBox.Text, out InputNumber))
{
MessageBox.Show(InputNumber.ToString());
for (int index = 0; index < Count; index++)
{
MessageBox.Show(AccountNumber[index].ToString());
if (InputNumber == AccountNumber[index])
{
MessageBox.Show("Number is Valid");
}
else
{
MessageBox.Show("Number is not Valid");
}
}
}
else if (NumbertextBox.Text == "")
{
MessageBox.Show("No Value is Entered");
}
else
{
MessageBox.Show("Invalid Data Type");
}
}
catch
{
MessageBox.Show("Something is Wrong");
}
}
 
 
CODE BLOCK TWO
 
public partial class Form1 : Form
{
private const int size = 100;
private int Count = 0;
private int[] AccountNumber = new int[size];
public Form1()
{
InitializeComponent();
}
private void ValidateButton_Click(object sender, EventArgs e)
{
try
{
// Read the values from File
StreamReader InPutFile;
InPutFile = File.OpenText(@"C:\Users\ahmadf1\Documents\C Sharp Exercises\Source Code and Chapter Tutorial Files\Chap07\ChargeAccounts.txt");
while (!InPutFile.EndOfStream && Count < AccountNumber.Length)
{
AccountNumber[Count] = int.Parse(InPutFile.ReadLine());
Count++;
}
InPutFile.Close();
//Populate values in List Box
for (int index = 0; index < AccountNumber.Length; index++)
{
DisplayListBox.Items.Add(AccountNumber[index]);
}
//Prompt the user to enter the values to be searched in the array.
MessageBox.Show("Enter a Number in the List");
 
private void FindNumberButton_Click(object sender, EventArgs e)
{
int InputNumber;
if (int.TryParse(NumbertextBox.Text, out InputNumber))
{
MessageBox.Show(InputNumber.ToString());
for (int index=0; index<Count; index++)
{
MessageBox.Show(AccountNumber[index].ToString());
if(InputNumber == AccountNumber[index])
{
MessageBox.Show("Number is Valid");
}
else
{
MessageBox.Show("Number is not Valid");
}
}
}
else if (NumbertextBox.Text == "")
{
MessageBox.Show("No Value is Entered");
}
else
{
MessageBox.Show("Invalid Data Type");
}
}
}
}
 

Answers (1)