Setting a Button in a sub? - vb.net

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

Related

How to avoid triggering MouseLeave event when hovering over a child control?

I am trying to make it so if you hover into a panel, it will change its color, and when you hover into a label, the panel will not change its color or go back to default, let me show pictures of what I am trying to explain:
If you didn't hover into anything, it would be at its default state:
If you hovered into it, panel color will change:
Then, if you hover into the label, panel color shouldn't change:
But instead, what it does is if you hover into the label, the panel color goes back to what its default was. This is my code:
Private Sub Panel13_MouseEnter(sender As Object, e As EventArgs) Handles Panel13.MouseEnter
Panel13.BackColor = Color.DarkGreen
End Sub
Private Sub Panel13_MouseLeave(sender As Object, e As EventArgs) Handles Panel13.MouseLeave
Panel13.BackColor = Color.LimeGreen
End Sub
How can I fix this problem? Is there any way to make the code clean or do I have to copy and paste the code and replace words?
You may use the GetChildAtPoint method to check if the mouse cursor is over a child control.
Replace the code in your MouseLeave event handler with the following:
Dim childControl = Panel13.GetChildAtPoint(Panel13.PointToClient(Cursor.Position))
If childControl Is Nothing Then
Panel13.BackColor = Color.LimeGreen
End If

programmatically select and highlight a row of a ListView in VB.NET

I want to do something seemingly simple - programmatically select and highlight a row of a ListView in VB.NET.
VB.NET: How to dynamically select a list view item?
tells me what should to be all that is needed, but it isn't. The row is selected, but not highlighted.
http://vbcity.com/forums/t/28260.aspx
tells me about the "HideSelection" property and the .Focus() method (also referenced at Select programmatically a row of a Listview), which sounded hopeful, but the best I can get is the faint highlight mentioned, I want the full monty. I tried a Noddy example with just a ListView, in Details mode, FullRowSelection = true, HideSelection = False, one columnheader defined and then
ListView1.Items.Add("Red")
ListView1.Items.Add("Orange")
ListView1.Items.Add("Yellow")
ListView1.Items.Add("Green")
ListView1.Items(2).Selected = True
I get this
but I want this
I can simulate highlighting by adding these lines
ListView1.SelectedItems(0).BackColor = Color.CornflowerBlue
ListView1.SelectedItems(0).ForeColor = Color.White
but then how can I be sure to undo the artificial highlight if the row can be implicitly as well as explicitly deselected? Do I have to think of all the possible cases? That's too much work for what should be a simple operation. Plus, since I want to color-code my rows, there is an additional challenge that when I undo the highlight color, I have to figure out what color is appropriate at that point. Is there a better, simpler way?
You can access the Graphics object used to draw each item, and draw them yourself.
Make a new project with a Button and ListView. Paste the following code:
Form_Load to use multiple subitems
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ListView1.OwnerDraw = True ' or else can't handle DrawItem event
ListView1.Columns.Add("ColumnHeader1")
ListView1.Columns.Add("ColumnHeader2")
ListView1.Columns.Add("ColumnHeader3")
Me.ListView1.Items.Add("Red")
Me.ListView1.Items.Add("Orange")
Me.ListView1.Items.Add("Yellow")
Me.ListView1.Items.Add("Green")
ListView1.Items(0).SubItems.Add("Strawberry")
ListView1.Items(0).SubItems.Add("Apple")
ListView1.Items(1).SubItems.Add("Pepper")
ListView1.Items(1).SubItems.Add("Apricot")
ListView1.Items(2).SubItems.Add("Plum")
ListView1.Items(2).SubItems.Add("Banana")
ListView1.Items(3).SubItems.Add("Apple")
ListView1.Items(3).SubItems.Add("Lime")
End Sub
Three handlers for the ListView's drawing related events. Code copied from this answer
Private Sub listView1_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView1.DrawColumnHeader
e.DrawDefault = True
End Sub
Private Sub listView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
Const TEXT_OFFSET As Integer = 1
' I don't know why the text is located at 1px to the right. Maybe it's only for me.
Dim listView As ListView = DirectCast(sender, ListView)
' Check if e.Item is selected and the ListView has a focus.
If Not listView.Focused AndAlso e.Item.Selected Then
Dim rowBounds As Rectangle = e.SubItem.Bounds
Dim labelBounds As Rectangle = e.Item.GetBounds(ItemBoundsPortion.Label)
Dim leftMargin As Integer = labelBounds.Left - TEXT_OFFSET
Dim bounds As New Rectangle(rowBounds.Left + leftMargin, rowBounds.Top, If(e.ColumnIndex = 0, labelBounds.Width, (rowBounds.Width - leftMargin - TEXT_OFFSET)), rowBounds.Height)
Dim align As TextFormatFlags
Select Case listView.Columns(e.ColumnIndex).TextAlign
Case HorizontalAlignment.Right
align = TextFormatFlags.Right
Exit Select
Case HorizontalAlignment.Center
align = TextFormatFlags.HorizontalCenter
Exit Select
Case Else
align = TextFormatFlags.Left
Exit Select
End Select
TextRenderer.DrawText(e.Graphics, e.SubItem.Text, listView.Font, bounds, SystemColors.HighlightText, align Or TextFormatFlags.SingleLine Or TextFormatFlags.GlyphOverhangPadding Or TextFormatFlags.VerticalCenter Or TextFormatFlags.WordEllipsis)
Else
e.DrawDefault = True
End If
End Sub
Private Sub listView1_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListView1.DrawItem
Dim listView As ListView = DirectCast(sender, ListView)
' Check if e.Item is selected and the ListView has a focus.
If Not listView.Focused AndAlso e.Item.Selected Then
Dim rowBounds As Rectangle = e.Bounds
Dim leftMargin As Integer = e.Item.GetBounds(ItemBoundsPortion.Label).Left
Dim bounds As New Rectangle(leftMargin, rowBounds.Top, rowBounds.Width - leftMargin, rowBounds.Height)
e.Graphics.FillRectangle(SystemBrushes.Highlight, bounds)
Else
e.DrawDefault = True
End If
End Sub
Button click handler to simulate item(2) selected
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.ListView1.Items(2).Selected = True
End Sub
This will draw the background color regardless of focus. You have a lot of control over other colors and fonts going this route too.
Here, the button has been clicked, to select item 2, while the button still has focus, and item 2 is selected.
Easiest thing,
Just allocate the LST_ItemIndex = lstList.FocusedItem.Index everytime you select a different item
and then fire the below whenever you want the highlight
If lstList.Items.Count > 0 Then
lstList.Items(LST_ItemIndex).Selected = True
lstList.Items(LST_ItemIndex).EnsureVisible()
End If

Set BackGround Color of Textbox to Transparent

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...

VB pop color picker

I have a question of VB event handler and color picker.
Now I have a label, and I want when user click it, it pops up a color picker dialog and let user to change the background color of the label.
Not sure how to implement this, can anyone give me a direction?
Thank you
Use the .NET Framework's ColorDialog class in your Label's click handler.
Private Sub Label1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Label1.Click
Dim cDialog As New ColorDialog()
cDialog.Color = Label1.BackColor ' initial selection is current color.
If (cDialog.ShowDialog() = DialogResult.OK) Then
Label1.BackColor = cDialog.Color ' update with user selected color.
End If
End Sub
here is the way to do it:
ColorDialog1.ShowDialog()
paperLabel.BackColor = Me.ColorDialog1.color

How to change textbox border color and width in winforms?

I would like to know how do I change the border color and border width of textbox as something shown below
If it is mouse hover I need to display one colour and on mouse down I need to display another colour.
Can anyone explain me the detailed process with the source if available.
You could do the following:
Place the TextBox inside a Panel
Give the panel 1 pixel padding
Set the text dock to Fill
Make the text box to have no border
Then, handle mouse events on the text box, switch the background color of the panel between your two colors, when the mouse enters/leaves.
This isn't the most elegant approach in terms of using resources/handles but it should work without any custom drawing.
Same as above with a little twist. Unfortunately I can't comment due to reputation.
Make a UserControl
Set usercontrol padding on all to 1
Put a Panel inside the usercontrol
Set panel dock style to fill
Set panel padding to 6, 3, 6, 3 (left, top, right, bottom)
Put a TextBox inside the panel
Set textbox dock style to fill
Set textbox borderstyle to None
...then for border colour changing properties, you could use this
Dim tbxFocus As Boolean = False
Private Sub tbx_GotFocus(sender As Object, e As EventArgs) Handles tbx.GotFocus
tbxFocus = True
Me.BackColor = Color.CornflowerBlue
End Sub
Private Sub tbx_LostFocus(sender As Object, e As EventArgs) Handles tbx.LostFocus
tbxFocus = False
Me.BackColor = SystemColors.Control
End Sub
Private Sub tbx_MouseEnter(sender As Object, e As EventArgs) Handles tbx.MouseEnter
If tbxFocus = False Then Me.BackColor = SystemColors.ControlDark
End Sub
Private Sub tbx_MouseLeave(sender As Object, e As EventArgs) Handles tbx.MouseLeave
If tbxFocus = False Then Me.BackColor = SystemColors.Control
End Sub
It's pretty self-explanatory.