如何使用某些按钮绑定列表框项? [英] How to bind Listbox items with certain buttons ?

查看:76
本文介绍了如何使用某些按钮绑定列表框项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将列表框项目绑定到同一窗口窗体上的某个按钮



更多解释:例如我在列表框中有一个项目,我选择它然后我按下一个按钮,然后在用户浏览器中打开一个链接,并确保列表框中的每个项目都有一个绑定到它的不同链接。



我怎么能这样做?

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 ?

推荐答案

您好,



首先要显示网页浏览器,您必须添加表格的webbrowser组件。

为此:



Hello,

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

Quote:

1>打开工具箱

2>现在转到公共控制部分。

3>在那里你会找到网络浏览器组件。

4>使用鼠标将其添加到Windows窗体中。

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>在Windows窗体中添加一个列表框。

2> ;另外在窗体上添加一个按钮

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







现在看下面的代码:






Now look at the code below:

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,


这篇关于如何使用某些按钮绑定列表框项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆