I'm trying to move a set of 30 buttons downwards 100px when a textbox is ticked (and then back to their origins when the box is unticked). This is to provide room for some labels below the buttons if the user requires it... like the "show hints" box here: http://www.phonemicchart.com/
I have the following code:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
Button5.Location = New Point(100, 100)
Label1.Visible = False
Else
Button5.Location = New Point(300, 300)
Label1.Visible = True
End If
End Sub
The problem is the New Point() function. The new point I would like is -100px relative to origin, and then for the Else statement for it to be Button5.Location = Origin.
What commands can I use to achieve this?
Another idea...put all the buttons inside a Panel, then changed the .Top property of it to move everything inside it.
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Panel1.Top += 100
Else
Panel1.Top -= 100
End If
End Sub
use Left and Top instead as in:
Changing the location of a control on a windows form programatically using VB.net?
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
Button5.Top = 100
Button5.Left = 100
Label1.Visible = False
Else
Button5.Top = 300
Button5.Left = 300
Label1.Visible = True
End If
End Sub
You can iterate over the controls owned by your container, and as offered by mfarid, you can use the Top property
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Dim ctrl As Control
For Each ctrl In container.Controls
If CheckBox1.Checked = True Then
ctrl.Top += 100
ElseIf
ctrl.Top -= 100
End If
Next
EndSub
Related
I'm having a slight conflict with a button which I've been working with in Visual Basic NET.
My first code sample is for my Button_Height_Tick, which controls changing the button's height:
Dim ChangeHeight As Boolean = False
Private Sub Button_Height_Tick(sender As Object, e As EventArgs) Handles Button_Height.Tick
If Not ChangeHeight Then
Do Until FlatButton1.Height = 63
FlatButton1.Height += 1
System.Threading.Thread.Sleep(1)
Loop
ChangeHeight = True
Else
End If
End Sub
And for my FlatButton1_MouseHover.
Private Sub FlatButton1_MouseHover(sender As Object, e As EventArgs) Handles FlatButton1.MouseHover
Button_Height.Enabled = True
Button_Height.Start()
End Sub
Now, as you can see in the Button_Height_Tick sub, the code changes the height of the button to 63, however, when this code is ran, the buttons total height is changed.
Here are some photos in-case I haven't explained it well.
What my original button looks like
What I want it to do
What it's doing (going up in size vertically going down, when I want it to go up)
Please comment below if you don't understand this question.
You need to change the 'Top' position and also I notice you have a timer then just go in a do a loop. In your example there's no need for a timer.
I'll give an example using a timer and hopefully you'll understand it and can use it for what you want. I've changed 'hover' to 'enter' and 'leave'.
If it's too slow just change the increment amount.
Dim ChangeHeight As Boolean = False
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If ChangeHeight Then
FlatButton1.Height += 2
FlatButton1.Top -= 2
If FlatButton1.Height < 63 Then Exit Sub
FlatButton1.Height = 63
Timer1.Enabled = False
Else
FlatButton1.Height -= 2
FlatButton1.Top += 2
If FlatButton1.Height > 31 Then Exit Sub
FlatButton1.Height = 31
Timer1.Enabled = False
End If
End Sub
Private Sub FlatButton1_MouseEnter(sender As Object, e As EventArgs) Handles FlatButton1.MouseEnter
ChangeHeight = True
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub FlatButton1_MouseLeave(sender As Object, e As EventArgs) Handles FlatButton1.MouseLeave
ChangeHeight = False
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Hello and welcome to StackOverflow. I did a little example of how to achieve what you are looking for.
Code:
Public Class Form1
Dim buttonXCoordinate As Integer
Dim buttonYCoordinate As Integer
Dim buttonOriginalHeight As Integer
Dim buttonOriginalLocation As Point
Private Sub GetButtonCoordinate()
buttonXCoordinate = testBtn.Left
buttonYCoordinate = testBtn.Top
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttonOriginalHeight = testBtn.Height
buttonOriginalLocation = testBtn.Location
GetButtonCoordinate()
End Sub
Private Sub testBtn_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseEnter
Dim buttonLocation As Point = Nothing
GetButtonCoordinate()
buttonLocation.X += buttonXCoordinate
buttonLocation.Y += buttonYCoordinate - buttonOriginalHeight
testBtn.Height += buttonOriginalHeight
testBtn.Location = buttonLocation
End Sub
Private Sub testBtn_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseLeave
testBtn.Height = buttonOriginalHeight
testBtn.Location = buttonOriginalLocation
End Sub
End Class
I did it really fast but it's enough to give you an idea to how to achive your goal.
In my example there is a button called testBtn, when you go over it with the mouse it the button's height is increased and it returns back to normal when you move your mouse out of it
I'm Trying To make A button Only clickable once in Vb
I was thinking of this code
Sub B64_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B64.Click
Dim BClick As Integer = 0
If BClick = 0 Then
BClick = 1
'Other instructions
End If
Any Other Ideas!
Also Can I do something so that the button will make a sound when it is clicked ?!
Any Other Ideas!
Thank u
In your click event you can do:
B64.Enabled = False
You can also play a .WAV file on click:
Dim player As New System.Media.SoundPlayer()
player.SoundLocation = path
player.Load()
player.Play()
Just Disable Button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Button1.Enabled = False
'Do Stuff....
'& Enable it if you want
Button1.Enabled = True
End Sub
you can disable button or any control with this code
by using Button1_Click(Button1, Nothing) or Button1_Click(Button2, Nothing)
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim Element As Button = DirectCast(sender, Button)
Element.Enabled = False
'Do Stuff....
'if you want delay use: Threading.Thread.Sleep(500)
Element.Enabled = True
End Sub
I want to know the code to the following illustration.
i have one form with some checkboxs and one button,
screen is here
i've try with this code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True And CheckBox2.Checked = True And CheckBox3.Checked = True And CheckBox4.Checked = True Then
'when the button is clicked will be the process for moving images
'Like
System.IO.File.Copy(Application.StartupPath + "\File\Pic1.jpg", "D:\File\Pic1.jpg")
End If
End Sub
I was tired with that code, is there a shorter coding ?
for example, if the checkbox1.checked = true and another checkbox not checked then only moving one pict
If I understand the question, you want to copy pictures 1 to 4 if the checkboxes 1 to 4 are checked.
Try this:
Dim SourcePath As string = Application.StartupPath + "\File\"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
CopyFile(CheckBox1, "Pic1.jpg")
CopyFile(CheckBox2, "Pic2.jpg")
CopyFile(CheckBox3, "Pic3.jpg")
CopyFile(CheckBox4, "Pic4.jpg")
End Sub
Private Sub CopyFile(CB As CheckBox, FileName As String)
If CB.Checked Then
System.IO.File.Copy(SourcePath + FileName, "D:\File\" + FileName)
End If
End Sub
I just want to ask a really important question in vb2008 :
I'm working on 2D level designer that I just put all the images and the collision rectangles in his place then the program generate the right code.
the problem is : I make a button and in his click event it add a new picture box , when it add I have to move it with the mouse , I have the "move" code but like you know it's a new picture box so I can't write the code before the picture box add.(if you doesn't understand I can explain my situation again)
for more clear this is the code :
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer
newPictureBox.Image = Image.FromFile("C:\Users\hp\Desktop\ground.bmp")
newPictureBox.Name = "image" & (i)
newPictureBox.Visible = True
newPictureBox.Top = 200
newPictureBox.Width = 100
newPictureBox.Height = 50
newPictureBox.Left = 100 + goToRight
newPictureBox.SizeMode = PictureBoxSizeMode.AutoSize
'add control to form
Controls.Add(newPictureBox)
goToRight = goToRight + newPictureBox.Width
i += 1
End Sub
and this is the "move picturebox" code(it's for an existed picturbox):
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim endPoint As Integer = Val(TextBox1.Text)
Label1.Text = "X: " & MousePosition.X - (Location.X + 8)
Label2.Text = "Y: " & MousePosition.Y - (Location.Y + 29)
RadioButton1.Left = endPoint + RadioButton2.Left
Panel1.Left = 0
'Move picture code :
If IcanMove = True Then
PictureBox1.Left = MousePosition.X - (Location.X + 8) - differenceX
PictureBox1.Top = MousePosition.Y - (Location.Y + 29) - differenceY
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
IcanMove = True
differenceX = (MousePosition.X - (Location.X + 8)) - PictureBox1.Left
differenceY = (MousePosition.Y - (Location.Y + 29)) - PictureBox1.Top
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
IcanMove = False
End Sub
thanks you for reading :)
You need to add event handlers for the newly added PictureBox, here is how to do that.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim NewPictureBox As PictureBox = New PictureBox
NewPictureBox.Parent = Me
NewPictureBox.Location = New Point(100, 100)
Me.Controls.Add(NewPictureBox)
'you can use existing event handlers like your PictureBox1_MouseDown
AddHandler NewPictureBox.MouseUp, AddressOf PictureBox_MouseUp
AddHandler NewPictureBox.MouseDown, AddressOf PictureBox_MouseDown
End Sub
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
MessageBox.Show("Triggered MouseDown Event")
End Sub
Private Sub PictureBox_MouseUp(sender As Object, e As MouseEventArgs)
MessageBox.Show("Triggered MouseUp Event")
End Sub
(if you also remove the PictureBox's before closing the form be sure to remove the handlers for them)
For more info check AddHandler and RemoveHandler
As you can see from the code provided below as well as the title, that I'm having trouble trying to figure out how to move this dynamically embeded borderless form in VB.NET.
Private Sub AddWidget_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddWidget.Click
Dim WidgetForm As Form
WidgetForm = New Form()
WidgetForm.ShowInTaskbar = False
WidgetForm.TopMost = True
WidgetForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
WidgetForm.BackgroundImage = Image.FromFile(Me.OpenFileDialog1.FileName)
WidgetForm.BackgroundImageLayout = ImageLayout.Stretch
WidgetForm.Show()
opac = Me.OpacInt.Text
WidgetForm.Opacity = opac
sizew = Me.WidgetW.Text
sizey = Me.WidgetY.Text
WidgetForm.Size = New System.Drawing.Size(sizew, sizey)
End Sub
I've done this method (code below) a couple times in the past, and it always seems to work for moving a form, but I'm unsure on how to apply this for the dynamically embeded form.
Any help would be greatly appreciated.
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
drag = True
mousex = Windows.Forms.Cursor.Position.X - Me.Left
mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End If
Timer1.Enabled = True
Timer1.Interval = 2500
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
Timer1.Enabled = False
drag = False
End Sub
The dynamic form would perform as same as the normal form. First of all you need to add some events for the dynamic form when you create it
Public WidgetForm As Form
Private Sub AddWidget_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddWidget.Click
WidgetForm = New Form()
WidgetForm.ShowInTaskbar = False
WidgetForm.TopMost = True
WidgetForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
WidgetForm.BackgroundImage = Image.FromFile(Me.OpenFileDialog1.FileName)
WidgetForm.BackgroundImageLayout = ImageLayout.Stretch
WidgetForm.Show()
opac = Me.OpacInt.Text
WidgetForm.Opacity = opac
sizew = Me.WidgetW.Text
sizey = Me.WidgetY.Text
WidgetForm.Size = New System.Drawing.Size(sizew, sizey)
'Add the event here
AddHandler WidgetForm.MouseDown, AddressOf WidgetForm_MouseDown
AddHandler WidgetForm.MouseMove, AddressOf WidgetForm_MouseMove
AddHandler WidgetForm.MouseUp, AddressOf WidgetForm_MouseUp
End Sub
Then you can move the form by the events handler
Private Sub WidgetForm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
drag = True
mousex = Windows.Forms.Cursor.Position.X - CType(sender, Form).Left
mousey = Windows.Forms.Cursor.Position.Y - CType(sender, Form).Top
End If
Timer1.Enabled = True
Timer1.Interval = 2500
End Sub
Private Sub WidgetForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If drag Then
CType(sender, Form).Top = Windows.Forms.Cursor.Position.Y - mousey
CType(sender, Form).Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub WidgetForm_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Timer1.Enabled = False
drag = False
End Sub