Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
this is my code to load the A-Z onto a treeview

C#
private void LoadTreeView(char start, char end)
  {
      treeView2.Nodes.Clear();
      for (char c = start; c <= end; c++)
      {
          TreeNode tn = new TreeNode(c.ToString());
          tn.Nodes.Add(new TreeNode("?"));
          treeView2.Nodes.Add(tn);
      }
  }


C#
private void Form1_Load(object sender, EventArgs e)
        {
            LoadTreeView('A', 'Z');
}


But I wanna know if i can do something like this

C#
if (treeView2.SelectedNode.Text == (LoadTreeView('A', 'Z')))
        {
//do something
        }


so if I select a nodes that's text is either A-Z it will do something, but I don't think I got that code right, on the (if) it doesn't work
Posted
Comments
Bala Selvanayagam 14-Apr-12 5:33am    
Did a copy from your code and pasted in visual stuidio and it seems to be working ( lists nodes from "A" to "Z" each with subnode "?")

Can you please refine you question with the exact error message and at what point it happens ?
VJ Reddy 14-Apr-12 6:45am    
I think Regex class can be used for this purpose as shown in Solution 4.

It definitely won't work like that - your LoadTreeView method returns a void (i.e. it returns nothing) and the if expression expects a string.

You would have to change your method quite a bit to get any sensible result - even if the whole test made any sense! How can the currently selected node be equal to a value you haven't created and loaded into your TreeView yet?
 
Share this answer
 
Comments
[no name] 14-Apr-12 6:10am    
I have already loaded loadtreeview in the formload and all that, what I am trying to do is when a click a letter in that treeview the letter A-Z it will do something
Hi,
You can use this code to test if selected node's text is between 'a' and 'z':
C#
string sn = treeView2.SelectedNode.Text.ToLower();
if (sn[0] >= 'a' && sn[0] <= 'z')
{
  //your code
}


I hope it helps,
Cheers
 
Share this answer
 
v2
Comments
VJ Reddy 14-Apr-12 6:44am    
Good answer. 5!
Reza Ahmadi 14-Apr-12 6:54am    
Thanks!
If the test is required to be done in a single statement, I think Regex class may be a better option.
The IsMatch method of System.Text.RegularExpressions.Regex class can be used as shown below:
C#
if (Regex.IsMatch(treeView2.SelectedNode.Text,"^[A-Z]$")) {
   //Do something
}
 
Share this answer
 
v2
C#
if (treeView2.SelectedNode.Text == "A" || treeView2.SelectedNode.Text == "B" || treeView2.SelectedNode.Text == "C" || etc)
        {
//do something
        }
 
Share this answer
 
Comments
[no name] 14-Apr-12 6:26am    
I know I can do it that way but it takes too long and takes up allot of typing and code space, simple = better
[no name] 14-Apr-12 6:29am    
Apparently you did not know that since you was comparing the node text to the result of a void function call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900