2

我正在创建一个打开自定义表单对话框的贷款程序,您选择图片,单击打开,然后需要在对话框中点击确定后将其传递给另一个表单以使用。当我从自定义对话框表单中单击徽标文件按钮时,这是我的代码。

该表单称为对话框表单,我需要将图片发送到 NewLoanCaculatorForm 以填充表单中的图片区域。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogoFile.Click

Dim mystream As Stream = Nothing

    'Open the File to pickup icon for Loan Calculator
    Dim OpenFileDialog1 As New OpenFileDialog

    'Set up and display the open File Dialog
    With OpenFileDialog1
        'Begin in the current Directory
        .InitialDirectory = "C:\"
        .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
    End With

    If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try
            mystream = OpenFileDialog1.OpenFile()
            If (mystream IsNot Nothing) Then
                ' I believe the coded goes here but I'm stuck 
            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open. 
            If (mystream IsNot Nothing) Then
                mystream.Close()
            End If
        End Try
    End If

End Sub
4

1 回答 1

0

这就是我通常做这样的事情的方式:

在 DialogForm 中创建全局变量:

Public Property sPath as String 

或者

Public Property imgLogo as Image

要获取图像,请执行以下操作:

imgLogo = Image.FromFile(OpenFileDialog1.FileName)

或者干脆做:

sPath = OpenFileDialog1.FileName

代替

mystream = OpenFileDialog1.OpenFile()

然后,当您完成此操作后,您可以通过单击“确定”按钮或您所称的任何名称来关闭表单。

然后在您调用 DialogForm 的主窗体 NewLoanCaculatorForm 中,您只需执行以下操作:

img = DialogForm.imgLogo

或者

path = DialogForm.sPath
img = Image.FromFile(path)

取决于您在 DialogForm 中存储信息的方式。

此外,如果您正在寻找图像,我建议您不要在过滤器中包含 .txt。那将严重破坏执行。

于 2012-12-11T07:32:48.380 回答