Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi , i want to bind listbox items to some button on the same window form

More explanation : for example i have an item in the list box and i select it then i press a button then a link open in the user browser and for sure every item in the listbox has a different link binded to it .

how can i do this ?
Posted
Comments
Sergey Alexandrovich Kryukov 27-Jan-14 18:06pm    
If you say "ListBox", the question is: which one? Full type name, please. And will you please tell us: What have you tried so far?
—SA
BillWoodruff 27-Jan-14 19:34pm    
Is this Windows Forms ? Are you using a WebBrowser Control in your application ?

If it is, why have a Button; why not just trigger opening the link in the browser by the user selecting one of the ListBox Items ?
Rahul VB 29-Jan-14 12:46pm    
Let me rephrase every thing:
- You have items in a listbox.
- You select an item in the list box.
- Now a link corresponding to that item gets linked with a button.
- The moment you click the button, a website opens for that item.

Please correct me if i am wrong.
Talaat Ashraf 31-Jan-14 9:13am    
Yes , this is exactly what i want to do and sorry for the confusing question :)
Rahul VB 31-Jan-14 9:34am    
Please take care in future. Before posting a question rephrase it properly. Let me see what can be done :)

1 solution

Hello,

Firstly to display a web browser, you will have to add a webbrowser component to your Form.
To do that:

Quote:
1> Open the toolbox
2> Now go to the common controls section.
3> In that you will find the web browser component.
4> Using the mouse add it to the windows form.



The next thing is:

Quote:
1>Add a listbox to the windows form.
2> Also add a button to the windows form



Now look at the code below:

XML
public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }

      public void bind_listbox()
      {
          listBox1.DataSource = new List<string>() {"codeproject","google"}; ///populate the list box with the sites you want to surf

      }


      /// <summary>
      /// Please note that when you add a web browser component then a text area will appear on your form itself where you will be able to browse to different sites. As shown in the code below.
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void button1_Click(object sender, EventArgs e)
      {


          switch (button1.Text) ///using the switch case navigate to the web sites
          {
              case "codeproject":

                  webBrowser1.Navigate("http://www.codeproject.com/");
                break;

              case "google":
                webBrowser1.Navigate("https://www.google.co.in/");

              break;


          }



      }

      private void Form1_Load(object sender, EventArgs e)
      {
          bind_listbox(); ///binding the listbox on form load. However in case of larger number of entries, form loading may take time and application may become unresponsive.
      }

      private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
         button1.Text = listBox1.SelectedItem.ToString();  ////this event is fired when you select an item from the list box.
      }
  }



Now Try this out.

Please note:
I have mapped the web site browsing function to just one button. If you want you can do it using multiple buttons(according to what i read from your question).

Thanks,
 
Share this answer
 
v3
Comments
Talaat Ashraf 31-Jan-14 11:07am    
OMG ! that is awesome guide thanks a lot Rahul :))
Rahul VB 31-Jan-14 11:20am    
Dont thank me. I am no Hercules or Batman. I just googled about "how to add a browser component to a windows form". The rest is just switch case which any one can write down. What you need to learn is "what are listbox's events?"."How are they handled?". So brother start studying. And does this code work? if it doesnt then no use thanking me. I will try something else.
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900