VB.NET WinForms:如何判断主窗体按钮点击以执行子窗体对应逻辑
解决VB.NET WinForms子窗体根据主窗体按钮执行不同逻辑的问题
核心思路是在子窗体中添加操作模式标识属性,主窗体打开子窗体时传递当前操作类型,子窗体的Photo按钮事件根据标识分支执行对应逻辑。具体步骤如下:
1. 在子窗体中定义操作类型标识
在frm2ndForm的代码文件中,先通过枚举明确操作模式,再添加属性存储当前模式:
' 定义操作模式枚举 Public Enum OperationMode NewProduct ConsultProduct ChangeProduct End Enum Public Class frm2ndForm ' 接收主窗体传递的操作模式 Public Property CurrentOperation As OperationMode = OperationMode.NewProduct ' 子窗体原有代码... End Class
2. 修改主窗体按钮代码,传递操作模式
在主窗体三个按钮的点击事件中,创建子窗体实例后设置对应的CurrentOperation属性:
New按钮代码修改:
Private Sub tsbNew_Click(sender As Object, e As EventArgs) Handles tsbNew.Click Try Using frm As New frm2ndForm frm.CurrentOperation = OperationMode.NewProduct ' 设置为新增模式 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
Consult按钮代码修改:
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.CurrentOperation = OperationMode.ConsultProduct ' 设置为查询模式 frm.txtPrdCod.Enabled = False frm.txtPrdCod.Text = DGProds.CurrentRow.Cells("prdCod").Value.ToString.Trim 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
Change按钮代码修改:
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.CurrentOperation = OperationMode.ChangeProduct ' 设置为修改模式 frm.txtPrdCod.Enabled = False frm.txtPrdCod.Text = DGProds.CurrentRow.Cells("prdCod").Value.ToString.Trim frm.ShowDialog() End Using tsbRefresh.PerformClick() Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical) End Try End Sub
3. 修改子窗体Photo按钮事件,分支执行逻辑
在frm2ndForm的btnPhoto_Click事件中,根据CurrentOperation的值执行对应逻辑:
Private Sub btnPhoto_Click(sender As Object, e As EventArgs) Handles btnPhoto.Click Select Case CurrentOperation Case OperationMode.NewProduct ' 新增模式:打开文件选择对话框选图 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 Case OperationMode.ConsultProduct, OperationMode.ChangeProduct ' 查询/修改模式:根据产品编码匹配服务器图片 Dim IdProduto As String = txtPrdCod.Text.Trim ' 补斜杠避免路径拼接错误 Dim photoPath As String = "\\server\cmg\projects\Photos\" & IdProduto If File.Exists(photoPath) Then PictureBox1.Image = Image.FromFile(photoPath) Else MessageBox.Show("No photo found for this product.") PictureBox1.Image = Nothing ' 清空图片框 End If End Select End Sub
补充说明:
- 枚举让操作类型更清晰,后续扩展新操作只需添加枚举值;
- 路径拼接时补斜杠,避免出现
\\server\cmg\projects\Photos123这类错误路径; - 查询/修改模式直接用子窗体的
txtPrdCod值,无需重复查询数据库,提升效率。
内容的提问来源于stack exchange,提问作者Clamarc
相关产品推荐
相关产品推荐

