Hi, I am trying to create a simple application using Microsoft Visual C# 2008 Express Edition. I have a listbox where I have a list of urls. And I have two textboxes on the form, one for username and one for password. What I want to do is: when I click an item in the listbox, the two textboxes will update with predefined values. For example, I have the following url in the listbox: "http://mail.google.com", when I click that url once the application is running, the username and password areas should be updated like "username" and "password" which are predefined. Is this possible?
Are you building a WinForms app or ASP.net thing? In both cases I think you'd need to subscribe to a selection change event of the listbox control: SelectedIndexChanged. Then you can put your textbox update code in the handler of this event.
This is a Windows Forms Application, I got what you suggested but do not know exactly how to implement it. Maybe an example would help much in this case.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { switch (listBox1.SelectedIndex) { case 0: textBox1.Text = "user1"; textBox2.Text = "pass1"; break; case 1: textBox1.Text = "user2"; textBox2.Text = "pass2"; break; case 2: textBox1.Text = "user3"; textBox2.Text = "pass3"; break; default: textBox1.Text = ""; textBox2.Text = ""; break; } } Code (markup):
This was exactly what I was looking for, thank you very much NeoCambell. Now I have one last thing with my application, to submit this info(username, password) on to the webpage. I am working on it now. EDIT: Is it possible to define the cases by the urls? For example, instead of having case 0, case 1, case 2 and so on, is it possible to have case url1, case url2, case url3 and so on.
Hi, Just short notice - try avoiding switch-case structure in object-oriented programming. It's not a good practice to use them
Hi.. In the other way beside swtch case, you can use If - Else If - Else condition to solve your problem in same SelectedIndex_Changed Event. But depend or your problem and you convenient. Thanks.
Try this one... switch (listBox1.Text) { case "abc": textBox1.Text = "user1"; textBox2.Text = "pass1"; break; case "bcd": textBox1.Text = "user2"; textBox2.Text = "pass2"; break; case "cde": textBox1.Text = "user3"; textBox2.Text = "pass3"; break; default: textBox1.Text = ""; textBox2.Text = ""; break; } Code (markup):
Hope this make you easier. 1. First you need define entity class, where you will keep your Url, Username, Password. public class MyCredential { public string Url; public string Username; public string Password; } Code (markup): 2. Second you need variables (local class variable), where you will keep your data (url, username, and password) private List<MyCredential> _credentials; Code (markup): 3. Fill your variable with data on your form constructor, and loop your data to fill list box with urls. public MyForm() { _credentials = new List<MyCredential>(); _credentials.Add(new MyCredential { Url = "http://www.domain1.com", Username = "user1", Password = "pass1" }); _credentials.Add(new MyCredential { Url = "http://www.domain2.com", Username = "user2", Password = "pass2" }); _credentials.Add(new MyCredential { Url = "http://www.domain3.com", Username = "user3", Password = "pass3" }); _listbox.Items.Clear(); _credentials.ForEach(credential => { _listbox.Items.Add(credential.Url); }); } Code (markup): 4. Handle your list box selected index change event. void _listbox_SelectedIndexChanged(object sender, EventArgs e) { int _index = (sender as ListBox).SelectedIndex; if ((_index >= 0) && (_index < _credentials.Count)) { textbox1.Text = _credentials[_index].Username; textbox2.Text = _credentials[_index].Password; } } Code (markup): What the benefit of this code: 1. It centralize your entity data, in case you need to modify your collection you just need modify data on your form constructor. You can copy/paste this code to add more data: _credentials.Add(new MyCredential { Url = "http://www.domain1.com", Username = "user1", Password = "pass1" }); Code (markup): 2. You don't need modify switch case on list box selected index change event. If you add more entity then you need add more case handler.
Thanks to all who contributed. I implemented an if else structure into the code as following: webBrowser1.Navigate(new Uri(listBox1.SelectedItem.ToString())); if ( (string)listBox1.SelectedItem == "..." ) { login.Text = "..."; password.Text = "..."; } else if ((string)listBox1.SelectedItem == "...) { login.Text = "..."; password.Text = "..."; } .... Code (markup): and it works fine. Now the next step is to pass this info on to the forms to be filled on the page. What it will do is simply when I click a button, it will get the login and password texts from the textboxes and send them to the login and password fields of the webpage. I am searching for how to do this but any help is greatly appreciated. Thanks