我正在制作一个Windows窗体我想要键入文本的链接,但我无法将我的链接分成多个帮助 [英] I Am Making A Windows Form Where I Want To Make Link Of Typing Text But I Am Unable To Split My Links Into Multiple Pls Help
本文介绍了我正在制作一个Windows窗体我想要键入文本的链接,但我无法将我的链接分成多个帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if (!string.IsNullOrEmpty(textBox1.Text))
{
string input = textBox1.Text;
string [] stringArray = input.Split(' 跨度>);
foreach ( var name in 输入)
{
LinkLabel label = new LinkLabel();
label.Click + = new System.EventHandler( this .lnkName_LinkClicked) ;
label.Text = textBox1.Text;
textBox1.Controls.Add(label);
解决方案
输入文字已被拆分
string [] stringArray = input.Split(' ,');
但你正试图循环......
foreach ( var name 在输入中)
名称
在输入中
。
所以,你需要遍历中的项目stringArray
;)
在你的代码中,你将LinkLabels插入到TextBox中,当你可以执行此操作时,正如您在此处的代码中看到的那样,并且,如果用户键入在带有LinkLabels的TextBox中,文本将环绕LinkLabels,我认为你可能会在视觉上结束一团糟。所以,我请你考虑把LinkLabel放在一个Control Designed 中,只是一个ContainerControl,就像在Form或Panel中一样。
假设您在主窗体上有一个TextBox'textBox1,'Form1,其'MultiLine属性设置为'true,此内容:google.com ,
codeproject.com,
ebay.co.uk
您在Form_Load EventHandler中执行此代码:
private void Form1_Load( object sender,EventArgs e)
{
string input = textBox1.Text;
string [] urlArray = input.Split( new char [] {' ,',' \ r',' \\\
'跨度>},StringSplitOptions.RemoveEmptyEntries);
for ( int i = 0 ; i < urlArray.Length; i ++)
{
string url = urlArray [i];
string urlFull = @& http:// www。 + url;
if (Uri.IsWellFormedUriString(urlFull,UriKind.Absolute))
{
LinkLabel newLLbl = new LinkLabel();
newLLbl.Text = url;
newLLbl.Links [ 0 ]。LinkData = urlFull;
newLLbl.LinkClicked + = newLLbl_LinkClicked;
newLLbl.AutoSize = true ;
textBox1.Controls.Add(newLLbl);
newLLbl.Location = new 点( 0 , 50 * i);
}
else
{
MessageBox.Show( 解析失败:{0}为有效链接!,url);
}
}
}
// let触发浏览器,交配
私有 void newLLbl_LinkClicked( object sender,LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}
if(!string.IsNullOrEmpty(textBox1.Text))
{
string [] inputLabels = textBox1.Text.Trim()。分割(',');
flowLayoutPanel1.Controls.Clear();
foreach(var inputLabel in inputLabels)
{
LinkLabel lblMessage = new LinkLabel();
lblMessage.AutoSize = true;
lblMessage.Text = inputLabel.Trim();
flowLayoutPanel1.Controls.Add(lblMessage) ;
if (!string.IsNullOrEmpty(textBox1.Text))
{
string input = textBox1.Text;
string[] stringArray = input.Split(',');
foreach (var name in input)
{
LinkLabel label = new LinkLabel();
label.Click += new System.EventHandler(this.lnkName_LinkClicked);
label.Text = textBox1.Text;
textBox1.Controls.Add(label);
解决方案
Input text has been splited
string[] stringArray = input.Split(',');
but you're trying to loop through the...
foreach (var name in input)
name
s ininput
.
So, you need loop through the items instringArray
;)
In your code you insert the LinkLabels into a TextBox, and, while you can do this, as you'll see in the code here, and, if the user types in the TextBox with the LinkLabels in it the text will wrap-around the LinkLabels, I think you are liable to end up with a mess, visually. So, I ask you to consider putting the LinkLabels inside a Control designed to be only a ContainerControl, like in a Form, or in a Panel.
Assume you have a TextBox 'textBox1 on a Main Form, 'Form1, with its 'MultiLine property set to 'true, and this content:google.com, codeproject.com, ebay.co.ukYou execute this code in the Form_Load EventHandler:
private void Form1_Load(object sender, EventArgs e) { string input = textBox1.Text; string[] urlArray = input.Split(new char []{',','\r','\n'},StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < urlArray.Length; i++) { string url = urlArray[i]; string urlFull = @&"http://www." + url; if (Uri.IsWellFormedUriString(urlFull, UriKind.Absolute)) { LinkLabel newLLbl = new LinkLabel(); newLLbl.Text = url; newLLbl.Links[0].LinkData = urlFull; newLLbl.LinkClicked += newLLbl_LinkClicked; newLLbl.AutoSize = true; textBox1.Controls.Add(newLLbl); newLLbl.Location = new Point(0, 50 * i); } else { MessageBox.Show("failed in parsing: {0} into valid link !", url); } } } // let fire the browser, mate private void newLLbl_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start(e.Link.LinkData.ToString()); }
if (!string.IsNullOrEmpty(textBox1.Text))
{
string[] inputLabels = textBox1.Text.Trim().Split(',');
flowLayoutPanel1.Controls.Clear();
foreach (var inputLabel in inputLabels)
{
LinkLabel lblMessage = new LinkLabel();
lblMessage.AutoSize = true;
lblMessage.Text = inputLabel.Trim();
flowLayoutPanel1.Controls.Add(lblMessage);
这篇关于我正在制作一个Windows窗体我想要键入文本的链接,但我无法将我的链接分成多个帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文