Jump to content

What I'm looking to do, is when I click a button I want to create a new button somewhere in the form.

Heres what I have so far...

Dim newButton = New Button

Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn.Click

newButton.Text = "New Button"

newButton.Location.X = btn1.Location.X + 200

newButton.Location.Y = btn1.Location.Y + 200

End Sub

When I click btn1 it errors out at...

newButton.Location.X = btn1.Location.X + 200...?

Where am I going wrong? I have a suspicion its somewhere in the creation of the "Private Sub..."

Link to comment
https://linustechtips.com/topic/225706-some-help-w-visual-basic/
Share on other sites

Link to post
Share on other sites

-snip-

 

Add another button to the canvas, click on it and go to properties. In properties look for visibility and set it to false.

 

Then double click the original button (the one you click to get the other one to appear) and it will take you to the code page. You can use this code to make the button visible when you click button 1.

button2.Visible = true

Sorry if that was confusing, i don't think i explained it very well :P

Link to comment
https://linustechtips.com/topic/225706-some-help-w-visual-basic/#findComment-3093210
Share on other sites

Link to post
Share on other sites

UPDATE:

 

Got it, the location error when I was trying to debug was due to the keyword "Location.X, or Location.Y" being replaced with the keywords "button.Left, or button.Top" I'm using VB2013

 

   

    //creating the variables needed

    Dim intC As Integer = 0
    Dim leftC As Integer
    Dim topC As Integer
    Dim newButton = New Button



    //updated code that works

    //on click of btn, increase intC (helps name, and add text to the new btn)

    //add new locations (leftC and topC)

    //add the new button, name it, add text to it, and update the location

    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        intC = intC + 1
        leftC = leftC + 50
        topC = topC + 50
        Me.Controls.Add(newButton)
        newButton.Name = "btn" + intC.ToString
        newButton.Text = "btn" + intC.ToString
        newButton.Left = leftC
        newButton.Top = topC
    End Sub

Link to comment
https://linustechtips.com/topic/225706-some-help-w-visual-basic/#findComment-3098445
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×