locked
Calling up a string variable from one Sub into another RRS feed

  • Question

  • Hello,

    My VB script currently allows the user to select a file using the OpenFileDialog function. The file pathname is then set as a string variable xPath

    I'm now attempting to call this variable in another sub, in order that the text in a text box will change it's value to that of the xPath string.

    Any help would be very much appreciated!

    Thanks

    Public Class Form1
        
    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName) 'Sets the 
                Dim xPath As String = OpenFileDialog1.FileName 'Sets the path name as a variable
                MessageBox.Show(xPath) 'Displays the path name in a message box
                sr.Close()
            End If
        End Sub
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            TextBox1.Text = "Hello"
        End Sub
    End Class

    Thursday, April 30, 2015 2:29 PM

Answers

  • If you want to show the file path in TextBox, then just store it in the TextBox's Text property.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim xPath As String = OpenFileDialog1.FileName 'Sets the path name as a variable
                TextBox1.Text = xPath 'Displays the path name in a TextBox
            End If
        End Sub
    End Class

    Make sure you delete the TextBox1_TextChanged Sub.

    • Edited by Blackwood Thursday, April 30, 2015 3:04 PM
    • Marked as answer by Carl Cai Thursday, May 7, 2015 6:27 AM
    Thursday, April 30, 2015 3:01 PM