|
-
Jul 13th, 2007, 12:42 AM
#1
Thread Starter
Lively Member
[02/03] fontStyle.bold in a variable
I wish to use this statement:
Code:
Dim fnt As New Font(fntfam, fntSize, FontStyle.Bold, GraphicsUnit.Point)
However instead of using Fontstyle.bold I'd like to use a variable in its place so it can be italic or regular etc...
I want to do this:
Code:
Dim fntStyle As String = "fontstyle." & admHash("fontstyle")
where admHash("fontstyle") contains either bold. italic or regular but I get an error
I guess I can use a select case stmt if there is no alternative
-
Jul 13th, 2007, 01:05 AM
#2
Re: [02/03] fontStyle.bold in a variable
What you're trying to do there may seem convenient but it doesn't make sense. You're creating a String object that contains the text "fonstyle.bold" or the like. That has no relationship to a FontStyle value as is. The Font constructor requires a FontStyle value, not a String object. You cannot simply turn strings into other types like that. That's like writing the word "cat" on a piece of paper and expecting it to meow.
Some types provide a way for you to read a string and convert it to that type, and enumerations do that. You can convert the string "Bold" to a FontStyle like this:
vb.net Code:
Dim style As FontStyle = DirectCast([Enum].Parse(GetType(FontStyle), _
"Bold"), _
FontStyle)
That said, you're mcuh, much better off storing enumerated values as Integers rather than Strings. That's because the underlying type of basically all enumerations is Integer. For instance, the numeric value of FontStyle.Bold is 1, while FontStyle.Italic is 2. Try this:
vb.net Code:
Dim style As FontStyle = FontStyle.Regular
MessageBox.Show("style = " & style.ToString())
style = FontStyle.Bold Or FontStyle.Italic
MessageBox.Show("style = " & style.ToString())
Dim int As Integer = CInt(style)
MessageBox.Show("int = " & int)
Dim newStyle As FontStyle = DirectCast(int, FontStyle)
MessageBox.Show("newStyle = " & newStyle.ToString())
That shows how you can just cast back and forth between Integer and an enumerated type, assuming that the Integer value corresponds to a valid value of that type.
-
Jul 13th, 2007, 09:16 AM
#3
Thread Starter
Lively Member
Re: [02/03] fontStyle.bold in a variable
 Originally Posted by jmcilhinney
What you're trying to do there may seem convenient but it doesn't make sense. You're creating a String object that contains the text "fonstyle.bold" or the like. That has no relationship to a FontStyle value as is. The Font constructor requires a FontStyle value, not a String object. You cannot simply turn strings into other types like that. That's like writing the word "cat" on a piece of paper and expecting it to meow.
Some types provide a way for you to read a string and convert it to that type, and enumerations do that. You can convert the string "Bold" to a FontStyle like this:
vb.net Code:
Dim style As FontStyle = DirectCast([Enum].Parse(GetType(FontStyle), _
"Bold"), _
FontStyle)
That said, you're mcuh, much better off storing enumerated values as Integers rather than Strings. That's because the underlying type of basically all enumerations is Integer. For instance, the numeric value of FontStyle.Bold is 1, while FontStyle.Italic is 2. Try this:
vb.net Code:
Dim style As FontStyle = FontStyle.Regular
MessageBox.Show("style = " & style.ToString())
style = FontStyle.Bold Or FontStyle.Italic
MessageBox.Show("style = " & style.ToString())
Dim int As Integer = CInt(style)
MessageBox.Show("int = " & int)
Dim newStyle As FontStyle = DirectCast(int, FontStyle)
MessageBox.Show("newStyle = " & newStyle.ToString())
That shows how you can just cast back and forth between Integer and an enumerated type, assuming that the Integer value corresponds to a valid value of that type.
I tried to run your example but I get the error:
Code:
'DirectCast' operand must have a reference type, but 'Integer' is a value type.
Also, what I am trying to do is save many of the properties of a listview that are changeable at runtime. Is there an easier way to save the state of a control?
Update: Yes, I do use search once in awhile I found this
http://www.vbforums.com/showthread.p...89#post2941189
but again it is for 2005 version - I think I need to upgrade. There is a databindings window in 02 but nothing was showing in the drop downs and I dont really know how to use it.
Last edited by jeffnyc; Jul 13th, 2007 at 10:53 AM.
-
Jul 13th, 2007, 10:12 PM
#4
Re: [02/03] fontStyle.bold in a variable
Change DirectCast to CType and it should work fine.
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
|