Go4Expert (http://www.go4expert.com/)
-   Visual Basic ( VB ) (http://www.go4expert.com/forums/visual-basic/)
-   -   VB.NET Selective Code Activation??? (http://www.go4expert.com/forums/vbnet-selective-code-activation-t26606/)

deprem88 2Sep2011 15:04

VB.NET Selective Code Activation???
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.

Syperus 6Sep2011 00:04

Re: VB.NET Selective Code Activation???
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

All times are GMT +5.5. The time now is 22:56.