添加item的时候设置tag属性,它是个object类型的属性;item.Tag=网址;

解决方案 »

  1.   

    感谢楼上的回复,我是个菜鸟,还请详细具体些,谢谢。
    我是想鼠标点击某item后输出对应的字母,比如鼠标点击数字4就msgbox输出d,十分感谢。using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;namespace Listbox
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void listBox1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(listBox1.SelectedItem.ToString());
            }        private void button1_Click(object sender, EventArgs e)
            {
                for (int i = 1; i <= 26; i++)
                {
                    this.listBox1.Items.Add(i);
                }
            }
        }
    }
      

  2.   

    顺便请教下那个ListItem能用在桌面程序上吗,总是说system.windows.dll不包含system.windows.documents。
    看这个方法貌似更简单些,求大神指点啊
      

  3.   


    for (int i = 1; i <= 26; i++)
                {
                  ListBoxItem item = new ListBoxItem ();
                 item.Tag ="i";
                    this.listBox1.Items.Add(i);
                }至于你第二个问题,你先去了解什么是ListBox控件,什么是ListBoxItem
      

  4.   

    谢谢,还有个问题啊,设置到tag了,怎么取出呢,在click方法里面显示。
      

  5.   

    实在不好意思,您给我那段代码没办法编译通过,增加了system.windows.controls命名空间和引入了PresentationFramework.dll还是报错。
    求给个完整的例子,万分感激
      

  6.   

    webform? webform要用别的方法。
      

  7.   

    顺手解答一下吧,如果是2.0的.net是没有listitem定义的,listbox里面是object的集合。
    object是什么这个我就不再教育了。
    自定义一个类        public class MyProperty {
                public string _showtext;
                public string _showurl;
                public string showtext { get{return _showtext;} set{_showtext=value;} }
                public string showurl { get{return _showurl;} set{_showurl=value;} }
                public override string ToString()
                {
                    return _showtext;
                }
            }后这样做:for (int i = 1; i <= 26; i++)
                {
                  MyProperty item = new MyProperty ();
                 item.showtext ="i";
                 item.showurl =url+"i";
                    this.listBox1.Items.Add(i);
                }
    读取的时候这样做:
    string url= ((MyProperty)listbox1.items[0]).showurl;
      

  8.   

    不是啊,我做的就是windows桌面程序,不是web的。
    下面这个累也适用吗?谢谢。
      

  9.   

    我用 string url=((MyProperty)NewsList.Items[0]).showurl 这个方法读取的时候编译器提示:
    无法将类型为“System.String”的对象强制转换为类型“webclient.MyProperty”。希望楼上老兄解答下哦,感谢
      

  10.   

    好吧,如果隐式转换不行咱么可以这样转换:
    object ob=NewsList.Items[0];
    string url=((MyProperty)ob).showurl
      

  11.   

    还有我上面的add写错了,                this.listBox1.Items.Add(item);