How do I put a performclick in an if statement - vb.net

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"
ComboBox1.Items.Clear()
ComboBox1.Items.Add("Laki-laki")
ComboBox1.Items.Add("Perempuan")
ComboBox2.Items.Clear()
ComboBox2.Items.Add("Administrasi")
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"
ComboBox1.Items.Clear()
ComboBox1.Items.Add("Laki-laki")
ComboBox1.Items.Add("Perempuan")
ComboBox2.Items.Clear()
ComboBox2.Items.Add("Aktif")
ComboBox2.Items.Add("Tidak Aktif")
ComboBox2.Items.Add("Berhenti")
End If

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

Related

Multiple buttons, one event to change clicked button colour

The code shown below should allow any button on the page to change colour except for the ones named in the first if statement. This code was working but now does nothing when the button is clicked. The button should turn yellow but just stays the default colour. Also is there anyway I can manipulate the code so only one button can be red at a time instead of allowing multiple red buttons. When reading into this. I cannot find any help for vb. Can anyone help?
Personally, I think it may be to do with thePublic Sub since the message box does not appear when a field is empty.
Public Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Try
Dim btn As Button = sender
If btn.Name = "BtnUpdate" Or btn.Name = "BtnBackCust" Or btn.Name = "BtnConfirm" Then
ElseIf TxtFirstName.Text = "" Or TxtLastName.Text = "" Or TxtAddress.Text = "" Or cboCountry.SelectedItem = "" Or cboRoomType.SelectedItem = "" Then
MsgBox("You must populate all fields")
Else
btn.BackColor = Color.Red
btn.Text = ChosenRoom
End If
Catch ex As Exception
End Try
End Sub
Instead of using the MyBase.Click Event, create an Handle for each Button on your Form Load:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each Button As Button In Me.Controls.OfType(Of Button)()
If Button.Name <> "BtnUpdate" AndAlso Button.Name <> "BtnBackCust" AndAlso Button.Name <> "BtnConfirm" Then
AddHandler Button.Click, AddressOf ChangeColor
End If
Next
End Sub
The ChangeColor sub, also create the RedButton variable to keep track of what is the currently red button:
Private RedButton As Button = Nothing
Private Sub ChangeColor(Sender As Object, e As EventArgs)
If TypeOf Sender Is Button Then
If TxtFirstName.Text = "" OrElse TxtLastName.Text = "" OrElse TxtAddress.Text = "" OrElse cboCountry.SelectedItem = "" OrElse cboRoomType.SelectedItem = "" Then
MsgBox("You must populate all fields")
Else
Dim SenderButton As Button = Sender
If RedButton IsNot Nothing Then
RedButton.BackColor = Me.BackColor
End If
If SenderButton IsNot RedButton Then 'This if will toogle the button between Red and the Normal color
SenderButton.BackColor = Color.Red
End If
RedButton = Sender
End If
End If
End Sub

VB SaveFileDialog if cancel then

When the user presses the "Cancel" button in SaveFileDialog I want to rename a textlabel.
Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Label6.Text = "Saved!"
End sub
This is working for the "Save" Button. I don't know how to do it for the "Cancel" button.
Thanks
Instead of using that event, why not compare the result returned from the ShowDialog() method?
If SaveFileDialog1.ShowDialog() = DialogResult.Ok
Label6.Text = "Saved!"
Else
Label6.Text = "Cancelled!"
End If
I prefer the use of cases for something like this
Select Case SaveFileDialog1.ShowDialog()
Case DialogResult.Ok
Label6.Text = "Saved!"
Case DialogResult.Cancel
Label6.Text = "Cancelled!"
End Select

VB.NET - Deleted row information cannot be accessed through the row

This is my Delete button code. When I click on the Delete button, it displays the error message "Deleted row information cannot be accessed through the row." So, what will be the possible solution?
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If (i < ds.Tables(0).Rows.Count - 1) Then
Dim r1 As DataRow
'r1 = ds.Tables(0).NewRow(i)(0)
'r1.Delete()
'Me.da.Update(ds.Tables(0))
ds.Tables(0).Rows(i).Delete()
ds.Tables(0).GetChanges(DataRowState.Deleted)
'ds.Tables(0).AcceptChanges()
'ds.AcceptChanges()
'Me.da.Update(Me.ds.Tables(0).Rows(i)("cust_id")).ToString()
'Me.da.Update(Me.ds.Tables(0).Rows(i)("cust_id"))
Me.da.Update(Me.ds.Tables(0).Rows(i).Item(0))
' Me.da.Update(Me.ds.Tables(0))
' ds.Tables(0).AcceptChanges()
i = i + 1
TextBox1.Text = ds.Tables(0).Rows(i)("cust_id").ToString()
TextBox2.Text = ds.Tables(0).Rows(i)("fname").ToString()
TextBox3.Text = ds.Tables(0).Rows(i)("sname").ToString()
TextBox4.Text = ds.Tables(0).Rows(i)("lname").ToString()
TextBox5.Text = ds.Tables(0).Rows(i)("address").ToString()
TextBox6.Text = ds.Tables(0).Rows(i)("city").ToString()
TextBox7.Text = ds.Tables(0).Rows(i)("state").ToString()
TextBox8.Text = ds.Tables(0).Rows(i)("contact1").ToString()
TextBox9.Text = ds.Tables(0).Rows(i)("contact2").ToString()
TextBox10.Text = ds.Tables(0).Rows(i)("email").ToString()
End If
End Sub
You have the data shown in a grid or another control and when you delete it, the information is refreshed on the screen and this is when the control throws the error that the information cannot be accessed.
Before you delete a row in your table, you need to make sure it is removed from any UI elements, like binding sources or grids.

Is there any way for Retrieve Record Value from Table Column , Loop and Loop in vb.net

Sorry for my poor English
When the user clicks the Start button, I am trying to display all of the records from the Phone column in the Textbox1 control. I want to see all those records pass into Textbox1 While it is processing the For loop. But it is currently processing very fast so that I only see the last record in Textbox1. What i'm going wrong?
While it is processing the For loop, I change the Start button to a Stop button. So when I click the button and it currently has a Text value equal to "Stop", I want it to skip the For loop and pass the value from TextBox1 to my FirstWin. And then it should change the BtnStart.Text back to "Start"
Here's my code:
Public Class PhoneFortune
Dim CN As OleDbConnection
Dim CM As OleDbCommand
Dim DA As OleDbDataAdapter
Dim DT As New DataTable
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
If BtnStart.Text = "Start" Then
CM = New OleDbCommand("SELECT * FROM TblPhoneNumber", CN)
DA = New OleDbDataAdapter(CM)
DA.Fill(DT)
For i = 0 To DT.Rows.Count - 1
TextBox1.Text = DT.Rows(i)("Phone")
Next
BtnStart.Text = "Stop"
End If
If BtnStart.Text = "Stop" Then
If FirstWin.Text = "" Then
FirstWin.Text = TextBox1.Text
BtnStart.Text = "Start"
ElseIf SecondWin.Text = "" Then
SecondWin.Text = TextBox1.Text
BtnStart.Text = "Start"
ElseIf ThirdWin.Text = "" Then
ThirdWin.Text = TextBox1.Text
BtnStart.Text = "Start"
End If
End If
End Sub
Private Sub PhoneFortune_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CN = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database\db.accdb;Jet OLEDB:Database Password=12345;")
CN.Open()
End Sub
End Class
I hope we are on the same page but if u want to slow down the process for a bit u use these two lines of code within the FOR loop
System.Threading.Thread.Sleep(1000)
Me.Refresh() Where 1000 is one second(value is represented in milliseconds).
Now what is that FirstWin.Text="" etc used for? Are you trying to start the looping,then change the text to "stop" so that when the user clicks stops it stops at the current record? Go to youtube and look for multithreading videos,not sure how to help u on that one hope the rest helps. If this is useful please acknowledge
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "Start" Then
If TextBox1.Text = "" Then
t1 = New Thread(AddressOf Me.PhoneThread)
t1.Start()
Button1.Text = "Stop"
Else
t1.Resume()
Button1.Text = "Stop"
End If
Else 'Click Stop
t1.Suspend()
Button1.Text = "Start"
If FirstWin.Text = "" Then
FirstWin.Text = TextBox1.Text
ElseIf SecondWin.Text = "" Then
SecondWin.Text = TextBox1.Text
ElseIf ThirdWin.Text = "" Then
ThirdWin.Text = TextBox1.Text
End If
End If
End Sub
Sub PhoneThread()
'Dim ThreadsArray As List(Of Threading.Thread) = New List(Of Threading.Thread)
Dim s As Integer
'MsgBox(DT.Rows.Count)
For s = 0 To DT.Rows.Count
If s = DT.Rows.Count Then
s = 0
Else
TextBox1.Text = DT.Rows(s)("Phone")
' ThreadsArray.Add(t1)
Thread.Sleep(19)
'TextBox1.Text = s.ToString()
Me.Refresh()
End If
Next
End Sub
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
Me.Close()
End Sub
For something simple like this, System.Threading.Thread.Sleep(100) (to block the thread) followed by Application.DoEvents() (to process any button clicks) seems like it would do the trick.
For something more complicated, this approach will result in re-entrancy and difficult-to-reproduce bugs; in that case, you definitely need asynchronous programming.

Display an image when button is clicked

I need to display two images when the button is clicked. First the user will browse for the two images and after clicking the third button it should display the two images. I have this code so far. I'm a complete newbie in vb
Dim dialog As New OpenFileDialog()
If DialogResult.OK = dialog.ShowDialog Then
TextBox2.Text = dialog.FileName
End If
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fdialog As New OpenFileDialog()
fdialog.FileName = String.Empty
fdialog.Multiselect = True
If fdialog.ShowDialog = DialogResult.OK Then
If fdialog.FileNames.Length = 2 Then
PictureBox1.Image = System.Drawing.Image.FromFile(fdialog.FileNames(0))
TextBox1.Text = fdialog.FileNames(0)
PictureBox2.Image = System.Drawing.Image.FromFile(fdialog.FileNames(1))
TextBox2.Text = fdialog.FileNames(1)
ElseIf fdialog.FileNames.Length = 1 Then
PictureBox1.Image = System.Drawing.Image.FromFile(fdialog.FileName)
TextBox1.Text = fdialog.FileName
PictureBox2.Image = Nothing
TextBox2.Text = String.Empty
End If
End If
End Sub
the following code goes in the click event of the "load" button.
regards ...