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



我在一个产品注册项目中有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

在第二个表单中,Photo按钮将有两个不同的行为:

  • 当用户点击"New"按钮,代码将打开一个搜索屏幕,让用户在Photos in服务器上的文件夹中选择一张图像,并将其显示在Form 2的picturebox1中;

  • 当用户点击了"咨询"时或";Change"按钮时,代码将比较Products Table的prdCod字段和Photos文件夹中的图像的文件名,找到后,将在Form 2的picturebox1中显示该图像。

如果"点击按钮"如果表单1是"New",执行以下命令:

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("\servercmgprojectsPhotos" & IdProduto) Then   ***Search the image from the Photos folder on the server
PictureBox1.Image = Image.FromFile("\servercmgprojectsPhotos" & IdProduto)
End If
End Sub

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

成功了:

在第一个表单(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

感谢@F0r3v3r-A-N00b、@jmcilhinney和@user09938先生的帮助。

最新更新