|
-
May 28th, 2009, 06:21 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] String.Format 2 digits always
For some reason I can't figure this out. Here's my code.
Code:
colorTextbox.Text = Format(Hex(ColorBox.Color.G), "00")
What I want it to do is format the Green Hex number to be "00" instead of just "0" when the color is something like black.
This has proven to be very stressful and this Format command makes no sense to me anymore (worked differently in 2005). For one, whatever I put in the quotes ("00") is what comes out in the text box. But according to the command parameters, "00" should be the format, not the actual text! So then I tried any string format I could think of (## etc.) along with switching the order and I STILL can't figure this out.
I'm not sure what I'm not getting but this makes no sense. I've google, used the MSDN Library, and tried all sorts of stuff but I can't gain anything on this problem.
I know I can go the long way and make an if = 0 then = "00" type statement but I swear there's a format function to do it, I just can't figure it out.
-
May 28th, 2009, 06:25 PM
#2
Re: String.Format 2 digits always
Have you tried using String.Format instead of just Format ?
-
May 28th, 2009, 06:28 PM
#3
Thread Starter
Fanatic Member
Re: String.Format 2 digits always
Yes, I've also tried FormatNumber to no avail.
-
May 28th, 2009, 06:31 PM
#4
Re: String.Format 2 digits always
OK can you just give me a couple of examples of what the output should be (if it was working properly) for specific colours?
-
May 28th, 2009, 06:31 PM
#5
Re: String.Format 2 digits always
-
May 28th, 2009, 06:45 PM
#6
Thread Starter
Fanatic Member
Re: String.Format 2 digits always
@ForumAccount
That works, if Hex() returned an integer. But I can't use ToString on a String, and if I try to use Hex(blahblah).Format("00") it gives me an Enum blah blah Warning saying expression will not be evaluated, and it doesn't work. It says I should use String.Format("00"), but then it doesn't tell me where to put the string that I want to format!
Here's a shortened example:
Code:
Dim str as String = "1"
When I use:
Code:
String.Format(str, "00")
I want it to return 01 not 1.
And just in case I'm going about this the wrong way. I basically just need to ad a 0 to the front of the string if it is only 1 character. So even if the string is just "F" I need it to format to "0F".
Still I've been testing Format with just a number and it still doesn't work. But if there is any easier way to do the above then that's what I'm looking for.
EDIT: final thing, heres my old code
Code:
colorLabel.BackColor = ColorBox.Color
Dim r As String = Hex(ColorBox.Color.R)
Dim g As String = Hex(ColorBox.Color.G)
Dim b As String = Hex(ColorBox.Color.B)
If r.Length = 1 Then r = "0" & r
If g.Length = 1 Then g = "0" & g
If b.Length = 1 Then b = "0" & b
colorTextbox.Text = r & g & b
Basically I have a label with a color and I'm trying to get the Hex value of that color. I was hoping to use Format to shorter the part about adding 0s if its only 1 character. Is there any other way?
Last edited by Vectris; May 28th, 2009 at 06:53 PM.
-
May 28th, 2009, 06:54 PM
#7
Re: String.Format 2 digits always
Best I could come up with I'm afraid...
vb Code:
If IsNumeric(Hex(colorbox.Color.G)) Then
MessageBox.Show(CInt(Hex(colorbox.Color.G)).ToString("0#"))
Else
MessageBox.Show(Hex(colorbox.Color.G))
End If
I know. I suck.... but I cant see any other way you can do it because you cant treat a string as an integer if you are not sure whether or not the string is going to contain numbers or not... hence the IsNumeric 
EDIT: OK just seen your update and obviously my code isnt what you want as I didnt realise you wanted to put a 0 infront of the string even if it was a letter and not a number.
Last edited by chris128; May 28th, 2009 at 07:00 PM.
-
May 28th, 2009, 10:49 PM
#8
Re: String.Format 2 digits always
This is a shot in the dark, but have you tried using .ToString("x2") instead of Hex() ?
-
May 28th, 2009, 10:55 PM
#9
Thread Starter
Fanatic Member
Re: String.Format 2 digits always
No, and I may try it later, but Hex() converts a number to hexadecimal and "x2" in tostring would just format it differently, not conver it... Or is does the "x" also call for a conversion? I'll try it tomorrow, going to bed now.
Thanks all for the suggestions, as of now I'm going with the code in post 6
-
May 28th, 2009, 11:00 PM
#10
Re: String.Format 2 digits always
This is from the MSDN documentation for Standard Numeric Format Strings:
This format is supported only for integral types. The number is converted to a string of hexadecimal digits. The case of the format specifier indicates whether to use uppercase or lowercase characters for the hexadecimal digits greater than 9. For example, use 'X' to produce "ABCDEF", and 'x' to produce "abcdef".
The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.
The following example formats Int32 values with the hexadecimal format specifier.
Code:
Dim value As Integer
value = &h2045e
Console.WriteLine(value.ToString("x"))
' Displays 2045e
Console.WriteLine(value.ToString("X"))
' Displays 2045E
Console.WriteLine(value.ToString("X8"))
' Displays 0002045E
value = 123456789
Console.WriteLine(value.ToString("X"))
' Displays 75BCD15
Console.WriteLine(value.ToString("X2"))
' Displays 75BCD15
Seek and ye shall find. Don't seek and ye might be lucky and have somebody provide the correct answer after more than four hours.
-
May 29th, 2009, 03:58 AM
#11
Re: String.Format 2 digits always
JMC - I'm pretty sure that doesnt work in this scenario because that only works for integers, unless im missing something (which is probably the case). The Hex function that Vectris is using returns a String, so you cant use ToString in the way you have mentioned
-
May 29th, 2009, 04:16 AM
#12
Re: String.Format 2 digits always
 Originally Posted by chris128
JMC - I'm pretty sure that doesnt work in this scenario because that only works for integers, unless im missing something (which is probably the case). The Hex function that Vectris is using returns a String, so you cant use ToString in the way you have mentioned
ColorBox.Color.G is an Integer. As JB said, ToString is called instead of Hex. The "conversion" to hexadecimal is part of the format. That's what that doco says:
The number is converted to a string of hexadecimal digits.
-
May 29th, 2009, 04:21 AM
#13
Re: String.Format 2 digits always
Oh, I thought ColorBox.Color.G was a Byte...
EDIT: well looks like you are right as always! 
vb.net Code:
colorbox.Color.G.ToString("X2")
Seems to work perfectly
Last edited by chris128; May 29th, 2009 at 04:26 AM.
-
May 29th, 2009, 05:05 AM
#14
Re: String.Format 2 digits always
 Originally Posted by chris128
Oh, I thought ColorBox.Color.G was a Byte...
You are quite correct. I didn't check that. It doesn't matter though because format strings that require integers will work for all integer types, i.e. Byte, Short (Int16), Integer (Int32) and Long (Int64).
-
May 29th, 2009, 06:44 AM
#15
Thread Starter
Fanatic Member
Re: String.Format 2 digits always
 Originally Posted by jmcilhinney
Don't seek and ye might be lucky and have somebody provide the correct answer after more than four hours.
If you read the first post I did seek. I'm just not familiar with formatting strings so I didn't seek the right stuff.
Thanks for the code though, it works fine.
Code:
casettecolorTextbox.Text = ColorBox.Color.R.ToString("X2") & ColorBox.Color.G.ToString("X2") & ColorBox.Color.B.ToString("X2")
-
May 29th, 2009, 07:16 AM
#16
Re: [RESOLVED] String.Format 2 digits always
The documentation for the Format function, which you are already using, includes a link to a topic named "Predefined Numeric Formats (Format Function)" that includes this:
X, or x
Displays number as a string that contains the value of the number in Hexadecimal (base 16) format. This option is supported for integral types (Byte, Short, Integer, Long) only.
Admittedly it doesn't mention that you can specify a length for the string. The Format documentation also provides a link to the String.Format documentation, which provides a link to the "Standard Numeric Format Strings" topic, which is where I got my original quote from.
-
May 29th, 2009, 04:48 PM
#17
Re: String.Format 2 digits always
 Originally Posted by chris128
Oh, I thought ColorBox.Color.G was a Byte...
EDIT: well looks like you are right as always!
vb.net Code:
colorbox.Color.G.ToString("X2")
Seems to work perfectly
Of course, JMC's always right
In this case so was I, though I'm not always right
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
|