Howto add a custom border to a FormBorderStyle=None - form? - vb.net

I have a form with the property FormBorderStyle set to 'None' and with a custom bar on top side for dragging and buttons.
Now I'd like to give the form a border because it's a child form and the parent form has the same background color as the child, so it's hard to see the child form.
And no, I can't/won't change the background color.
Help

There is a way without a need to set a background image and/or fixed sized form. So this is the most proper and simple way I guess. Say you have a form named Form1, all you need to do is:
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Black, ButtonBorderStyle.Solid)
End Sub
An alternative, if you want to use the default border provided by your Windows version:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
Me.Text = ""
Me.ControlBox = False
End Sub

You can use the Visual Basic .NET Power Packs which you can download here. It has this Control called LineShape that you can put onto your border-less form's edges, like this one program that I am currently working on.
The north border is just a LineShape with a BorderWidth set to 60 and the other borders' BorderWidths are set to 10.

Maybe you can use a BackgroundImage transparent except in the borders.

You can use this on the form paint event:
ControlPaint.DrawBorder(e.Graphics, Me.ClientRectangle, Color.Black, ButtonBorderStyle.Solid)
This will draw the client border only, also if you are resizing the form, or maximizing the form use Me.Refresh() on form resize events so that the form redraws its borders.

After seeing the answer by theGD, I did the same thing for a TableLayouPanel on a form:
Private Sub TableLayoutPanel1_Paint(sender As Object, e As PaintEventArgs) Handles TableLayoutPanel1.Paint
ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.DarkOrange, ButtonBorderStyle.Solid)
End Sub

Related

How to make transparent label in vb.net? [duplicate]

This question already has answers here:
Transparent control over PictureBox
(8 answers)
Closed 4 years ago.
I have a label which is on a picture. So I want to make transparent back color of the label. When I make backcolor property of the label to transparent. The picture doesn't showed through the text box back color. The backcolor gets color of the form.
Image
The background of a Lable is inherited from its container Background.
You can set a different Parent Container using the [Control].Controls.Add() method, which sets the Parent property to the new Container (you could also modify the .Parent property directly).
You can re-define your Label's Parent in the Form constructor (the Public Sub New() of a Form - insert the code after InitializeComponent()) or in the Form.Load() event handler.
Here I'm showing both, pick one:
Public Sub New()
InitializeComponent()
PictureBox1.Controls.Add(Label1)
'Re-define the Label.Location if required
Label1.Location = New Point(0, 0)
End Sub
The same thing in the Form.Load() event:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.Controls.Add(Label1)
End Sub
As an alternative, you could use the PictureBox .Paint() event's e.Graphics to simply Draw the same Text.
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Using TextBrush As SolidBrush = New SolidBrush(Me.ForeColor)
e.Graphics.DrawString("BITS Registration ID", Me.Font, TextBrush, New Point(5, 5))
End Using
End Sub

Best way to use transparent controls as invisible triggers

Once I develop a vb6 code to use transparent controls (Dont remember if I use Buttons or PictrureBoxes) with coordinates as invisible tags & invisible labels to show the names of eachone at groupal photos like facebook does. Now Im trying to recreate the same code at vb.net but I can't reach to get it work..
If I use Buttons with transparent .backcolor, no-text and no-borders, flat style, etc. to mark the photo area, they become opaque when I move the mouse over the control. if I disable becomes invisible for the mouse-over function.
If I use empty PictureBoxes instead for the same purpouse, as are empty they became invisible at runtime also for the "mouse over" function...
I dont know wich empty or invisible control must use to this finality. any suggestion?
Here is an example of what I was talking about in my comments:
Public Class Form1
Private ReadOnly actionsByRectangle As New Dictionary(Of Rectangle, Action)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'If the user clicks near the top, left corner, display a message.
actionsByRectangle.Add(New Rectangle(10, 10, 100, 100),
Sub() MessageBox.Show("Hello World"))
'If the user clicks near the bottom, right corner, minimise the form.
actionsByRectangle.Add(New Rectangle(ClientSize.Width - 110,
ClientSize.Height - 110,
100,
100),
Sub() WindowState = FormWindowState.Minimized)
End Sub
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
For Each rectangle As Rectangle In actionsByRectangle.Keys
If rectangle.Contains(e.Location) Then
'We have found a rectangle containing the point that was clicked so execute the corresponding action.
actionsByRectangle(rectangle).Invoke()
'Don't look for any more matches.
Exit For
End If
Next
End Sub
'Uncomment the code below to see the click targets drawn on the form.
'Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
' For Each rectangle As Rectangle In actionsByRectangle.Keys
' e.Graphics.DrawRectangle(Pens.Black, rectangle)
' Next
'End Sub
End Class
Note that I have added code there that can draw the boxes on the form if you want to see them, but those are just representations of the areas, not the Rectangle values themselves.

Animate Picturebox in VB

I am new to VB and just can't figure it out how to animate a button using the MouseHover Event..
I want to create a single loop for all the buttons(picturebox) in my project that will increase the button's size when the user rests the mouse on it.
Maybe something like:
For Each Form As Form In Application.OpenForms
For Each Control As Control In Form.Controls
Tks. Any help is appreciated.
Use Inherits to create a new button (or PictureBox) class for you purpose. Here is the code.
Public Class cuteButton
Inherits System.Windows.Forms.Button
Protected Overrides Sub OnMouseHover(e As EventArgs)
'
'Wite code here to change button size or whatever.
'
MyBase.OnMouseHover(e)
End Sub
End Class
A very simple way is to use a common MouseHover event to grow your buttons (which I guess is really a Picturebox with an image in it):
Private Sub CustomButton_Grow(sender As Object, e As System.EventArgs) Handles Picturebox1.MouseHover, Picturebox2.MouseHover
'Set a maximum height to grow the buttons to.
'This can also be set for width depending on your needs!
Dim maxHeight as single = 50
If sender.Height < maxHeight then
sender.Size = New Size(sender.Width+1,sender.Height+1)
End if
End Sub
Then you can reset all buttons in a snap using the MouseLeave event. If you want that part animated as well then you can to use a global shrink routine that constantly shrink all buttons but the one in MouseHover. Good luck!
This Will Work even if you have 10,000 button or picture box ....
I am Assuming that you only have 1 form and many Buttons ,,, you have to be specific on your question
This Code will work fine with Buttons, Picturebox ,text box
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each cntrl As Control In Controls ' Looping for each Button as Control
If TypeOf cntrl Is Button Then
AddHandler cntrl.MouseEnter, AddressOf cntrl_MouseEnter ' Adding the event and handler
AddHandler cntrl.MouseLeave, AddressOf cntrl_MouseLeave ' Adding the event and handler
End If
Next
End Sub
'' Assuming you wanna eNlarge everytime the mouse Hover or Enter
Private Sub cntrl_MouseEnter(sender As Object, e As EventArgs)
CType(sender, Button).Size = New Point(CType(sender, Button).Size.Width + 50, CType(sender, Button).Size.Height + 50)
End Sub
'' Here it goes back normal size
Private Sub cntrl_MouseLeave(sender As Object, e As EventArgs)
CType(sender, Button).Size = New Point(CType(sender, Button).Size.Width - 50, CType(sender, Button).Size.Height - 50)
End Sub

VB.net Borderless Form Maximize over Taskbar

When I maximize my borderless form, the form covers the entire screen including the taskbar, which I don't want it to do. I'm finding it very difficult to find a solution to my problem on the net, all I've come up with is the below code, which displays the taskbar for a short time, but then disappears and the form still takes up the entire screen.
Private Sub TableLayoutPanel1_DoubleClick(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.DoubleClick
If e.Location.Y < 30 Then
Me.WindowState = FormWindowState.Maximized
Me.ControlBox = True
End If
End Sub
I'm starting to think the only way to solve my problem is to find the screen size height minus the taskbar height to get the form height, but I'm hoping there might be a simpler answer.
Use the form's Load event to set its maximum size, like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.MaximumSize = Screen.FromRectangle(Me.Bounds).WorkingArea.Size
End Sub
Maximizing it now restricts the size to the monitor's working area, which is the monitor size minus the taskbars.
Use the "working area" of the screen:
Me.Bounds = Screen.GetWorkingArea(Me)
You should better do this:
Me.MaximumSize = Screen.FromControl(Me).WorkingArea.Size
Because some users use multiple monitors. So if you use Hans Passant's way, when user maximize application to secondary monitor, application will get primary's monitor working area size and will look awful!!!
I think this is a better way to solve your problem
Private Sub main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Normal
Me.StartPosition = FormStartPosition.Manual
With Screen.PrimaryScreen.WorkingArea
Me.SetBounds(.Left, .Top, .Width, .Height)
End With
End Sub

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.