i am trying to put train stations on the combo box in order for when the system is run using c# i will be able to pick which train station i want..can anybody help?
james6754 39 Junior Poster
private void button1_Click(object sender, EventArgs e)
{
string[] arr = new string[10];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = "String: " + i;
comboBox1.Items.Add(arr[i]);
}
}
You should bring along some of your own code to show you have at least attempted it.
ChrisHunter 152 Posting Whiz in Training Featured Poster
Are you going to type the names of the train station into code or are they retrieved from a database ?
siralv1 0 Newbie Poster
public partial class Form1 : Form
{
String[] stations = { "Manchester Victoria", "Rochdale", "Smithy Bridge", "Littleborough", "Walsden", "Todmorden", "Hebden Bridge", "Mytholmroyd", "Sowerby Bridge", "Halifax", "Bradford Interchange", "New Pudsey", "Bramley", "Leeds", "Cross Gates", "Garforth", "East Garforth", "Micklefield", "Selby" };
String[] Prices = { "0", "3.00", "0.60", "0.45", "0.75", "0.45", "0.90", "0.45", "0.90", "0.80", "1.80", "0.60", "0.45", "1.20", "1.50", "0.75", "0.30", "0.60", "2.40" };
private void bADD_Click(object sender, EventArgs e)
{
stations = new string[stations.Length];
Prices = new string[Prices.Length];
i was wondering how i could put a code behind the calculate button so that after the user has chosen the stations it calculates the prices and gives an accurate answer...
do i convert the string into a float and if so how can i do it?
james6754 39 Junior Poster
Well you need to give some more info about how you want to calculate the price...the price is £3.00 for example..right, so what are you calculating from that??
As for the string to it's floating point equivalent...
public void convert()
{
string s = "1.60";
float val;
bool successful = float.TryParse(s,out val);//TryParse() will return true if conversion succeded..false if not.
if(successful){}//carry on
}
Getting text from combox and index of station in array like this..
private void calculate_Click(object sender, EventArgs e)
{
string text = comboBox1.Text;//get current station selected
string[] arrStations = new string[10];//your array of stations...
string[] arrTicketPrices = new string[10];//your array of prices
int index = Array.IndexOf(arr, combobox1.Text);//find index of station
arrTicketPrices[index] //this could be the corresponding price for station...
//use this to calculate some other stuff...
}
siralv1 0 Newbie Poster
i want to calculate how much it will cost for example from manchester victoria to hebden bridge
i want the code to add up the prices for example £3.00 + 0.60p + 0.45p + 0.75p + 0.45p + 0.90 = price..if you know what i mean...
for a single ticket.....and how much it will cost for a return ticket.
castajiz_2 35 Posting Whiz
If I understood correctly all these stations are on the same route and Manchester Victoria is the one where the trip would eventually start so you gave it a value of 0 pounds but you can select another station and define it as a starting point.
First of all fill populate the combobox
List<string> _items = new List<string>();
for (int i = 0; i < stations.Length; i++)
{
_items.Add(stations[i]);
}
comboBox1.DataSource = _items;
Then create another combobox to define the destination(also populate).Now the main problem is that you have to add station (prices) which are between the starting point and destitanion point if there isn t anything between just add the price of the To STATION, if there is something between, for example start at index (victoria)[0] end at index(Littleborough)[3] then implement another loop to sum the prices and index 1,2,3 stop the loop after adding value at index 3.
siralv1 0 Newbie Poster
public partial class Form1 : Form
{
String[] stations = { "Manchester Victoria", "Rochdale", "Smithy Bridge", "Littleborough", "Walsden", "Todmorden", "Hebden Bridge", "Mytholmroyd", "Sowerby Bridge", "Halifax", "Bradford Interchange", "New Pudsey", "Bramley", "Leeds", "Cross Gates", "Garforth", "East Garforth", "Micklefield", "Selby" };
decimal[] Prices = {0.0m, 3.00m, 0.60m, 0.45m, 0.75m, 0.45m, 0.90m, 0.45m, 0.90m, 0.80m, 1.80m, 0.60m, 0.45m, 1.20m, 1.50m, 0.75m, 0.30m, 0.60m, 2.40m };
decimal dSingleFare = 0.0m;
decimal dAdultFare = 0.0m;
decimal dOapFare = 0.0m;
decimal dKidSingleFare = 0.0m;
bool bPeak = false;
DateTime time1 = Convert.ToDateTime("06:00");
DateTime time2 = Convert.ToDateTime("09:30");
DateTime time3 = Convert.ToDateTime("16:00");
DateTime time4 = Convert.ToDateTime("18:30");
public Form1()
{
InitializeComponent();
for (int x = 0; x < stations.Length; x++)
{
cmbstation1.Items.Add(stations[x]); // Put the station names in the combo boxes
cmbStation2.Items.Add(stations[x]);
}
//default_values();
cmbstation1.SelectedIndex = 0;
cmbStation2.SelectedIndex = 0;
dateTimePicker1.ShowUpDown = true; // puts Time onto the date and time picker
dateTimePicker1.CustomFormat = "hh:mm";
dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
DateTimePicker dateTimePicker2 = new DateTimePicker(); // puts Date onto the date and time picker.
Controls.AddRange(new Control[] { dateTimePicker2 });
// MessageBox.Show(dateTimePicker2.Value.ToString());
dateTimePicker2.Value = DateTime.Now.AddDays(1);
//MessageBox.Show(dateTimePicker2.Value.ToString());
}
private void bCalculate_Click(object sender, EventArgs e)
{
// Are we going towards Selby or Manshester?
int iDestination = cmbStation2.SelectedIndex;
if (cmbStation2.SelectedIndex < cmbstation1.SelectedIndex) iDestination = cmbstation1.SelectedIndex;
dSingleFare = 0.0m;
for (int x = 0; x < iDestination + 1; x++)
{
dSingleFare = dSingleFare + Prices[x];
}
// are we travelling at peak time?
// if travel time is between 6 and 9:30 OR 16:00 and 18:30 bPeak is true
if (dateTimePicker1.Value > time1 && dateTimePicker1.Value < time2) bPeak = true;
if (dateTimePicker1.Value > time3 && dateTimePicker1.Value < time4) bPeak = true;
if (bPeak == true)
{
dSingleFare = dSingleFare * 1.5m;
}
// a return ticket is 80 % of fare there and back
if (chbReturn.Checked == true)
{
dSingleFare = dSingleFare * 2.0m * 0.8m;
}
// multiply single person fare by number of adults
dAdultFare = dSingleFare * numericUpDown1.Value;
//calculates Oap maximum pay charge except for peak times
dOapFare = dSingleFare;
if (dOapFare > 1.0m) dOapFare = 1.0m;
dOapFare = dOapFare * numericUpDown2.Value;
// something abo8ut pewak times here
//kids pay half the asking price.
dKidSingleFare = dSingleFare / 2.0m;
MessageBox.Show("Adult Fare " + dAdultFare.ToString("C") + "\n OAP Fare " + dOapFare.ToString("C") + "\n Kids Fare " + dKidSingleFare.ToString("C"));
}
private void default_values()
{
cmbstation1.SelectedIndex = 0;
cmbStation2.SelectedIndex = 0;
numericUpDown1.Value = 0;
numericUpDown2.Value = 0;
numericUpDown3.Value = 0;
chbReturn.Checked = false;
}
private void bReset_Click(object sender, EventArgs e) // the reset button
{
default_values();
}
hey guys thanks for the help, basically i have finally managed to make everything to work...now what im a bit confused on is how to show the results for example
when the user clicks calculate i want a screen to pop up showing
the departure
the destination
the date
time
how many adults
how many oaps
how many kids
and whether it is a return trip or not
and the total amount..
so far i have managed to show the adult, oap and kids through a message box but i havent figured how i can show it the way i have described...any suggestions on how i could achieve this??
tinstaafl 1,176 Posting Maven
it looks like most of your variables are there, the values in your comboboxes should still be there, date and time you can get from your datetime picker, the return status from the checkbox. Looks like all you need is a variable to combine all the different fare variables.
Considering the number of variables you're juggling you might want to consider a struct
public struct Info
{
string Departure;
string Destination;
string DepartureDate;
string DepartureTime;
float AdultFare;
float OAPFare;
float KidsFare;
bool Return;
float TotalFare;
}
Info NewTicket = new Info();
Edited by tinstaafl
james6754 39 Junior Poster
You could simply create a form that will show when the user requests it. Create it and pass your values in to it's constructor and change your labels or whatever you have on your form within it.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.