C# ListBox and textbox question.

Discussion in 'Programming' started by ademmeda, Mar 12, 2010.

  1. #1
    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?
     
    ademmeda, Mar 12, 2010 IP
  2. webrotate360

    webrotate360 Greenhorn

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    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.
     
    webrotate360, Mar 12, 2010 IP
  3. ademmeda

    ademmeda Active Member

    Messages:
    354
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    70
    #3
    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.
     
    ademmeda, Mar 12, 2010 IP
  4. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
            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):
     
    NeoCambell, Mar 13, 2010 IP
    ademmeda likes this.
  5. ademmeda

    ademmeda Active Member

    Messages:
    354
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    70
    #5
    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.
     
    Last edited: Mar 13, 2010
    ademmeda, Mar 13, 2010 IP
  6. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, you can do that.
     
    NeoCambell, Mar 14, 2010 IP
  7. nemanja.todic

    nemanja.todic Well-Known Member

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    130
    #7
    Hi,

    Just short notice - try avoiding switch-case structure in object-oriented programming. It's not a good practice to use them ;)
     
    nemanja.todic, Mar 14, 2010 IP
  8. umar_alatas

    umar_alatas Active Member

    Messages:
    116
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #8
    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.
     
    umar_alatas, Mar 16, 2010 IP
  9. ademmeda

    ademmeda Active Member

    Messages:
    354
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    70
    #9
    I am trying it but can not get it to work.

    So, what do you suggest?
     
    ademmeda, Mar 16, 2010 IP
  10. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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):
     
    NeoCambell, Mar 16, 2010 IP
  11. noface83

    noface83 Peon

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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.
     
    noface83, Mar 16, 2010 IP
    ademmeda likes this.
  12. ademmeda

    ademmeda Active Member

    Messages:
    354
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    70
    #12
    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
     
    ademmeda, Mar 17, 2010 IP