Set BackGround Color of Textbox to Transparent - vb.net

I am trying to set the background color of textbox to transparent, to blend with my backcolor of my form.
I have tried the following below.
TextBox1.BackColor = Color.Transparent 'This doesn't work it stays white'
Is there something I am missing?

When I set TextBox.BackColor to Color.Transparent, it throws System.ArgumentException. I got this message :
Unvalid property value, The control does not support transparent colors background.

Hope am not late to the party, but this actually works for me. First create a class for the panel as below
Partial Public Class iPanel
Inherits Panel
Public Sub New()
SetStyle(ControlStyles.SupportsTransparentBackColor Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
BackColor = Color.Transparent
End Sub
End Class
Then create a RichTextBox (Instead of a Textbox) as below
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim CP As CreateParams = MyBase.CreateParams
CP.ExStyle = CP.ExStyle Or &H20
Return CP
End Get
End Property
Now compile the code and add the iRichTextBox inside the panel. Works for me

Private Sub TextBox1_Paint(sender As Object, e As PaintEventArgs) Handles TextBox1.Paint
TextBox1.ForeColor = Color.White
TextBox1.BackColor = Color.Transparent
End Sub
Instead of doing the first one, You can try this. Just put your code in Form Load
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.BackColor = color.(color of your Choice, same color of your background)
TextBox1.ForeColor = color.White
End Sub
As simple as that, it works for me

As far as I know textbox does not support transparent color property. But if you set the back color of the textbox to the same color as of its background component, still it can be considered as transparent.
How to do that - You can get the color name of the background component(in your case it is the form) and pass that name to the component which you want to be transparent.
Dim lname As String = Me.BackColor.ToString
Dim name As String = lname.Substring(7, lname.Length - 8)
txtbox1.BackColor = System.Drawing.Color.FromName(name)
Explanation -
The first line in the code gets you the name of the color, but there's a rub, it gets the name something like this - Color [Dark Orange] and we need only the name of the color i.e Dark Orange.
Thus the second line is to get the exact color name by removing this - Color [] part
And the last line to set that color same as of the background component color.
Hope it works, still have problem let me know...

Related

Combobox custom control with a lid

I have created a custom combobox with a label to cover the combobox (as it's very ugly) when it's not in use. The label, witch is the lid should show the display member. The covering and uncovering forks fine however the text on the label displayed is the previous value and not the current one. Passing over the label with the mouse, triggers the labels mouse enter event and than the display value is correct.
Here is the code for the custom control.
Public Class ComboBoxWithALid
Inherits ComboBox
Private WithEvents Lid As New Label With {.BackColor = Color.LightCyan, .ForeColor = Color.Black,
.TextAlign = ContentAlignment.MiddleCenter}
Protected Overrides Sub OnDataSourceChanged(ByVal e As EventArgs)
MyBase.OnDataSourceChanged(e)
Lid.Location = Location
Lid.Size = Size
Parent.Controls.Add(Lid)
Lid.BringToFront()
End Sub
Private Sub Lid_MouseEnter(sender As Object, e As EventArgs) Handles Lid.MouseEnter
Lid.SendToBack()
End Sub
Protected Overrides Sub OnMouseLeave(e As EventArgs)
MyBase.OnMouseLeave(e)se
Lid.Text = SelectedText
Lid.BringToFront()
End Sub
Protected Overrides Sub OnDropDownClosed(e As EventArgs)
MyBase.OnDropDownClosed(e)
Lid.BringToFront()
Lid.Text = SelectedText
End Sub
End Class
To test the control, drag the control from the tool box to your form and bind the control to any table you have
I tried to use text in place of selected text - same results
I found the solution. Change the move statement to:
Lid.Text = Items(SelectedIndex)(DisplayMember)
and is works.
SelectedText is for the highlighted portion of the text that is currently selected, not the selected item. You probably want something like this:
If SelectedIndex = -1 Then
Lid.Text = String.Empty
Else
Lid.Text = Items(SelectedIndex).ToString()
End If
Yes, you have an annoying control.

Setting a Button in a sub?

So, I have a little problem. Im making a application (in visual basic), and I have a way so that I can set a color when you hover over it. Now, I want to do this for all buttons, but make it a little easier by making a sub that can do this for me. The problem is, how can my sub tell which button to initialize the custom hover color? Here is my code.
Private Sub initButton(ByVal color As Color)
Button1.TabStop = False
Button1.FlatStyle = FlatStyle.Flat
Button1.FlatAppearance.BorderSize = 0
Button1.FlatAppearance.BorderColor = color
Button1.FlatAppearance.CheckedBackColor = color
Button1.FlatAppearance.MouseDownBackColor = color
Button1.FlatAppearance.MouseOverBackColor = color
End Sub
Now, how can I set Button1 to the button I want to initialize? Is there anyway to put that as an argument? If you find a answer, please reply.
As Plutonix suggested, using the Hover Event of the buttons would be the first logical choice. You will need to add a Parameter to your your Sub to pass in the button being hovered over.
Private Sub Button1_MouseHover(sender As Object, e As EventArgs)
Handles Button1.MouseHover,Button2.MouseHover 'add more buttons....
initButton(CType(sender, Button),Color.Blue)
End Sub
Private Sub initButton(hoverButton As Button, ByVal color As Color)
hoverButton.TabStop = False
hoverButton.FlatStyle = FlatStyle.Flat
hoverButton.FlatAppearance.BorderSize = 0
hoverButton.FlatAppearance.BorderColor = color
hoverButton.FlatAppearance.CheckedBackColor = color
hoverButton.FlatAppearance.MouseDownBackColor = color
hoverButton.FlatAppearance.MouseOverBackColor = color
End Sub

How to make a control to be painted/refreshed properly

I have a control derived from checkbook which I called "SettingBooleanButton", but when any window or dialog is dragged over the control the control keeps signs of the drag
The next image shows the effect of dragging an application window over control
This is the code block that I have for OnPaint()
Public Class SettingBooleanButton
Inherits CheckBox
Private _settingSection As String
Private _settingName As String
Private _associatedSetting As Setting
Public Event StateChange(ByVal affectedSetting As Setting)
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Appearance = Appearance.Button
FlatStyle = FlatStyle.Flat
TextAlign = ContentAlignment.MiddleCenter
AutoSize = False
End Sub
Public Property SettingSection As String
Get
Return _settingSection
End Get
Set(value As String)
_settingSection = value
End Set
End Property
Public Property SettingName As String
Get
Return _settingName
End Get
Set(value As String)
_settingName = value
End Set
End Property
''' <summary>
''' Sets a boolean value to indicate the initial checked state of the control.
''' </summary>
''' <value>
''' <c>true</c> to set it as [checked state]; otherwise, <c>false</c>.
''' </value>
Public Property CheckedState As Boolean
Get
Return Checked
End Get
Set(value As Boolean)
_associatedSetting = New Setting(_settingSection, _settingName, String.Empty)
RemoveHandler CheckedChanged, AddressOf StateChanged
Checked = value
SetText()
AddHandler CheckedChanged, AddressOf StateChanged
End Set
End Property
Private Sub StateChanged(sender As Object, e As EventArgs)
If IsNothing(_associatedSetting) Then
Return
End If
_associatedSetting.Value = Checked.ToString()
SetText()
RaiseEvent StateChange(_associatedSetting)
End Sub
Public Sub SetText()
If Checked Then
Font = New Font(Font.FontFamily, Font.Size, FontStyle.Bold)
ForeColor = Color.WhiteSmoke
Text = Resource.SettingBooleanButton_TrueState
Else
Font = New Font(Font.FontFamily, Font.Size, FontStyle.Regular)
ForeColor = SystemColors.ControlText
Text = Resource.SettingBooleanButton_FalseState
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
If Checked Then
ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Black, ButtonBorderStyle.Solid)
End If
End Sub
End Class
ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, ...)
Using e.ClipRectangle like this is a traditional bug in a Paint event handler. It is not a rectangle that matches the border you want to draw. It is only the part of the control that needs to be painted. Which is usually the entire control, but not always. Such as in your case when you drag a window across your control, only the part that is revealed needs to be repainted. So now you are painting the border in the wrong position, producing those black lines.
You only ever use the ClipRectangle if your painting code is expensive and you want to take the opportunity to skip that expensive code when it isn't needed anyway. Which is pretty rare, clipping in Windows is already pretty efficient.
You'll need to pass the actual rectangle of your border. Fix:
ControlPaint.DrawBorder(e.Graphics, Me.ClientRectangle, _
Color.Black, ButtonBorderStyle.Solid)
Sometimes the simplest solutions (or causes) are overlooked.
I have a panel with 15 buttons on it and each has an image. Depending on rows selected from a data grid they all might be enabled or disabled.
It all worked fine except toggling between enabled and disabled was taking 2+ seconds and caused lag when multi-selecting from the data grid.
Tried a few things, then I thought maybe it was something to do with the images.
The images were all in an imagelist and size was set to 24,24 which was a compromise between 32,32 and 16,16. I changed the size in the imagelist to 32,32 as that is the native size of all the images... and shazam!!! All the buttons are basically rendered instantly now. No idea ATM whether being small PNG images makes a difference... but I'm going to convert all the images I have to ICO format.
Also... as all my buttons are on a panel I enable/disable the panel which in turn enables and disables all the children on it.

Button BackColor (seems to) ignore alpha channel

I built a little Tic-Tac-Toe game in winforms using nothing but layout panels, buttons and labels. Each game has 2 players, and each player is associated with a mark and color. When a player claims a field (clicks a button on a grid), that button's BackColor is changed to that player's color.
What I'd like to do now is have the open fields in the grid become a semi-transparent shade of a player's color while the cursor is over a field.
For some reason, this isn't working for my buttons:
Public Class FieldButton
Inherits Button
' ... Omitting for brevity '
Private _mouseIn As Boolean
Protected Overrides Sub OnMouseEnter(e As EventArgs)
MyBase.OnMouseEnter(e)
_mouseIn = True
End Sub
Protected Overrides Sub OnMouseLeave(e As EventArgs)
MyBase.OnMouseLeave(e)
_mouseIn = False
End Sub
Public Overrides Property BackColor As Color
Get
If Field.HasOwner Then
Return Field.Owner.Color
ElseIf _mouseIn Then
Return Color.FromArgb(16, Presenter.Game.CurrentPlayer.Color)
End If
Return MyBase.BackColor
End Get
Set(value As Color)
MyBase.BackColor = value
End Set
End Property
Private Shared ReadOnly FullPen As New Pen(Brushes.Black, 3)
Private Shared ReadOnly SemiTransparentPen As New Pen(Color.FromArgb(64, Color.Black), 3)
Protected Overrides Sub OnPaint(pevent As PaintEventArgs)
MyBase.OnPaint(pevent)
If Field.HasOwner Then
PaintMark(pevent.Graphics, pevent.ClipRectangle, Field.Owner.Mark, FullPen)
ElseIf _mouseIn And Not Presenter.Game.IsGameOver Then
PaintMark(pevent.Graphics, pevent.ClipRectangle, Presenter.Game.CurrentPlayer.Mark, SemiTransparentPen)
End If
End Sub
' ... '
End Class
In the above code, Field is another object that represents a field in the grid. Each field has an Owner, which is set to the player that claimed the field (or null).
Anyway, the line that is supposed to do the magic:
Return Color.FromArgb(16, Presenter.Game.CurrentPlayer.Color)
Has the following result:
It might be a little difficult to see due to the illusion created by the semi-transparent mark, but the button background color FromArgb(16, ...) is exactly the same as the buttons with alpha channel 255.
What am I not doing right?
EDIT
Turns out that the button's FlatButtonAppearance.MouseOverBackColor property takes precedence over the button's BackColor when FlatStyle = FlatStyle.Flat.
I don't think that explains why my button was still showing purple on mouse-over. I'm guessing that MouseOverBackColor defaults to the current backcolor, but ignores the alpha channel.
Research says this should work for you, add it to your buttons constructor;
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true)
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx

Transparent control backgrounds on a VB.NET gradient filled form?

I'm filling the background of some VB.NET 2005 WinForms form with a nice pretty gradient fill (by overriding the OnPaint event). This works fine but the various labels etc on the form show with a solid background even after I set the BackColor to Color.Transparent. Setting the transparency key of the form itself seems to affect this but I cannot get the labels to have a truely transparent BackColor, is there an easy way to get around this or am I looking at custom controls etc?
Add a new class to your project and paste the code shown below. Build. Drop the new control from the top of your toolbox onto your form.
Public Class TransparentLabel
Inherits Label
Public Sub New()
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
End Sub
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H20 ' Turn on WS_EX_TRANSPARENT
Return cp
End Get
End Property
End Class
The flicker might be noticeable, no fix.
After some experimentation I've found that the following works for gradiant filling form backgrounds and preserving label transparency:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim formGraphics As Graphics = e.Graphics
Dim gradientBrush As New LinearGradientBrush(New Point(0, Height), New Point(0, 0), Me.AppSettings.FormGradiantFrom, Me.AppSettings.FormGradiantTo)
formGraphics.FillRectangle(gradientBrush, ClientRectangle)
End Sub
And in the form load event:
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or _
ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)