Rizky92 Rizky92 - 1 year ago 118
Vb.net Question

How do I put a performclick in an if statement

I have a problem.

I have 4 forms, 3 forms have the same button used for 1 form. I want to change the label in form 1 if one of these 3 forms button is pressed with performclick. But when I test it, it shows error "expression does not produce a value".

Is there any way to avoid that?

Here's my code:

If FormPetugas.Button2.PerformClick Then
Label1.Text = "Kode Petugas"
Label2.Text = "Nama Petugas"
Label3.Text = "Jenis Kelamin"
Label4.Text = "Alamat Petugas"
Label5.Text = "Nomor Telpon"
Label6.Text = "Username"
Label7.Text = "Password"
Label8.Text = "Hak Akses"


ComboBox2.Items.Add("User Terpercaya")
ComboBox2.Items.Add("User Biasa")
ElseIf FormKaryawan.Button2.PerformClick Then
Label1.Text = "Kode Karyawan"
Label2.Text = "Nama Karyawan"
Label3.Text = "Jenis Kelamin"
Label4.Text = "Alamat Karyawan"
Label5.Text = "Nomor Telpon"
Label6.Text = "Tanggal Pendaftaran"
Label7.Text = "Ruangan"
Label8.Text = "Status"


ComboBox2.Items.Add("Tidak Aktif")
End If

Answer Source

You can avoid it by removing PerformClick() from the If-statement. PerformClick() performs a fake click on the button, it is a method which does not return a value. You cannot check it in an If-statement.

You won't be able to check if a button was clicked with it because what PerformClick() does is just to invoke the paint event to make it look like the button is pressed, then it just calls OnClick(EventArgs.Empty) to raise all subscribed Click events.

Refer to the Reference Source.

To check for regular clicks you would have to subscribe to the Click event:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Do stuff.
End Sub