Travel Around the World, One Double-Click at a Time
This tip will show you how to use a WebBrowser
control and a DataGridView
control to open CSV files that contain address information, and display the selected address (one at a time) from the grid in the browser control.
First, drop four controls on your form: a button
, an OpenFileDialog
, a DataGridView
, and a WebBrowser
.
Name them, respectively: btnRun
, ofd
, dgv
, and webBrowser1
. Of course, you could really name them just about whatever you want, but then you'd have to adjust the code snippets below accordingly.
After all that heavy lifting, feel free to take a break...
Now that you're back, the code follows, in snippets for the various pieces you'll need to add.
First, create a class to mirror the CSV file (which should contain data in the format Address
, City
, State
, Zip
):
public class AddressClass
{
public string CSV_Address { get; set; }
public string CSV_City { get; set; }
public string CSV_State { get; set; }
public string CSV_Zip { get; set; }
}
Now add this at the top of the form:
private List<addressclass> addressList;
The button click event:
private void btnRun_Click(object sender, EventArgs e)
{
if (LoadAddresses())
{
Popul8DGV();
}
}
Add the positively puny method just called:
private void Popul8DGV()
{
dgv.DataSource = addressList;
}
...and the more substantial one:
private bool LoadAddresses()
{
bool addressesLoaded = false;
ofd.Title = "Open CSV File";
ofd.Filter = "CSV files|*.csv";
ofd.InitialDirectory = @"C:\Direcciones";
if (ofd.ShowDialog() == DialogResult.OK)
{
string filename = ofd.FileName;
string[] filelines = File.ReadAllLines(filename);
addressList = new List<addressclass>();
AddressClass address;
foreach (string s in filelines)
{
address = new AddressClass();
string[] fileline = s.Split(',');
address.CSV_Address = fileline[0];
address.CSV_City = fileline[1];
address.CSV_State = fileline[2];
address.CSV_Zip = fileline[3];
if (string.IsNullOrWhiteSpace(address.CSV_Address)) continue;
if (address.CSV_Address.Equals("Address1")) continue;
addressList.Add(address);
addressesLoaded = true;
}
}
return addressesLoaded;
}
Finally, the DatatGridView
's doubleclick
event:
private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs dgvargs)
{
const int ADDRESS_COL = 0;
const int CITY_COL = 1;
const int STATE_COL = 2;
const int ZIP_COL = 3;
int row = dgvargs.RowIndex;
string addr = dgv.Rows[row].Cells[ADDRESS_COL].Value.ToString();
string city = dgv.Rows[row].Cells[CITY_COL].Value.ToString();
string st8 = dgv.Rows[row].Cells[STATE_COL].Value.ToString();
string zip = dgv.Rows[row].Cells[ZIP_COL].Value.ToString();
string urlStr = string.Format(@"https://www.google.com/maps/place/{0},+{1},+{2}+{3}/",
addr, city, st8, zip);
webBrowser1.Url = new Uri(urlStr);
}
That should be "all she wrote".
Sample Data and Recourse for the IE-Bitten
If you just want to try it out quickly, save these handful of sample addresses to a file named whateverYouWant.csv:
Scenic Valley Road, Jenny Lind, CA, 95252
Prindle Road,Mokelumne Hill,CA,95245
Hillside Apartments,San Andreas,CA,95249
412 Dela Vina,Monterey,CA,93940
523 Ramona Avenue,Monterey,CA,93940
40 Lower Ragsdale,Monterey,CA,93940
If you eventually run into a problem where the WebBrowser
scolds you for using an old fuddy-duddy browser (the WebBrowser
control is a wrapper for Internet Explorer), Google or Bing "How can I get the WebBrowser component to use a browser other than Internet Explorer 11?" and you will find the accepted answer on how to deal with this problem.
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).
Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.
I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven