Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi .. in my prjct i am creating new form at run time.. in that new form load event i want create some N of buttons.. that buttons are should be moveable at runtime itself..pls give me code for that guys..
Posted
Updated 9-Oct-10 22:03pm
v2
Comments
OriginalGriff 10-Oct-10 4:20am    
"this is not right code for my questions.. pls give me correct coding - sameertm 2 mins ago"
Care to elaborate on where it is too complex for you?
Abhinav S 10-Oct-10 5:59am    
You should try out something before you ask for code...

VB
Dim myForm As New NewForm()
Dim b As New Button()
b.Location = New Point(100, 200)
myForm.Controls.Add(b)
 
Share this answer
 
Comments
sameertm 10-Oct-10 4:17am    
this is not right code for my questions.. pls give me correct coding
Hello. I have read the questions you have posted and what you are trying to do is a rather big chunk to bite on if you are not clear what you are doing.
However what you want to do can be accomplished but you have to realize that it is pretty tough if you are not versatile at this.

There was a piece of code posted in an answer to one of your other questions and i just made some additions to it. This code does what you want but if you want those forms to be rich with controls there will be a lot more of coding ahead of you to do. A simple workaround for what you are trying to do just doesn't exist.

Create a form and create a button on the form named Button1 and insert this into your code:

VB
'This sub creates a new form and a button on the form AT RUNTIME
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Create a new form and button
    Dim FormX As New Form
    Dim ButtonX As New Button
    ButtonX.Location = New Point(100, 100)
    ButtonX.Text = "My Button"
    FormX.Name = "SomeForm"
    'Create handlers for the new form and button
    AddHandler FormX.Load, AddressOf FormXLoadHandlerFunction
    AddHandler ButtonX.MouseDown, AddressOf ButtonXMouseDownHandlerFunction
    AddHandler ButtonX.MouseClick, AddressOf ButtonXMouseClickHandlerFunction
    FormX.Controls.Add(ButtonX)
    FormX.Show()
End Sub
'this sub represents the LOAD event for your RUNTIME generated form any code within this sub will be executed on loading of the RUNTIME generated form
Public Sub FormXLoadHandlerFunction(ByVal sender As System.Object, ByVal e As System.EventArgs)
    MsgBox(sender.Name + " was loaded.")
End Sub
'this sub represents the MOUSEDOWN event for the form and allows you to move the button on the generated form with holding the RIGHT MOUSE BUTTON
Public Sub ButtonXMouseDownHandlerFunction(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If Control.MouseButtons = Windows.Forms.MouseButtons.Right Then
        Dim mousepos As New Point(MousePosition)
        Dim deviation As New Point(mousepos.X - sender.Location.X, mousepos.Y - sender.Location.Y)
        Do
            sender.Location = MousePosition - deviation
            Application.DoEvents()
        Loop While MouseButtons = Windows.Forms.MouseButtons.Right
    End If
    If Control.MouseButtons = Windows.Forms.MouseButtons.Left Then
        MsgBox("Button on a runtime created form was clicked")
    End If
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900