Results 1 to 17 of 17

Thread: [RESOLVED] String.Format 2 digits always

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Resolved [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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: String.Format 2 digits always

    Have you tried using String.Format instead of just Format ?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: String.Format 2 digits always

    Yes, I've also tried FormatNumber to no avail.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: String.Format 2 digits always

    Have a look here: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx if you haven't already...

  6. #6

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: String.Format 2 digits always

    Best I could come up with I'm afraid...

    vb Code:
    1. If IsNumeric(Hex(colorbox.Color.G)) Then
    2.             MessageBox.Show(CInt(Hex(colorbox.Color.G)).ToString("0#"))
    3. Else
    4.             MessageBox.Show(Hex(colorbox.Color.G))
    5. 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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: String.Format 2 digits always

    This is a shot in the dark, but have you tried using .ToString("x2") instead of Hex() ?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String.Format 2 digits always

    Quote Originally Posted by chris128 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. colorbox.Color.G.ToString("X2")
    Seems to work perfectly
    Last edited by chris128; May 29th, 2009 at 04:26 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String.Format 2 digits always

    Quote Originally Posted by chris128 View Post
    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).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: String.Format 2 digits always

    Quote Originally Posted by jmcilhinney View Post
    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")
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: String.Format 2 digits always

    Quote Originally Posted by chris128 View Post
    Oh, I thought ColorBox.Color.G was a Byte...

    EDIT: well looks like you are right as always!

    vb.net Code:
    1. 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
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width