|
-
May 9th, 2005, 12:00 AM
#1
Thread Starter
Frenzied Member
Enumeration Members [Resolved]
Can one loop through an enumeration in code? Or write something to get a random member of the enumeration?
For instance. I'm testing this treeview control. Got many borderstyles. So just want to add a button and when i click it, set the borderstyle to anoter on in the Borderstyle enumeration
Last edited by StrangerInBeijing; May 29th, 2005 at 11:07 AM.
Reason: RESOLVED
-
May 9th, 2005, 03:25 AM
#2
Re: Enumeration Members
you can use the getNames method to get a list of the items in an enum
Code:
public enum testE
{
ItemOne,
ItemTwo,
ItemThree
}
...
foreach(string s in Enum.GetNames(typeof(testE)))
Console.WriteLine(s);
...
-
May 9th, 2005, 03:46 AM
#3
Thread Starter
Frenzied Member
Re: Enumeration Members
Thanks DE, but I'm a bit stuck again.....
Code:
foreach (string s in Enum.GetNames(typeof (System.Web.UI.WebControls.BorderStyle)))
{
TreeView1.BorderStyle = ...what do in do here????
System.Threading.Thread.Sleep(1000);
}
-
May 9th, 2005, 04:35 AM
#4
Re: Enumeration Members
use the getNames to get a string array you can use the lenght of the array to determine the number of elements in the enum then while less than length:
TreeView1.BorderStyle++;
-
May 9th, 2005, 05:35 AM
#5
Re: Enumeration Members
sod that above this is nicer
Code:
foreach(string s in Enum.GetNames(typeof(BorderStyle))){
treeView1.BorderStyle = (BorderStyle)Enum.Parse(typeof(BorderStyle),s,true);
Thread.Sleep(1000);
}
-
May 9th, 2005, 05:37 AM
#6
Thread Starter
Frenzied Member
Re: Enumeration Members
Thanx a lot! I will try it out first thing in the AM.
Right now diggin into this asp.net 2 book (why do they always change things so much...grrrrrr)
Thanx again hey!
cheers
-
May 9th, 2005, 09:22 PM
#7
Thread Starter
Frenzied Member
Re: Enumeration Members
Yes, it works cool! I mean when I check the TreeView's borderstyle property when stepping through code.
Got another question about this, but don't know if it's relevant, as this Treeview is not the one in your toolbox, but one that the team I'm in are testing.
Q: How do you refresh the treeview after you changed the borderstyle? It does change in code, but you cannot see the change on your web page. There are no refresh method I'm used to in VB, and also I never did webpages....complete noob!
-
May 10th, 2005, 03:01 AM
#8
Re: Enumeration Members
Can't help you with that one, my knowledge of ASP.Net is zero.
-
May 10th, 2005, 03:03 AM
#9
Thread Starter
Frenzied Member
Re: Enumeration Members
welcome to the club.. ..gonna get me a normal desktop app job as soon as I get this C# under the knee!
But thanx a lot for your help.
-
May 17th, 2005, 03:41 AM
#10
Thread Starter
Frenzied Member
Re: Enumeration Members
It's been a while, but I wrote some stuff today that uses this.
Works sweet.
uuuuh....Now how do I convert a string (say "Dotted") back to the enumeration member (System.Web.UI.WebControls.BorderStyle.Dotted) ?
Hope it's not too easy, otherwise I'm look like an ass......nevermind....allready look like an ass
-
May 17th, 2005, 05:07 AM
#11
Thread Starter
Frenzied Member
Re: Enumeration Members
this does give me an error...
Code:
BorderStyle MyStyle = Enum.Parse(typeof (BorderStyle), sSel);
TreeView1.BorderStyle = MyStyle;
Error 2 Cannot implicitly convert type 'object' to 'System.Web.UI.WebControls.BorderStyle'. An explicit conversion exists (are you missing a cast?) c:\inetpub\wwwroot\WebPages\TreeView\BorderStyles.aspx.cs 28 27 http://localhost/WebPages/
-
May 17th, 2005, 05:32 AM
#12
Re: Enumeration Members
Well Eeyore,
The compiler asks "are you missing a cast?"
The Parse method returns an object which must be converted to the appropriate Enum.
Code:
BorderStyle MyStyle = (BorderStyle)Enum.Parse(typeof (BorderStyle), sSel);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|