如何把一个\\ n(换行符)列表框内? [英] How to put a \n(new line) inside a list box?

查看:94
本文介绍了如何把一个\\ n(换行符)列表框内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何把.selected项列表框中换行符(\\ n)?在这里,我有code它所产生的所有链接,但我想在列表框中所有这些环节,这些code为工作,但链接不会在新的生产线即将全部进来一行
我的code是:

  VAR链接= TextBox1.Text.Split(新的String [] {\\ n,\\ r},
StringSplitOptions.RemoveEmptyEntries);  的foreach(在链接VAR链接)
        {
            如果(!IsLinkWorking(链接))
            {
                //在这里,您可以显示错误。你不指定要如何证明这一点。
                TextBox2.ForeColor = System.Drawing.Color.Green;
               TextBox2.Text + =的String.Format({0} \\ n不是工作\\ n \\ n链接);
                //ListBox1.SelectedItem+=的String.Format({0} \\ n不是工作\\ n \\ n链接);
            }
            其他
            {
               // ListBox1.SelectedValue + =的String.Format({0} \\ n不是工作\\ n \\ n链接);
                TextBox2.Text + =的String.Format({0} \\ n工作\\ n \\ n链接);
            }串[]值= TextBox2.Text.Split(,);的foreach(在值字符串值)
{
   如果(value.Trim()==)
       继续;
   ListBox1.Items.Add(value.Trim());
       }
    }
}


解决方案

您code应该可以正常工作,我只能怀疑你没有正确分割字符串,你确定你有在你的字符串,分隔符,因为只有这样,你会看到在一行中的所有项目,因为分裂将产生只有一个单一的项目

刚刚尝试样品code和将多行添加的项目。

 字符串str =somelink1,somelink2,somelink3
串[]值= str.Split(,);的foreach(在值字符串值)
{
    如果(value.Trim()==)
        继续;
    ListBox1.Items.Add(value.Trim());
}

现在,如果由于某种原因,你最终字符串somelink1 somelink2 somelink3,没有逗号作为分隔符,你会得到你的数组中一个字符串,这将出现在一个单一的线路。

编辑:根据您编辑的问题。
你加入 \\ n ,然后你正在尝试将其分割有关逗号将拆分的说法应该是:

 的String []值= TextBox2.Text.Split(新[] {\\ n},StringSplitOptions.RemoveEmptyEntries);

How to put the newline(\n) in list box .selected item? Here I have code it generates the all links but I want to have all those link in listbox, these code is working but the links are not coming in the new line all comes in single line and my code is:

var links = TextBox1.Text.Split(new string[] { "\n", "\r" }, 
StringSplitOptions.RemoveEmptyEntries);

  foreach (var link in links)
        {
            if (!IsLinkWorking(link))
            {
                //Here you can show the error. You don't specify how you want to show it.
                TextBox2.ForeColor = System.Drawing.Color.Green; 
               TextBox2.Text += string.Format("{0}\nNot working\n\n ", link);
                //ListBox1.SelectedItem+= string.Format("{0}\nNot working\n\n ", link);
            }
            else
            {
               // ListBox1.SelectedValue += string.Format("{0}\nNot working\n\n ", link);
                TextBox2.Text += string.Format("{0}\n working\n\n", link);
            }

string[] values = TextBox2.Text.Split(',');

foreach (string value in values)
{
   if (value.Trim() == "")
       continue;
   ListBox1.Items.Add(value.Trim());    
       }
    }
}

解决方案

Your code should work fine, I can only suspect that you are not splitting the string properly, are you sure that you have , delimiter in your string , because only then you will see all the items in one line, since split would produce only a single item

Just try the sample code and it will add the items in multiple lines.

string str = "somelink1,somelink2,somelink3";
string[] values = str.Split(',');

foreach (string value in values)
{
    if (value.Trim() == "")
        continue;
    ListBox1.Items.Add(value.Trim());
}

Now if for some reason you end up with string "somelink1 somelink2 somelink3", without the comma as delimiter , you will get a single string in your array and that will appear in a single line.

EDIT: Based on your edited question. You are adding \n and then you are trying to split it on comma your split statement should be:

string[] values = TextBox2.Text.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

这篇关于如何把一个\\ n(换行符)列表框内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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