c# gmap.net google marker - c#

I made an application that uses gmap.net. On the map I have three markers. Now what I'm trying to do is to click on a marker opens a new form, click on the second marker to open another form. This is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
namespace GMap
{
public partial class Form1 : Form
{
GMarkerGoogle marker;
GMapOverlay markerOverlay;
DataTable dt;
int Selekcija = 0;
double LatInicial = 43.1383292506958;
double LngInicial = 20.5198994278908;
double LatTehnicka = 43.1378458151015;
double LngTehnicka = 20.5214631557465;
double LatMedicinska = 43.1324426240355;
double LngMedicinska = 20.5122631788254;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add(new DataColumn("Opis", typeof(string)));
dt.Columns.Add(new DataColumn("Lat", typeof(double)));
dt.Columns.Add(new DataColumn("Long", typeof(double)));
//Ubacivanje podataka u tabelu
dt.Rows.Add("Gimnazija", LatInicial, LngInicial);
dt.Rows.Add("Tehnicka skola", LatTehnicka, LngTehnicka);
dt.Rows.Add("Medicinska skola", LatMedicinska, LngMedicinska);
dataGridView1.DataSource = dt;
//Vidljivost pojedinih kolona
dataGridView1.Columns[1].Visible = false;
dataGridView1.Columns[2].Visible = false;
gMapControl1.DragButton = MouseButtons.Left;
gMapControl1.CanDragMap = true;
gMapControl1.MapProvider = GMapProviders.GoogleMap;
gMapControl1.Position = new PointLatLng(LatInicial, LngInicial);
gMapControl1.MinZoom = 0;
gMapControl1.MaxZoom = 24;
gMapControl1.Zoom = 17;
gMapControl1.AutoScroll = true;
// Obelezivac
markerOverlay = new GMapOverlay("markers");
marker = new GMarkerGoogle(new PointLatLng(LatInicial, LngInicial),GMarkerGoogleType.green);
markerOverlay.Markers.Add(marker);
//marker.ToolTipMode = MarkerTooltipMode.Always;
marker.ToolTipText = string.Format("Gimnazija: \n Latituda: {0} \n Longituda: {1}", LatInicial, LngInicial);
gMapControl1.Overlays.Add(markerOverlay);
markerOverlay = new GMapOverlay("markers");
marker = new GMarkerGoogle(new PointLatLng(LatTehnicka, LngTehnicka), GMarkerGoogleType.green);
markerOverlay.Markers.Add(marker);
//marker.ToolTipMode = MarkerTooltipMode.Always;
marker.ToolTipText = string.Format("Tehnicka skola: \n Latituda: {0} \n Longituda: {1}", LatTehnicka, LngTehnicka);
gMapControl1.Overlays.Add(markerOverlay);
markerOverlay = new GMapOverlay("markers");
marker = new GMarkerGoogle(new PointLatLng(LatMedicinska, LngMedicinska), GMarkerGoogleType.green);
markerOverlay.Markers.Add(marker);
//marker.ToolTipMode = MarkerTooltipMode.Always;
marker.ToolTipText = string.Format("Medicinska skola: \n Latituda: {0} \n Longituda: {1}", LatMedicinska, LngMedicinska);
gMapControl1.Overlays.Add(markerOverlay);
}
private void button1_Click(object sender, EventArgs e)
{
dt.Rows.Add(txtOpis.Text, txtLatituda.Text, txtLongituda.Text);
}
private void SelekcijaSkole(object sender, DataGridViewCellMouseEventArgs e)
{
Selekcija = e.RowIndex;
txtOpis.Text = dataGridView1.Rows[Selekcija].Cells[0].Value.ToString();
txtLatituda.Text = dataGridView1.Rows[Selekcija].Cells[1].Value.ToString();
txtLongituda.Text = dataGridView1.Rows[Selekcija].Cells[2].Value.ToString();
marker.Position = new PointLatLng(Convert.ToDouble(txtLatituda.Text), Convert.ToDouble(txtLongituda.Text));
gMapControl1.Position = marker.Position;
}
private void gMapControl1_MouseDoubleClick(object sender, MouseEventArgs e)
{
double lat = gMapControl1.FromLocalToLatLng(e.X, e.Y).Lat;
double lng = gMapControl1.FromLocalToLatLng(e.X, e.Y).Lng;
txtLatituda.Text = lat.ToString();
txtLongituda.Text = lng.ToString();
marker.Position = new PointLatLng(lat, lng);
marker.ToolTipText = string.Format("Koordinate: \n Latituda {0} \n Longituda {1}", lat, lng);
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(Selekcija);
}
private void button3_Click(object sender, EventArgs e)
{
gMapControl1.MapProvider = GMapProviders.GoogleChinaSatelliteMap;
}
private void button4_Click(object sender, EventArgs e)
{
gMapControl1.MapProvider = GMapProviders.GoogleMap;
}
private void button5_Click(object sender, EventArgs e)
{
gMapControl1.MapProvider = GMapProviders.GoogleTerrainMap;
}
private void gMapControl1_OnMarkerClick(GMapMarker item, MouseEventArgs e)
{
}
}
}

The gMapControl has a event called OnMarkerClick that you can subscribe to to listen for click events on your makers. You can right click your GmapControl and then select properties. Then Click the lightening bolt button and that will list your events and in there is a OnMarkerClick Event you can double click it and it will build a event handler for you, or you can set it like so.
gMapControl1.OnMarkerClick += (marker, mouseArgs) =>
{
// From this point marker is the clicked marker do as you wish here
// Pass it to another form and use form.show to display the form.
// MessageBox.Show is to show proof the event fired.
MessageBox.Show(marker.ToolTipText);
// If you have a marker form you can display it like so.
MarkerForm form = new MarkerForm();
form.Show();
};

Related

How to display the route using GMap in C#?

My main problem is that it doesn't display the line between the 2 markers when I click on the display button.It should draw my route between the first pin and the second pin, but it doesn't show me anything at all. I've been trying for some time to figure out what's wrong.
This is the code. I also uploaded images, maybe it will help.
Can anyone help me with this? Thanks!
code
interface
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GMap.NET.MapProviders;
using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
namespace Licenta
{
public partial class Map : Form
{
List<PointLatLng> _points;
public Map()
{
InitializeComponent();
_points = new List<PointLatLng>();
}
private void Map_Load(object sender, EventArgs e)
{
GMapProviders.GoogleMap.ApiKey = "#AIzaSyBreQ8N4txQ74BwKwigtnmkSW8Vhisyles";
gMapControl1.ShowCenter = false;
}
private void btnAdauga_Click(object sender, EventArgs e)
{
_points.Add(new PointLatLng(Convert.ToDouble(txtLat.Text), Convert.ToDouble(txtLong.Text)));
}
private void btnSterge_Click(object sender, EventArgs e)
{
if (gMapControl1.Overlays.Count > 0)
{
gMapControl1.Overlays.RemoveAt(0);
gMapControl1.Refresh();
}
}
private void btnAfiseaza_Click(object sender, EventArgs e)
{
var route = GoogleMapProvider.Instance.GetRoute(_points[0], _points[1], false, false, 14);
var r = new GMapRoute(route.Points, "My Route")
{
Stroke = new Pen(Color.Red, 5)
};
var routes = new GMapOverlay("routes");
routes.Routes.Add(r);
gMapControl1.Overlays.Add(routes);
lblDist.Text = route.Distance + " km";
}
GMapOverlay markers = new GMapOverlay("markers");
private void btnIncarca_Click(object sender, EventArgs e)
{
gMapControl1.DragButton = MouseButtons.Left;
gMapControl1.MapProvider = GMapProviders.GoogleMap;
double lat = Convert.ToDouble(txtLat.Text);
double longt = Convert.ToDouble(txtLong.Text);
gMapControl1.Position = new PointLatLng(lat, longt);
gMapControl1.MinZoom = 2;
gMapControl1.MaxZoom = 100;
gMapControl1.Zoom = 7;
PointLatLng point = new PointLatLng(lat, longt);
GMapMarker marker = new GMarkerGoogle(point, GMarkerGoogleType.red_pushpin);
markers.Markers.Add(marker);
gMapControl1.Overlays.Add(markers);
}
private void gMapControl1_Load(object sender, EventArgs e)
{
gMapControl1.SetPositionByKeywords("Timisoara, Romania");
}
private void button5_Click(object sender, EventArgs e)
{
_points.Clear();
}
}
}

Save a copy of the file to specific folder, from a LinkLabel Hyperlink

I have a program that creates a list of hyperlinks to the files i search for. I can click the resulting hyperlink and the PDF opens. I would like to also have it make a copy of the pdf on my C: also without dialog. The source of the files is from the network. FileSearchButton_Click will search a directory for files with the content that match my search string.
aLinkLabel_LinkClicked is where I feel the copy action should be done
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.DirectoryServices;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FindFilesBasedOnText
{
public partial class FileSearcherBasedOnSpecificText : Form
{
SmartTextBox DirectoryTextBox = new SmartTextBox();
SmartTextBox SearchTextBox = new SmartTextBox();
SmartButton SearchButton = new SmartButton();
SmartButton FileSearchButton = new SmartButton();
public FileSearcherBasedOnSpecificText()
{
InitializeComponent();
DirectoryTextBox.Location = new Point(70, 12);
DirectoryTextBox.ForeColor = Color.Black;
DirectoryTextBox.ForeColor = Color.Black;
this.Controls.Add(DirectoryTextBox);
SearchTextBox.Location = new Point(70, 43);
//SearchTextBox.Size = new Size(478, 20);
this.Controls.Add(SearchTextBox);
SearchButton.Location = new Point(526, 12);
SearchButton.Size = new Size(160, 23);
SearchButton.Text = "Search Directory";
SearchButton.TabStop = false;
SearchButton.Click += SearchButton_Click;
this.Controls.Add(SearchButton);
FileSearchButton.Location = new Point(526, 43);
FileSearchButton.Size = new Size(160, 23);
FileSearchButton.Text = "Search file based-on text";
FileSearchButton.TabStop = false;
FileSearchButton.Click += FileSearchButton_Click;
this.Controls.Add(FileSearchButton);
listBox1.AllowDrop = true;
listBox1.DragDrop += listBox1_DragDrop;
listBox1.DragEnter += listBox1_DragEnter;
listBox1.Focus();
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
listBox1.Items.AddRange(File.ReadAllLines(file));
}
private void SearchButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
DialogResult dialogReasult = folderBrowserDialog.ShowDialog();
if(!string.IsNullOrWhiteSpace(folderBrowserDialog.SelectedPath))
{
DirectoryTextBox.Text = folderBrowserDialog.SelectedPath;
}
}
public void FileSearchButton_Click(object sender, EventArgs e)
{
FilesPanel.Controls.Clear();
int i = 0;
int y = 5;
string filesName = string.Empty;
string rootfolder = DirectoryTextBox.Text.Trim();
string[] files = Directory.GetFiles(rootfolder, "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
try
{
string contents = File.ReadAllText(file);
if(contents.Contains(SearchTextBox.Text.Trim()))
{
i += 1;
LinkLabel aLinkLabel = new LinkLabel();
aLinkLabel.Text = file;
aLinkLabel.Location = new Point(5, y);
aLinkLabel.AutoSize = true;
aLinkLabel.BorderStyle = BorderStyle.None;
aLinkLabel.LinkBehavior = LinkBehavior.NeverUnderline;
aLinkLabel.ActiveLinkColor = Color.White;
aLinkLabel.LinkColor = Color.White;
aLinkLabel.BackColor = Color.Transparent;
aLinkLabel.VisitedLinkColor = Color.Red;
aLinkLabel.Links.Add(0, file.ToString().Length, file);
aLinkLabel.LinkClicked += aLinkLabel_LinkClicked;
FilesPanel.Controls.Add(aLinkLabel);
y += aLinkLabel.Height + 5;
}
else
{ // This shows DONE after each Search
LinkLabel aLinkLabel = new LinkLabel();
aLinkLabel.Font = new Font("", 12);
aLinkLabel.ActiveLinkColor = Color.White;
aLinkLabel.LinkColor = Color.White;
aLinkLabel.BackColor = Color.Transparent;
aLinkLabel.Text = "DONE";
FilesPanel.Controls.Add(aLinkLabel);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
void aLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel lnk = new LinkLabel();
lnk = (LinkLabel)sender;
lnk.Links[lnk.Links.IndexOf(e.Link)].Visited = true;
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
//Create a directory if not exist for the file to be copied into
if (!System.IO.Directory.Exists(#"C:\Temp\RoHS_Docs"))
{
System.IO.Directory.CreateDirectory(#"C:\Temp\RoHS_Docs");
}
//Perform the file copy action on click
string CopyDestinationPath = #"C:\Temp\RoHS_Docs\";
System.IO.File.Copy(WHAT????, CopyDestinationPath, true);
}
private void FileSearcherBasedOnSpecificText_Load(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
FilesPanel.Controls.Clear();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SearchTextBox.Text = listBox1.SelectedItem.ToString();
}
private void label4_Click(object sender, EventArgs e)
{
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
}
}

How to plot graphs with data from a SerialPort with multiple sensors

I need to plot data from sensors of pH, Temperature and Humidity, the data is sent as a matrix from arduino to PC through the serial port.
I can show the data in a TextBox, but when I try to plot the data, it doesn't work (I don't know how to do it).
I just can plot the data when is not a matrix and it's data from just one sensor.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Windows.Forms.DataVisualization.Charting;
namespace grafik1
{
public partial class Form1 : Form
{
private SerialPort sensport;
private DateTime datetime;
private string data;
private string data2;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DataSource = SerialPort.GetPortNames();
timer1.Start();
}
double rt = 0;
Boolean i = false;
private void timer1_Tick(object sender, EventArgs e)
{
rt = rt + 0.1;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
sensport.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox2.Text == "")
{
MessageBox.Show("Error");
}
else
{
sensport = new SerialPort();
sensport.BaudRate = int.Parse(comboBox2.Text);
sensport.PortName = comboBox1.Text;
sensport.Parity = Parity.None;
sensport.DataBits = 8;
sensport.StopBits = StopBits.One;
sensport.Handshake = Handshake.None;
sensport.DataReceived += sensport_DataReceived;
try
{
sensport.Open();
textBox1.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
}
void sensport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (i == false)
{
rt = 0;
i = true;
}
data = sensport.ReadLine();
this.chart1.Series["Data1"].Points.AddXY(rt, data);
this.Invoke(new EventHandler(displaydata_event));
}
private void displaydata_event(object sender, EventArgs e)
{
datetime = DateTime.Now;
string time = datetime.Day + "/" + datetime.Month + "/" + datetime.Year + "\t" + datetime.Hour + ":" + datetime.Minute + ":" + datetime.Second;
txtData.AppendText(time + "\t" + data + "\n");
}
private void button2_Click(object sender, EventArgs e)
{
string directorio = textBox1.Text;
if (directorio == "")
{
MessageBox.Show("Error");
}
else {
try
{
string kayıtyeri = #"" + directorio + "";
this.chart1.SaveImage(("+kayityeri+"), ChartImageFormat.Png);
MessageBox.Show("Grafica guardada en " + kayıtyeri);
}
catch (Exception ex3)
{
MessageBox.Show(ex3.Message, "Error");
}
}
}
private void label4_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
sensport.Close();
}
private void button4_Click(object sender, EventArgs e)
{
try
{
string pathfile = #"C:\Users\MARIO GONZALEZ\Google Drive\VisualStudio\Arduino0_1\DATA";
string filename = "arduinoRTPv1.xls";
System.IO.File.WriteAllText(pathfile + filename, txtData.Text);
MessageBox.Show("Data saved");
}
catch (Exception ex3)
{
MessageBox.Show(ex3.Message, "Error");
}
}
private void button5_Click(object sender, EventArgs e)
{
}
private void chart1_Click(object sender, EventArgs e)
{
}
}
}
Let's assume you have prepared your chart, maybe like this:
chart1.Series.Clear();
chart1.Series.Add("ph");
chart1.Series.Add("Temp");
chart1.Series.Add("Hum");
chart1.Series["ph"].ChartType = SeriesChartType.Line;
chart1.Series["Temp"].ChartType = SeriesChartType.Line;
chart1.Series["Hum"].ChartType = SeriesChartType.Line;
chart1.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
chart1.ChartAreas[0].AxisY2.Title = "Temp";
chart1.ChartAreas[0].AxisY2.Maximum = 100;
Now you can add a string that contains some data blocks as shown in the comments..
string data = "7.5 23.8 67 \n8.5 23.1 72 \n7.0 25.8 66 \n";
..like this:
var dataBlocks = data.Split('\n');
foreach (var block in dataBlocks)
{
var numbers = block.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries);
// rt += someTime; (*)
for (int i = 0; i < numbers.Length; i++)
{
double n = double.NaN;
bool ok = double.TryParse(numbers[i], out n);
if (ok) chart1.Series[i].Points.AddXY(rt, n);
else
{
int p = chart1.Series[i].Points.AddXY(rt, 0);
chart1.Series[i].Points[p].IsEmpty = true;
Console.WriteLine("some error message..");
}
}
}
I have modified the data a little to show the changes a little better..
Note that I left out the counting up of your timer rt, which is why the chart shows the points with indexed x-values. For a real realtime plot do include it maybe here (*) !
If you keep adding data your chart will soon get rather crowded.
You will then either have to remove older data from the beginning or at least set a minimum and maximum x-values to restrict the display to a reasonably number of data points and or turn on zooming!
See here here and here for some examples and discussions of these things!

DataGridView column DateTimePicker in vb.net C#

I am using a DataGridView inner DateTimePicker, but it is not working properly.
It can only select the year or month and the calendar control is closed.
How do I to select the year & month & date?
Try this code (c# code)
DateTimePicker dtp = new DateTimePicker(); //DateTimePicker
Rectangle _Rectangle;
public Form2()
{
InitializeComponent();
dataGridView2.Controls.Add(dtp);
dtp.Visible = false; //
dtp.Format = DateTimePickerFormat.Custom; //2010-08-05
dtp.TextChanged += new EventHandler(dtp_TextChange);
}
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
switch (dataGridView2.Columns[e.ColumnIndex].Name)
{
case "dateAchatDataGridViewTextBoxColumn1":
_Rectangle = dataGridView2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //
dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height); //
dtp.Location = new Point(_Rectangle.X, _Rectangle.Y); //
dtp.Visible = true;
break;
}
}
private void dtp_TextChange(object sender, EventArgs e)
{
dataGridView2.CurrentCell.Value = dtp.Text.ToString();
}
private void dataGridView2_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
dtp.Visible = false;
}
private void dataGridView2_Scroll(object sender, ScrollEventArgs e)
{
dtp.Visible = false;
}
This is answer:
public partial class Form1 : Form
{
private DateTimePicker cellDateTimePicker = new DateTimePicker();
DateTimePicker[] sp = new DateTimePicker[100];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("Start-Date", typeof(DateTime)));
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
sp[e.RowIndex] = new DateTimePicker();
if (dataGridView1.Columns[e.ColumnIndex].Name == "Start-Date")
{
this.cellDateTimePicker = sp[e.RowIndex];
this.cellDateTimePicker.Format = DateTimePickerFormat.Time;
this.cellDateTimePicker.Name = "sp[" + e.RowIndex + "]";
// this.cellDateTimePicker.ValueChanged += new EventHandler(cellDateTimePickerValueChanged);
this.cellDateTimePicker.Visible = true;
this.cellDateTimePicker.CustomFormat = "dd/MM/yyyy";
this.cellDateTimePicker.Format = DateTimePickerFormat.Custom;
this.dataGridView1.Controls.Add(cellDateTimePicker);
System.Drawing.Rectangle tempRect = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
cellDateTimePicker.Location = tempRect.Location;
cellDateTimePicker.Width = tempRect.Width;
dataGridView1.Columns[0].DefaultCellStyle.Format = "MM'/'dd'/'yyyy";
this.cellDateTimePicker.Visible = true;
}
}
}

Convert string to double from combobox

I am having a real difficulty getting a string that is being returned from a combobox to convert to a double. I have done some research online and I believe that it should be working. I keep getting user exceptions.
Specifically, I am having an issue with the following part of my code:
private void cboBeverage_SelectedIndexChanged(object sender, EventArgs e)
{
string tempString = cboBeverage.SelectedValue.ToString();
double tempPrice = Convert.ToDouble(tempString);
Calculations(tempPrice);
}
Here is my entire code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JO_BillCalculator
{
public partial class Form1 : Form
{
double subtotal = 0.00;
double tax = 6.875;
double total = 0.00;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Add data to dtBeverage datatable
DataTable dtBeverage = new DataTable();
DataColumn dc1Beverage = new DataColumn("ItemBeverage");
DataColumn dc2Beverage = new DataColumn("PriceBeverage");
dtBeverage.Columns.Add(dc1Beverage);
dtBeverage.Columns.Add(dc2Beverage);
dtBeverage.Rows.Add("", "0.00");
dtBeverage.Rows.Add("Milk", "1.50");
dtBeverage.Rows.Add("Juice", "2.50");
dtBeverage.Rows.Add("Mineral Water", "2.95");
dtBeverage.Rows.Add("Coffee", "1.25");
dtBeverage.Rows.Add("Tea", "1.50");
dtBeverage.Rows.Add("Soda", "1.95");
//Display data in correct combobox
cboBeverage.DataSource = dtBeverage;
cboBeverage.DisplayMember = "ItemBeverage";
cboBeverage.ValueMember = "PriceBeverage";
//Add data to dtAppitizer datatable
DataTable dtAppitizer = new DataTable();
DataColumn dc1Appitizer = new DataColumn("ItemAppitizer");
DataColumn dc2Appitizer = new DataColumn("PriceAppitizer");
dtAppitizer.Columns.Add(dc1Appitizer);
dtAppitizer.Columns.Add(dc2Appitizer);
dtAppitizer.Rows.Add("", "0.00");
dtAppitizer.Rows.Add("Buffalo Wings", "5.95");
dtAppitizer.Rows.Add("Buffalo Fingers", "6.95");
dtAppitizer.Rows.Add("Potato Skins", "8.95");
dtAppitizer.Rows.Add("Nachos", "8.95");
dtAppitizer.Rows.Add("Mushroom Caps", "10.95");
dtAppitizer.Rows.Add("Shrimp Cocktail", "12.95");
dtAppitizer.Rows.Add("Chips and Salsa", "6.95");
//Display data in correct combobox
cboAppetizer.DataSource = dtAppitizer;
cboAppetizer.DisplayMember = "ItemAppitizer";
cboAppetizer.ValueMember = "PriceAppitizer";
//Add data to dtMainCourse datatable
DataTable dtMainCourse = new DataTable();
DataColumn dc1MainCourse = new DataColumn("ItemMainCourse");
DataColumn dc2MainCourse = new DataColumn("PriceMainCourse");
dtMainCourse.Columns.Add(dc1MainCourse);
dtMainCourse.Columns.Add(dc2MainCourse);
dtMainCourse.Rows.Add("", "0.00");
dtMainCourse.Rows.Add("Chicken Alfredo", "13.95");
dtMainCourse.Rows.Add("Chicken Picatta", "13.95");
dtMainCourse.Rows.Add("Turkey Club", "11.95");
dtMainCourse.Rows.Add("Lobster Pie", "19.95");
dtMainCourse.Rows.Add("Prime Rib", "20.95");
dtMainCourse.Rows.Add("Shrimp Scampi", "18.95");
dtMainCourse.Rows.Add("Turkey Dinner", "13.95");
dtMainCourse.Rows.Add("Stuffed Chicken", "14.95");
dtMainCourse.Rows.Add("Seafood Alfredo", "15.95");
//Display data in correct combobox
cboMainCourse.DataSource = dtMainCourse;
cboMainCourse.DisplayMember = "ItemMainCourse";
cboMainCourse.ValueMember = "PriceMainCourse";
//Add data to dtDessert datatable
DataTable dtDessert = new DataTable();
DataColumn dc1Dessert = new DataColumn("ItemDessert");
DataColumn dc2Dessert = new DataColumn("PriceDessert");
dtDessert.Columns.Add(dc1Dessert);
dtDessert.Columns.Add(dc2Dessert);
dtDessert.Rows.Add("", "0.00");
dtDessert.Rows.Add("Apple Pie", "5.95");
dtDessert.Rows.Add("Sundae", "3.95");
dtDessert.Rows.Add("Carrot Cake", "5.95");
dtDessert.Rows.Add("Mud Pie", "4.95");
dtDessert.Rows.Add("Apple Crisp", "5.95");
//Display data in correct combobox
cboDessert.DataSource = dtDessert;
cboDessert.DisplayMember = "ItemDessert";
cboDessert.ValueMember = "PriceDessert";
}
private void cboBeverage_SelectedIndexChanged(object sender, EventArgs e)
{
string tempString = cboBeverage.SelectedValue.ToString();
double tempPrice = Convert.ToDouble(tempString);
Calculations(tempPrice);
}
private void cboAppetizer_SelectedIndexChanged(object sender, EventArgs e)
{
cboAppetizer.SelectedIndex = 0;
}
private void cboMainCourse_SelectedIndexChanged(object sender, EventArgs e)
{
cboMainCourse.SelectedIndex = 0;
}
private void cboDessert_SelectedIndexChanged(object sender, EventArgs e)
{
cboDessert.SelectedIndex = 0;
}
private void Calculations(double price)
{
}
}
}
use selectedItem insted of SelectedValue
DataRow selectedDataRow = ((DataRowView)cboBeverage.SelectedItem).Row;
double tempPrice = Convert.ToDouble(selectedDataRow["PriceBeverage"]);
Calculations(tempPrice);

Categories