添加按钮 [英] adding button

查看:81
本文介绍了添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..在我的项目中,我正在运行时创建新表单..在该新表单加载事件中,我要创建N个按钮..按钮在运行时本身应该是可移动的..pls为我提供了代码那家伙..

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..

推荐答案

Dim myForm As New NewForm()
Dim b As New Button()
b.Location = New Point(100, 200)
myForm.Controls.Add(b)


您好.我已经阅读了您发布的问题,如果您不清楚自己在做什么,那么您要尝试的工作是很大的一块.
但是,您想做的事情可以实现,但是您必须意识到,如果您对此不多才多艺,这将是非常困难的.

在回答您的其他问题中有一段代码,我只是在其中添加了一些内容.这段代码可以满足您的要求,但是如果您希望这些表单具有丰富的控件,那么您需要做的代码还要很多.您尝试做的事情没有一个简单的解决方法.

创建一个表单,并在名为Button1的表单上创建一个按钮,并将其插入您的代码中:

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:

'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


这篇关于添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆