VB.NET Selective Code Activation???

Discussion in 'Visual Basic ( VB )' started by deprem88, Sep 2, 2011.

  1. deprem88

    deprem88 New Member

    Sep 2, 2011
    Likes Received:
    Trophy Points:
    Hi all,

    This is my first post on this forum, but I am a long time fan of this site. I would like to thank all those who offer help, especially to noobs like myself. Now on to my question.

    I have a program in VB.NET with the option to open two different files, one is an excel and the other is notepad. Each contain relevant information that is extracted using various user selections via buttons. What I want is to use the same buttons for both file formats, though the actual coding of what the button does for each differs. The two files are not opened at the same time.

    For now I am trying to set it up where, if you open an excel then the coding on the button only works for the excel and vice versa with notepad.

    Can anyone give me some help regarding this? Thanks.
  2. Syperus

    Syperus New Member

    Sep 2, 2011
    Likes Received:
    Trophy Points:
    Just to make sure I'm understanding the question, your wanting to have 1 button work with 2 different file formats, but not at the same time? If that's correct there's a few different ways to go about this depending on what your wanting. Since your going to have different instructions depending on the file format your working with, I would create a function for each file format that you want to use and if your wanting to only work on 1 file format at a time, i would have radial buttons for you to tell the program which way to go about it. So for this it would be something like:

    Public Class Form1
        'I'm setting these to global variables so you don't have to redefine them for each function
        Dim excelFile As String = "C:\file1.xlsx" 'string to read to your excel file. Change accordingly 
        Dim notepadFile As String = "C:\file1.txt" 'string to read your text file. Change accordingly 
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'You have to make 2 radial buttons named excelRBTN and notepadRBTN or just change the names in here
            'Check if our excel radial button is checked, if yes then execute
            If excelRBTN.Checked = True Then
                excelFunction() 'Call all statements in this function
                'You could just use Else instead of Elseif but this will be executed anytime you press the button and excelRBTN is not checked.
                'If notepadRBTN is checked, execute.
            ElseIf notepadRBTN.Checked = True Then
                notepadFunction() 'call all statements in this function
            End If
        End Sub
        Function excelFunction()
            'Statements to execute for your excel file
            'I put a messagebox in here to show you how it works
            MessageBox.Show("Excel function executed")
        End Function
        Function notepadFunction()
            'Statements to execute for your notepad file. 
            MessageBox.Show("Notepad function executed")
        End Function
    End Class
    Just throw that into your compiler and run it to see how it works. From there alter to use for your needs. I wrote this in VS 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice