Results 1 to 5 of 5

Thread: NooB Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    295

    Question NooB Question

    Hello all,

    I'm teaching myself VB .NET and I am having a bit of an issue.

    I have a Form with two buttons...

    Button 1 is where I capture the path and file name using OpenFileDialog.
    Button 2 is where I want to process the file I captured from button 1.

    I do not seem to be able to pass the file name from button 1 to button 2.

    is there an Easy way to do this?

    Thanks in advance,

    -NJ

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: NooB Question

    Well there is but why would you need to? Button1 opens the filedialog, you get the filename, so why not just process it as a continuation of that sub? Button 2 appears to have no raison d'etre (oooh, hark at me coming over all posh!)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    Re: NooB Question

    You can assign the file path you got from the OFD to a string variable and then use that variable with button2.

    edit: VS is taking foreverr to load, so this is freehand:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Dim filepath As String
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.  
    7.         OpenFileDialog1.ShowDialog()
    8.         filepath = OpenFileDialog1.FileName
    9.  
    10.     End Sub
    11.  
    12.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    13.  
    14.         MessageBox.Show(filepath)
    15.  
    16.     End Sub
    17. End Class
    Last edited by MetalInquisitor; May 9th, 2013 at 10:37 AM.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: NooB Question

    Yes, as long as the variable is NOT declared within one of the button click event handlers, then it is visible to the other click handler. The problem you may have been having is that you were holding the filename in a local variable within the click event handler, so the other click handler couldn't see it. This is called variable scoping, and is kind of important....but it's also a guess as to what you were doing on my part.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    295

    Re: NooB Question

    Quote Originally Posted by MetalInquisitor View Post
    You can assign the file path you got from the OFD to a string variable and then use that variable with button2.

    edit: VS is taking foreverr to load, so this is freehand:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Dim filepath As String
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.  
    7.         OpenFileDialog1.ShowDialog()
    8.         filepath = OpenFileDialog1.FileName
    9.  
    10.     End Sub
    11.  
    12.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    13.  
    14.         MessageBox.Show(filepath)
    15.  
    16.     End Sub
    17. End Class
    That did it! Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width