如何使窗口窗体返回所选按钮的值

时间:2014-01-08 17:32:45

标签: vb.net

我在窗体上制作电影院的图形表示。每个座位都表示为一个按钮。

我在另一张表格上有另一个用于预订座位的按钮。此按钮启动电影室表格,如果用户点击任何按钮,则应返回并显示该按钮上的文本。

电影室类(图形表示)

Imports System.Data.SqlClient

Public Class Cinema1Seats
    Dim seat As String = vbNull
    Private Sub Cinema1Seats_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Try

        con = getconnect()
        con.Open()
        Dim comm As New SqlClient.SqlCommand
        comm = New System.Data.SqlClient.SqlCommand("select * from seats ", con)
        ' comm.Parameters.Add("@id", SqlDbType.VarChar).Value = ticketNumber
        Dim sqlReader As System.Data.SqlClient.SqlDataReader
        sqlReader = comm.ExecuteReader
        Do While sqlReader.Read
            Dim seat As String = sqlReader.GetString(0)
            Dim seatbtn As New Button
            seatbtn.Width = 50
            seatbtn.Height = 20
            seatbtn.Text = seat


            AddHandler seatbtn.Click, AddressOf Me.Button_Click   ' Again from answer by Reed.
            Me.FlowLayoutPanel1.Controls.Add(seatbtn)
        Loop
    Catch ex As Exception
        MsgBox(ex.ToString)

    End Try





End Sub
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim asas As String = (CType(sender, System.Windows.Forms.Button).Text)
    MsgBox(asas)
End Sub
Public Function getseat() As String
    If (seat = vbNull) Then

    Else
        Return seat
        Me.Close()
    End If
End Function


End Class

具有按钮

的第二个表单
Public Class SampleSeat_vb

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Try
        Dim flag As Integer = 0
        Dim i As Integer = 0
        While i < Application.OpenForms.Count And flag = 0
            If Application.OpenForms(i).Name = "Cinema1Seats" Then
                flag = 1
            End If
            i += 1
        End While
        If flag Then
            Application.OpenForms("Cinema1Seats").BringToFront()
        Else
            Dim str As New Cinema1Seats
            str.Show()
            Dim we As String = str.getseat
            MsgBox("You selected : " + we)
        End If

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try





End Sub

Private Sub SampleSeat_vb_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我并不完全清楚你想要实现的目标,但为了找到被点击的按钮文本,你可以这样做:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim btn As Button = CType(sender, Button)
    MessageBox.Show("You clicked on " + btn.Text")
End Sub