Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a Sub multiple times

Status
Not open for further replies.

Yoxy99

MIS
Jul 28, 2007
23
0
0
US
Hello , I have an array that I run from two buttons and for each buttons event it has the exact same code except one button reads from 1 text file and the other button reads from a different text file. How can I use this code one time instead of having to use it for both buttons but be able to change the one line of code that tells it which file to read from?
 
Suppose you have a function like this
Code:
    Function Message(ByRef CTL As Control, ByVal MSG As String) As String
        Return MessageBox.Show(MSG & " from " & CTL.Name)
    End Function

You will call it from button1 or button2 like this
Code:
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Message(sender, "Good morning")
    End Sub
Code:
    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Message(sender, "Hello")
    End Sub


________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Code:
Public sub ReadFromFile(vstrFileName as string)
...
'This function is written such that in reads the file pointed to in the argument... 
...
end sub

public sub ButtonHandler(sender as Object, e as EventArgs) Handles but1.Click, but2.click

	if sender is but1 then 
		ReadFromFile("File1.txt")
	elseif sender is but2 then 
		ReadFromFile("File2.txt")
	endif

end sub

(Not tested)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top