Tuesday, July 29, 2014

How can i remove/delete digits in the beginning of a string ?

This is what i tried to do now this is the completed class code:



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);
}
}
}
}

I added this part in the method FindRecursive:



int index = tn.Text.IndexOf(" ");
string text = tn.Text.Substring(0, index);
tn.Text = tn.Text.Replace(text, "").TrimStart();

When i'm using a breakpoint i see it does removing the digits and point as i wanted.


If tn.Text was: 4. hello


So now it's: hello


The problem is that when i added this part of code everything got very slow taking forever.


If i remove this part of code with the IndexOf and Substring it's working fast but then i need to type in textBox2 also the digits: 4. hello


And not only hello as i wanted.


No comments:

Post a Comment