2012-10-18 24 views
-2

这是代码我如何通过项目间移动,并显示在一个标签:如何使用SelectedIndexChanged事件在列表框中移动项目并仅选择部分项目?

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      label4.Text = listBox1.SelectedItem.ToString(); 
     } 

但如果我有3个项目,例如:

Url: http://www.cnet.com --- Localy KeyWord: cnet 
Url: http://www.google.com --- Localy KeyWord: google 
Url: http://www.microsoft.com --- Localy KeyWord: microsoft 

然后我想在展示label4只有每个项目的url,如果im在label4中的第一个项目上,我将只会看到http://www.cnet.com如果im在第二个项目上,那么label4将仅显示http://www.google.com等等......在label4中仅显示网站地址部分。

+0

你的url的格式是否为常量?什么后缀是允许的?这可以很容易地通过正则表达式来解决。 –

+0

[你试过了什么?](http://www.WhatHaveYouTried.com) – JDB

+0

乔丹的URL在列表框中是不变的,用户可以用与上面相同的格式项添加新的URL到列表框中,但列表框中的URL不能改变。任何后缀我的意思是在列表框中的网址可能包含http://20%www.google.com043554TRtytr我想从列表框中只有Url:和Localy KeyWord:google之间的部分,所以在这个exmaple中的label4它会是http://www.google.com,但它可以是任何类型的网址。 – user1741587

回答

0

解决了很长的路我猜,但它的工作:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 

      if (listBox1.SelectedItem != null) 
      { 
       label4.Text = listBox1.SelectedItem.ToString(); 

       string startTag = "Url: "; 
       string endTag = " ---"; 
       int startTagWidth = startTag.Length; 
       int endTagWidth = endTag.Length; 
       int index = 0; 
       index = label4.Text.IndexOf(startTag, index); 
       int start = index + startTagWidth; 
       index = label4.Text.IndexOf(endTag, start + 1); 
       string g = label4.Text.Substring(start, index - start); 
       label4.Text = g; 
      } 
     } 

感谢。

相关问题