VB.NET Color Dialog Return String Value

Here recently I have been working more and more with Visual Studio again, and more specifically the Color Picker / Color Dialog control. For my purposes, I wanted the user to pick a color, and that color to then populate a text box in the form of an HTML compliant hex value, in other words, I need the result from the color picker as a string. Now, in VB.NET 2010 which is what I am using, the result from the color picker is in a format that is primed and ready to apply to a control, to change its fore or back round color. It does not give you a value in string format. So, I had to do a good bit of searching and work to figure this out.

Basically, what I ended up with was a function that you run the color picker response or value through. It’s pretty simple really:

Public Function Color2Html(ByVal MyColor As Color) As String
  Return "#" & MyColor.ToArgb().ToString("x").Substring(2).ToUpper
End Function

That’s it, that will return to you a hex color code formatted for HTML use. Just in case you are unsure of exactly how to use the function, this is the code I use with my color picker control (I use a standard button to fire it off with the result going into a text box):

Private Sub btnColor1_Click(sender As System.Object, e As System.EventArgs) Handles btnColor1.Click
  Dim NewColor As New ColorDialog()
  NewColor.AllowFullOpen = True
  NewColor.ShowHelp = True
  If (NewColor.ShowDialog() = Windows.Forms.DialogResult.OK) Then
    txtColor.Text = Color2Html(NewColor.Color)
  End If
End Sub

That’s all there is too it. I hope this helps you get your string data from your Color Dialog / Color Picker control. Enjoy!

Visual Basic dot NET – Making a button the form default

Here is a quick tip for any and all of you VB slimming pills that work.NET heads out there.  Need to make a button on your form the default one to fire off when you hit the enter key (while that form has focus naturally)?  In the old days, it was a button property, and now it has become a form property.  It actually kind of makes sense when you think about it.  I mean, you can only have one default button on a form, right?  Anyway, go to the properties of the form in question and look for an option called “AcceptButton”.  This will be a drop down list comprised of all the buttons that have been placed on the form.  Pick the one you want as the default and there you have it, your button is ready to go!  Hope that helps …

See If A Directory Exists In Visual Basic

Hi there, how about a close cousin to yesterdays Visual Basic function?  In a previous post we showed you a function that will tell you whether or not a file does indeed exist.  Well, here is another similar function, but it tells you whether or not a directory exists.  This is another handy tool for all you code monkeys out there.  It works much the same way, check it out below:

Continue reading

See if a file exists in Visual Basic

Here is a handy little tip (and function) for all you Visual Basic developers out there.  I use this quite often, it’s useful to see if whatever file you are working with or looking for exists before you start trying to access it.  So, here we have a nice little function you can drop into a module and call from anywhere when you need it.

(Need to do the same for a directory? That function is here!)

Check it out:

Continue reading