Put controls on a form border in vb.net? - vb.net

I have a project where I want to put controls on the form's border, when the formborderstyle is set to formborderstyle.sizable
Formborderstyle.none will not work, since it can't be sized on runtime, and goes in front of the taskbar when maximized.
I'm using vb.net 2010.

I'm not sure if you can override the drawing of the border and if you could not sure how you would add control to the border.
You can temporarily change the border style before you maximize the form.
And you can overload the client event to handle re-sizing the form yourself.
Are there any other reasons you dont want to go with Formborderstyle.none?
Public Class Form1
Inherits Windows.Forms.Form
Private Const BorderWidth As Integer = 30 'This is just for demo purposes.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Panel1.Location = New Point(Me.Width - BorderWidth, 0)
Panel1.Width = BorderWidth
End Sub
Public xLocation, yLocation As Integer
Private Sub Panel1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
xLocation = PointToScreen(Cursor.Position).X
yLocation = PointToScreen(Cursor.Position).Y
End Sub
Private Sub Panel1_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
'Stop resizing form
Me.Width = Me.Width + (PointToScreen(Cursor.Position).X - xLocation)
xLocation = PointToScreen(Cursor.Position).X
yLocation = PointToScreen(Cursor.Position).Y
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
Me.WindowState = FormWindowState.Maximized
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
End Sub
End Class

Related

How can i keep adding buttons related to what i wrote when i click the button

Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oForm As Form2 = New Form2
Dim btn As Button = New Button With {
.Location = New Point(300, 100),
.Text = TextBox1.Text,
.ForeColor = Color.Black
}
oForm.Controls.Add(btn)
oForm.StartPosition = FormStartPosition.CenterScreen
oForm.Show(Me)
Me.Hide()
End Sub
End Class
I want to keep adding new buttons when the button is clicked but not at the same position i want the buttons to keep adding downwards like this:
[Button1]
[Button2]
[Button3]
You can get your Button1 location and than increase the Y value every time you click on it.
Public Class Form1
Dim Button1Coordinate As Point
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim btn As Button = New Button
Button1Coordinate.Y += 46
With btn
.Location = New Point(Button1Coordinate)
.Text = TextBox1.Text
.ForeColor = Color.Black
End With
Me.Controls.Add(btn)
Me.StartPosition = FormStartPosition.CenterScreen
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1Coordinate = Button1.Location
End Sub
End Class
N.B. the default height of a button is 23 pixels

border less form gets automatically resized during runtime in vb .net

I've created a small borderless form using vb .net. The form contains three square buttons. The size of the form is (93, 31). All good during the design of the form, but when I run the program the size of the form gets increased to somewhat like (98,34). I even switched between true and false for the autosize property of the form to check if it was the cause of the problem but that dint help.
How do I stop the form from resizing?
EDIT:
I made the form borderless by setting the form's FormBorderStyle property to None
Here's the code
Public Class OSD_Dialog
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
' The folllowing three subs are helpfull in making the form dragable
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
drag = True
mousex = Windows.Forms.Cursor.Position.X - Me.Left
mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
drag = False
End Sub
'The following sub is helpful in creating an outline on the border of the form
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
MyBase.OnPaintBackground(e)
Dim rect As New Rectangle(0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
e.Graphics.DrawRectangle(Pens.White, rect)
End Sub
Private Sub OSD_Dialog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TopMost = True
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.BackColor = Color.Red
TransparencyKey = BackColor
End Sub
Add this to your Form_Load event:
Me.Size = New Size(93, 31)
Also ensure you have set AutoScaleMode to 'None' in design time

Creating New Form Controls in Visual Basic

I am trying to have a button, when clicked, creates a new picturebox control. So that everytime I click it, it adds another new picturebox control. These pictureboxes will all have the same functions like being able to move them around and draw on them. But the button only makes one and no more after that with the following code. What am I missing?
Public Class Form1
Dim xpos As New Integer
Dim ypos As New Integer
Dim pos As New Point
Dim x As Integer
Dim y As Integer
Dim canvas As New PictureBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 0
i = i + 1
canvas.Name = "canvas" & i
canvas.BackColor = Color.White
canvas.BorderStyle = BorderStyle.FixedSingle
canvas.Image = Nothing
canvas.Height = TextBox1.Text
canvas.Width = TextBox2.Text
AddHandler canvas.MouseDown, AddressOf PictureBox1_MouseDown
AddHandler canvas.MouseMove, AddressOf PictureBox1_MouseMove
Controls.Add(canvas)
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
xpos = Cursor.Position.X - canvas.Location.X
ypos = Cursor.Position.Y - canvas.Location.Y
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
pos = MousePosition
pos.X = pos.X - xpos
pos.Y = pos.Y - ypos
canvas.Location = pos
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Dim canvas As New PictureBox
You have multiple bugs in your code but this is the most serious one. The As New syntax ensures that you'll always create a PictureBox object, but it will only ever be one object. And of course, one variable cannot keep track of multiple picture boxes.
What you need to do is create a new one ever time the button is clicked. It is generally a good idea to keep track of the picture boxes you create. So correct code looks like:
Dim canvases As New List(Of PictureBox)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim canvas = New PictureBox
canvases.Add(canvas)
canvas.Name = "canvas" & canvases.Count.ToString()
'' etc...
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim canvas = DirectCast(sender, PictureBox)
xpos = Cursor.Position.X - canvas.Location.X
ypos = Cursor.Position.Y - canvas.Location.Y
End Sub
Note how the sender argument gives you a reference back to the picture box object that's being moused. Do the same thing in any other event handlers.
Increasing the counter whenever a new canvas is created:
Public Class Form1
Dim counter As New Integer
...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter += 1
canvas.Name = "canvas" & counter.ToString()
...
Accessing the canvas that the mouse is currently moving over:
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
pos = MousePosition
pos.X = pos.X - xpos
pos.Y = pos.Y - ypos
DirectCast(sender, PictureBox).Location = pos
End If
End Sub

PictureBox MouseEnter / MouseLeave Events not Firing

Create a new form with three picture boxes. This code is intended to draw a border when the mouse enters the picture box and remove it when it leaves. It is inconsistent in the results. Sometimes it draws/removes the border, sometimes it doesn't. This code is not complex. Using VS 2012.
Private Sub PictureBox_MouseEnter(sender As Object, e As EventArgs) _
Handles PictureBox1.MouseEnter, PictureBox2.MouseEnter, PictureBox3.MouseEnter
Dim pb As PictureBox = DirectCast(sender, PictureBox)
pb.BorderStyle = BorderStyle.FixedSingle
' Debug.WriteLine("E " & pb.Name)
End Sub
Private Sub PictureBox_MouseLeave(sender As Object, e As EventArgs) _
Handles PictureBox1.MouseLeave, PictureBox2.MouseLeave, PictureBox3.MouseLeave
Dim pb As PictureBox = DirectCast(sender, PictureBox)
pb.BorderStyle = BorderStyle.None
' Debug.WriteLine("X " & pb.Name)
End Sub
I could also reproduce the issue. So, expanding on the comments above about "drawing something else" instead of using the Picturebox's property, let me suggest this quick and dirty approach:
Use a RectangleShape object, the one provided by the VisualBasic Powerpack 3.0 addon. Simply put one of those in the same form your PictureBox is in, and make it invisible (visible = false).
The code is also easy:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.RectangleShape1.Location = New Point(Me.PictureBox1.Left - 1, Me.PictureBox1.Top - 1)
Me.RectangleShape1.Size = New Size(Me.PictureBox1.Width + 1, Me.PictureBox1.Height + 1)
End Sub
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
Me.RectangleShape1.Visible = True
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
Me.RectangleShape1.Visible = False
End Sub
End Class
Need some help from your Form MouseEnter Event ..
Dim pb As PictureBox = New PictureBox
Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
pb.BorderStyle = BorderStyle.None
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
pb = PictureBox1
pb.BorderStyle = BorderStyle.FixedSingle
End Sub
I followed KalaNag idea by putting my picturebox inside a panel and handled the event on the pciturebox by doing like this
private void PictureBox_MouseEnter(object sender, EventArgs e)
{
PictureBox control = sender as PictureBox;
(control.Parent as Panel).Width = 20;
(control.Parent as Panel).Height = 20;
(control.Parent as Panel).BorderStyle = BorderStyle.Fixed3D;
}
private void PictureBox_MouseLeave(object sender, EventArgs e)
{
PictureBox control = sender as PictureBox;
(control.Parent as Panel).Width = 18;
(control.Parent as Panel).Height = 18;
(control.Parent as Panel).BorderStyle = BorderStyle.None;
}
I changed the control's size because otherwise, the picturebox keeps flickering when the mouse hovers the borders as the cursor is entering and leaving indefinitely since the borders change the size of the control.
Works like a charm !

Fade in picturebox in VB

How do I make a picturebox fade in after a timer
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
picturebox1.opacity = false
End Sub
Opacity is not a member of System.Windows.Forms.PictureBox. You can't have the same effect like forms. But you can do this:
PictureBox1.Visible = False