How to convert Color to Hex and Hex to Color using C# and VB.NET

To convert a Color to Hex and vice versa using C# and VB.NET you can use the snippets below.

Sample C#

private static String ColorToHex(Color inputColor)
{
	return ColorTranslator.ToHtml(inputColor);
}

private static Color HexToColor(string hexInput)
{
	return ColorTranslator.FromHtml(hexInput);
}

Sample VB.NET (autoconverted)

Private Shared Function ColorToHex(inputColor As Color) As [String]
	Return ColorTranslator.ToHtml(inputColor)
End Function

Private Shared Function HexToColor(hexInput As String) As Color
	Return ColorTranslator.FromHtml(hexInput)
End Function

Please rate this snippet

How to change the BackgroundColor of a XtraGrid Grouprow using C# or VB.NET

To change the BackgroundColor of a XtraGrid Grouprow you need to use the Gridviews CustomDrawGroupRow Event.

Sample C#

private void gridView1_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e)
{
	var converter = new ColorConverter();
	var convertFromString = converter.ConvertFromString("#ebebeb");
	if (convertFromString != null)
	{
		e.Appearance.BackColor = (Color) convertFromString;
	}
}

Sample VB.NET

Private Sub gridView1_CustomDrawGroupRow(sender As Object, e As RowObjectCustomDrawEventArgs)
	Dim converter = New ColorConverter()
	Dim convertFromString = converter.ConvertFromString("#ebebeb")
	If convertFromString IsNot Nothing Then
		e.Appearance.BackColor = DirectCast(convertFromString, Color)
	End If
End Sub

Please rate this snippet

How to choose a color using ColorDialog in C# and VB.NET

To choose a color using ColorDialog in C# and VB.NET you can use the following snippet.

Sample C#

using (var colorDialog = new ColorDialog())
{
	if (colorDialog.ShowDialog() != DialogResult.Cancel)
	{
		var colorChosen = colorDialog.Color;
		textBox1.ForeColor = colorChosen;
		textBox1.BackColor = colorChosen;
	}
}

Sample VB.NET

Using colorDialog = New ColorDialog()
	If colorDialog.ShowDialog() <> DialogResult.Cancel Then
		Dim colorChosen = colorDialog.Color
		textBox1.ForeColor = colorChosen
		textBox1.BackColor = colorChosen
	End If
End Using

Please rate this snippet