在两个窗体之间传递信息- VB .Net-Winforms

llmtgqce  于 2023-01-14  发布在  .NET
关注(0)|答案(1)|浏览(142)

我在产品注册项目中有2个表格
第一个表单有3个按钮:新增|咨询|变更|- 调用第二个窗体,其中我有一个照片按钮。
新按钮:

Private Sub tsbNew_Click(sender As Object, e As EventArgs) Handles tsbNew.Click
        Try
            Using frm As New frm2ndForm
                frm.txtPrdCod.Enabled = True
                frm.txtPrdCod.Text = ""
                frm.ShowDialog()
            End Using

            tsbRefresh.PerformClick()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
End Sub

咨询按钮:

Private Sub tsbConsult_Click(sender As Object, e As EventArgs) Handles tsbConsult.Click
        Try
            If DGProds.CurrentRow Is Nothing Then
                MessageBox.Show("Select one product")
                Exit Sub
            End If

            Using frm As New frm2ndForm
                frm.txtPrdCod.Enabled = False

                frm.txtPrdCod.Text = DGProds.CurrentRow.Cells("prdCod").Value.ToString.Trim 'dr.Item("prdCod")
                frm.txtDes.Enabled = False
                frm.txtDesRed.Enabled = False
                
                frm.ShowDialog()
            End Using
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            Exit Sub
        End Try
End Sub

更改按钮

Private Sub tsbChange_Click(sender As Object, e As EventArgs) Handles tsbChange.Click
        Try
            If DGProds.CurrentRow Is Nothing Then
                MessageBox.Show("Select one product")
                Exit Sub
            End If

            Using frm As New frm2ndForm
                frm.txtPrdCod.Enabled = False
                frm.txtPrdCod.Text = DGProds.CurrentRow.Cells("prdCod").Value.ToString.Trim 'dr.Item("prdCod")
                frm.ShowDialog()
            End Using

            tsbRefresh.PerformClick()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
End Sub

在第二个窗体中,照片按钮将具有两种不同的行为:

  • 当用户点击了表单1上的“新建”按钮时,代码将打开搜索屏幕,供用户选择Photos in服务器上的文件夹中的图像,并将其显示在表单2中的图片框1中;
  • 当用户点击表单1上的“Consult”或“Change”按钮时,代码将比较Products表的prdCod字段和Photos文件夹中图像的文件名,如果找到,则在表单2的picturebox 1中显示图像。

如果表单1上的“单击按钮”为“新建”,请执行以下命令:

Private Sub btnPhoto_Click(sender As Object, e As EventArgs) Handles btnPhoto.Click        
        Using open As New OpenFileDialog With {
                .Title = "Select Photo",
                .FileName = "",
                .Filter = "Images PNG,JPEG,BMP,JPG|*.png;*.jpeg";*.bmp;*.jpg,
                .Multiselect = False}
            If open.ShowDialog = DialogResult.OK Then
                PictureBox1.Image = Image.FromFile(open.FileName)
            End If
        End Using
    End Sub

如果表单1上的“单击按钮”是“咨询或更改”,请执行以下命令:

Private Sub btnPhoto_Click(sender As Object, e As EventArgs) Handles btnPhoto.Click
        
        Dim IdProduto As String = prdCod   ***field Products Table that will be used for the search in the Photos folder

        If File.Exists("\\server\cmg\projects\Photos" & IdProduto) Then   ***Search the image from the Photos folder on the server
           PictureBox1.Image = Image.FromFile("\\server\cmg\projects\Photos" & IdProduto)
        End If
    End Sub

我如何检查第一个表单上的哪个按钮被点击,以便能够在第二个表单上执行正确的私有Sub?

uyhoqukh

uyhoqukh1#

它起作用了:
在第一个窗体(frmConsProd)的New Button代码(tsbNew_Click)中,我包含了将发送到第二个窗体(frmCadProd)的参数:

Public Class frmConsProd

    Private Sub tsbNew_Click(sender As Object, e As EventArgs) Handles tsbNew.Click
            Try
                Using frm As New frmCadProd("New")
                    frm.txtPrdCod.Enabled = True
                    frm.txtPrdCod.Text = ""
                    frm.ShowDialog()
                End Using

                tsbRefresh.PerformClick()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical)
            End Try
    End Sub
End Class

在第二种形式(frmCadProd)中,我创建了一个类变量(_operation)和构造函数(Public Sub New)来接收发送的参数:

Public Class frmCadProd    

    Dim _operation As String
    
    Public Sub New(operation As String)
        InitializeComponente()
        Select Case operation.ToLower()
            Case "new"
                _operation = operation                                    
            Case Else
                MessageBox.Show("Invalid option!")
                Close()
        End Select
    End Sub
End Class

感谢@ F0 r3 v3 r-A-N 00 b、@jmcilhinney和@user09938先生的帮助。

相关问题