2

カスタムフォームダイアログボックスを開くローンプログラムを作成しています。画像を選択し、[開く]をクリックして、ダイアログボックスから[OK]を押した後に使用する別のフォームに渡す必要があります。これは、カスタム ダイアログ フォームから [ロゴ ファイル] ボタンをクリックしたときのコードです。

フォームはダイアログ フォームと呼ばれ、画像を 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()

次に、これを行ったら、[OK] ボタンまたは任意のボタンをクリックしてフォームを閉じます。

次に、DialogForm を呼び出すメイン フォーム NewLoanCaculatorForm で、次のようにします。

img = DialogForm.imgLogo

また

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

DialogForm に情報を保存した方法によって異なります。

また、画像を探している場合は、フィルターに .txt を含めないことをお勧めします。それは実行を深刻に台無しにするでしょう。

于 2012-12-11T07:32:48.380 に答える