Reputation: 41
I have a Windows Form with two textboxes and two buttons. The textboxes are named txtVisited and txtAnswer. The buttons are Visited?
and Exit
. I want to input a city into the first textbox and have it check the array for a match. If there is a match, I want to display the text that it has been visited and it's position in the array. If it does not find a match, I want it to display the text "not visited." I have copied all the code I have so far. Any help would be much appreciated. I'm still really new to c# so I may or may not understand your answer. So bear with me. Sorry in advance.
namespace Array
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnVisited_Click(object sender, EventArgs e)
{
string[] CityName = {"Columbus", "Bloomington", "Indianapolis",
"Fort Wayne", "Greensburg", "Gary", "Chicago", "Atlanta", "Las Vegas"};
bool visitedbool;
int Subscript;
string QueryCity;
QueryCity = txtState.Text.ToUpper();
int Subscript =0;
visitedbool = false;
while (visitedbool = true)
if (CityName(intsubscript).ToUpper= QueryCity)
{
visitedbool = true
}
else
{
Subscript = Subscript +1
}
}
}
}
Upvotes: 2
Views: 163
Reputation: 41
So I don't know what happened to the other guy's answer, but he helped me through this process. I just want to extend my deepest gratitude for his effort. He never got impatient with me and I'm a tard when it comes to c#. He gave me the code to get it working. I just had to add the code for the text boxes. `namespace Project { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnVisited_Click(object sender, EventArgs e)
{
{
string[] CityName = {"Columbus", "Bloomington", "Indianapolis",
"Fort Wayne", "Greensburg", "Gary", "Chicago", "Atlanta", "Las Vegas"};
string queryCity = txtState.Text;
int position;
string city;
if (CityName.Contains(queryCity))
{
position = Array.IndexOf(CityName, queryCity);
city = txtState.Text;
txtAnswer.Text = "You have visited" +" " + queryCity + " " + position;
}
else
{
txtAnswer.Text = "You have not visited this city yet.";
}
}`
Upvotes: 0
Reputation: 9224
var idx = Array.IndexOf(CityName, QueryCity);
if (idx != -1)
{
// display QueryCity and idx
}
else
{
// display "not visited"
}
Upvotes: 1