Hi there,
I am new to VB.Net, i have cracked my brain to figure out on how to use if else statements in form1 to call button.click from another class. Please help me! :(
THANK YOU SO MUCH! I REALLY APPRECIATE IT! :)
Hi there,
I am new to VB.Net, i have cracked my brain to figure out on how to use if else statements in form1 to call button.click from another class. Please help me! :(
THANK YOU SO MUCH! I REALLY APPRECIATE IT! :)
do you mean calling button click of a button on another form????
Because i need to identify which button is the sender to initiate the codes in the class. That is why i need to know on how to identify the button in the form.
can u provide some coding so i can understand situation much better ?
can u provide some coding so i can understand situation much better ?
Public Sub retrieveRec()
'insert if else statement to call individual buttons
If Arr.Length > 0 Then
mcmd.CommandText = "Select * from table1"
mconn.Open() 'establish connection with database
Dim rdr As SqlDataReader
rdr = mcmd.ExecuteReader 'for retrieving 1 or more values
Dim i As Byte
Do While rdr.Read()
Arr(i) = New quiz
Arr(i).sQuestion = rdr.GetValue(0)
Arr(i).soptionA = rdr.GetValue(1)
Arr(i).soptionB = rdr.GetValue(2)
Arr(i).soptionC = rdr.GetValue(3)
Arr(i).soptionD = rdr.GetValue(4)
Arr(i).sphoto = rdr.GetValue(7)
i += 1
Loop
End If
End Sub
I am receiving all the information from Database. That is why i am using the Array to retrieve in from database.
so u want to find which button called this retrieveRec() sub??
so u want to find which button called this retrieveRec() sub??
Yes, i have 3 buttons in another form, i want to identify which button is the caller so that i can assign the values to the form.
suppose i have three buttons named
button1
button2
button3
and i want to find which button called the retrieveRec()
modify retrieveRec
here is sample code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Sub retrieveRec(ByVal btnname As String)
If btnname = "Button1" Then
MessageBox.Show("Called From Button1")
' Do your Coding here
End If
If btnname = "Button2" Then
MessageBox.Show("Called From Button2")
' Do your Coding here
End If
If btnname = "Button3" Then
MessageBox.Show("Called From Button3")
' Do your Coding here
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
retrieveRec("Button1")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
retrieveRec("Button2")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
retrieveRec("Button3")
End Sub
End Class
Another approach.
Public Class Form1
Private myCoolClass As New testClass
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myCoolClass.retrieveRec(Button1)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
myCoolClass.retrieveRec(Button2)
End Sub
End Class
Public Class testClass
Public Sub retrieveRec(ByVal myCoolSelectedButton As Button)
Select Case myCoolSelectedButton.Name
Case "Button1"
MsgBox("Button1 is the Sender.")
Case "Button2"
MsgBox("Button2 is the Sender.")
End Select
End Sub
End Class
Now VB.Net is give me a problem, in my class it says that
'constructor for connection and command
Sub New()
'connection
mconn = New SqlClient.SqlConnection
mconn.ConnectionString = connstr
'command object
mcmd = mconn.CreateCommand
initarray()
retrieveRec()'this is giving me an error Argument not specified for parameter 'selectbutton' of 'Public sub retrieveRec(selectbutton as system.window.forms.button)'.
End Sub
what should i declare inside?
In that case I would use the "Optional", where you have the "Option" to send a Button or Not.
Public Sub retrieveRec(Optional ByVal myCoolSelectedButton As Button = Nothing)
i guess while u create an instance u dont call this method through any button..
but after it u call it from other buttons..
change it as follows
Dim demo As New Button
retrieveRec(demo)
its just a temporary button
In that case I would use the "Optional", where you have the "Option" to send a Button or Not.
Public Sub retrieveRec(Optional ByVal myCoolSelectedButton As Button = Nothing)
When i use optional, my welcome screen with this code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Category.Show()'An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.
Me.Close()
End Sub
I'm not sure exactly to "why" this occurs, but you can try the solution by sandeepparekh9 and use:
Sub New()
Category.retrieveRec(New Button)
End Sub
..and remove the Optional, as it was originally posted.
Can you pass it Nothing and test for it?
e.g.
Public Class Form1
Private myCoolClass As New testClass
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click 'etc
myCoolClass.retrieveRec(Sender)
End Sub
End Class
Public Class testClass
Public Sub retrieveRec(ByVal Sender As System.Object)
If Not Sender Is Nothing then
Select Case Ctype(Sender,Button).Name
Case "Button1"
MsgBox("Button1 is the Sender.")
Case "Button2"
MsgBox("Button2 is the Sender.")
End Select
Else
MsgBox("Sub New is the Sender.")
End if
End Sub
End Class
and
Sub New()
'connection
mconn = New SqlClient.SqlConnection
mconn.ConnectionString = connstr
'command object
mcmd = mconn.CreateCommand
initarray()
retrieveRec(Nothing)'this is giving me an error Argument not specified for parameter 'selectbutton' of 'Public sub retrieveRec(selectbutton as system.window.forms.button)'.
End Sub
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.