【问题标题】:Design-time Coding设计时编码
【发布时间】:2011-12-06 20:05:37
【问题描述】:

我是 Visual Basic dot net 的新手。我试图在运行时创建一个设计。这是我的简单代码。

    Dim frmLogin As New Form
    Dim lblUserName, lblPassword As New Label
    Dim txtUserName, txtPassword As New TextBox
    Dim btnContinue, btnCancel As New Button

    'set frmLogin properties
    frmLogin.StartPosition = FormStartPosition.CenterScreen
    frmLogin.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    frmLogin.BackColor = Color.LightGray
    frmLogin.Size = New Size(400, 200)

    'set lblUserName properties
    lblUserName.Text = "User Name: "
    lblUserName.Font = New Font("Calibri", 12, FontStyle.Regular)
    lblUserName.Location = New Point(20, 30)

    'set lblPassword properties
    lblPassword.Text = "Password: "
    lblPassword.Font = New Font("Calibri", 12, FontStyle.Regular)
    lblPassword.Location = New Point(20, 70)

    'set txtUserName properties
    txtUserName.Font = New Font("Calibri", 12, FontStyle.Regular)
    txtUserName.Location = New Point(120, 20)
    txtUserName.Size = New Size(250, 20)

    txtPassword.Font = New Font("Calibri", 12, FontStyle.Regular)
    txtPassword.Location = New Point(120, 60)
    txtPassword.Size = New Size(250, 20)
    txtPassword.PasswordChar = "*"

    btnCancel.Text = "Cancel"
    btnCancel.Location = New Point(270, 120)
    btnCancel.Size = New Size(100, 28)
    btnCancel.BackColor = Color.White
    btnCancel.Font = New Font("Calibri", 12, FontStyle.Regular)

    frmLogin.Controls.Add(lblPassword)
    frmLogin.Controls.Add(lblUserName)
    frmLogin.Controls.Add(txtUserName)
    frmLogin.Controls.Add(txtPassword)
    frmLogin.Controls.Add(btnCancel)
    frmLogin.ShowDialog()

如何开始在这里创建代码?我想从 btnCancel 开始,如何在 btnCancel 中插入此代码

dim myAns as string = msgbox("This will exit the program. Are you sure?")
if ans = vbyes then application.exit

【问题讨论】:

    标签: vb.net design-time


    【解决方案1】:

    您必须向控件添加事件处理程序:

    'register the event handler after initializing of the control
    AddHandler btnCancel.Click, AddressOf CancelClick
    
    
    'create a method with same signature as the delegate of the event
    Private Sub CancelClick(ByVal sender As Object, ByVal e As EventArgs)
       'do your stuff here
    End Sub
    

    【讨论】:

    • 如果我在另一个按钮中创建代码(用于运行时设计)怎么办?如何在取消按钮中插入退出代码。
    • ...并且出现错误方法'Public Sub CancelClick(sender As Object, e As system.Windows.Forms.MoseEventArgs)'与委托'Delegate Sub EventHandler(sender)没有相同的签名作为对象,e 作为 System.EventArgs)'。
    • 对不起它的 EventArgs 而不是 MouseEventArgs --> 见编辑。请再问你另一个问题,我不明白你的意思。
    猜你喜欢
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    相关资源
    最近更新 更多