问题是在tn.Text中,字符串以数字和点/点开头,例如:
我需要tn.Text只包含文本,例如:
hello
hi
world
这是类代码:
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 ScrollLabelTest
{
public partial class DisplayResponses : Form
{
private List<string> nodesNames = new List<string>();
private List<TreeNode> CurrentNodeMatches = new List<TreeNode>();
public DisplayResponses()
{
InitializeComponent();
addmore();
}
public void addmore()
{
foreach (List<string> l_branch in ListsExtractions.responsers)
{
TreeNode l_node = treeView1.Nodes.Add(l_branch[l_branch.Count - 1]);
for (int l_count = 0; l_count < l_branch.Count - 1; l_count++)
{
l_node.Nodes.Add(l_branch[l_count]);
}
}
}
private void DisplayResponses_Load(object sender, EventArgs e)
{
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
txtName.Text = "";
txtParentName.Text = "";
txtText.Text = "";
txtTag.Text = "";
if (treeView1.SelectedNode.Name != null)
{
txtName.Text = treeView1.SelectedNode.Name.ToString();
}
if (treeView1.SelectedNode.Text != null)
{
txtText.Text = treeView1.SelectedNode.Text.ToString();
}
if (treeView1.SelectedNode.Tag != null)
{
txtTag.Text = treeView1.SelectedNode.Tag.ToString();
}
if (treeView1.SelectedNode.Parent != null)
{
txtParentName.Text = treeView1.SelectedNode.Parent.Text.ToString();
}
}
catch { }
}
private void treeView1_Click(object sender, EventArgs e)
{
ClearBackColor();
}
private void button1_Click(object sender, EventArgs e)
{
ClearBackColor();
try
{
TreeNode[] tn = treeView1.Nodes[0].Nodes.Find(txtNodeSearch.Text, true);
for (int i = 0; i < tn.Length; i++)
{
treeView1.SelectedNode = tn[i];
treeView1.SelectedNode.BackColor = Color.Yellow;
}
}
catch { }
}
private void ClearBackColor()
{
TreeNodeCollection nodes = treeView1.Nodes;
foreach (TreeNode n in nodes)
{
ClearRecursive(n);
}
}
private void ClearRecursive(TreeNode treeNode)
{
foreach (TreeNode tn in treeNode.Nodes)
{
tn.BackColor = Color.White;
ClearRecursive(tn);
}
}
private void button2_Click(object sender, EventArgs e)
{
ClearBackColor();
FindByText();
}
private void FindByText()
{
TreeNodeCollection nodes = treeView1.Nodes;
foreach (TreeNode n in nodes)
{
FindRecursive(n);
}
}
private void FindRecursive(TreeNode treeNode)
{
foreach (TreeNode tn in treeNode.Nodes)
{
int index = tn.Text.IndexOf(" ");
string text = tn.Text.Substring(0, index);
tn.Text = tn.Text.Replace(text, "").TrimStart();
if (tn.Text == this.txtNodeTextSearch.Text)
tn.BackColor = Color.Yellow;
FindRecursive(tn);
}
}
private void button3_Click(object sender, EventArgs e)
{
ClearBackColor();
FindByTag();
}
private void FindByTag()
{
TreeNodeCollection nodes = treeView1.Nodes;
foreach (TreeNode n in nodes)
{
FindRecursiveTag(n);
}
}
private void FindRecursiveTag(TreeNode treeNode)
{
foreach (TreeNode tn in treeNode.Nodes)
{
if (tn.Tag.ToString() == this.txtTagSearch.Text)
tn.BackColor = Color.Yellow;
FindRecursiveTag(tn);
}
}
}
}
编辑:
找到了一种方法来做到这一点,但它很慢,所以一切都冻结了太长时间:
private void FindRecursive(TreeNode treeNode)
{
foreach (TreeNode tn in treeNode.Nodes)
{
int index = tn.Text.IndexOf(" ");
string text = tn.Text.Substring(0, index);
tn.Text = tn.Text.Replace(text, "").TrimStart();
if (tn.Text == this.txtNodeTextSearch.Text)
tn.BackColor = Color.Yellow;
FindRecursive(tn);
}
}
如果在tn.Text例如我有:4。你好 然后我输入textBox2 hello并单击它将永远占用的按钮。
但如果我删除此代码:
int index = tn.Text.IndexOf(" ");
string text = tn.Text.Substring(0, index);
tn.Text = tn.Text.Replace(text, "").TrimStart();
然后它工作得很快但我需要输入4.你好 而且不仅是你好
为什么在使用IndexOf和Substring添加这部分代码时速度太慢?
答案 0 :(得分:4)
使用String.IndexOf查找前缀的最后一个字符(在上面的示例中,这将是句点与所需文本的第一个字符之间的空格),然后使用String.Substring来剪切出你想要的部分。
最好使用描述为&#34的单字符串形式的子字符串;子字符串从指定的字符位置开始并继续到字符串的末尾。&#34;如果您传递的位置超过了您找到的空间,那么Substring方法应返回您想要的部分,并且与其他形式相比可能会加快它的速度,因为您可以删除TrimStart和Replace方法调用。
答案 1 :(得分:1)
TrimStart是为此而做的:
TrimStart(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', ' ', });