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
Related
Here is the code:
Private Sub btn_selectfile_Click(sender As Object, e As EventArgs) Handles btn_selectfile.Click
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files | *.txt"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'some code here
ElseIf OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
OpenFileDialog1.Dispose()
End If
End Sub
It also happens if I reverse them and put the DialogResult.OK in the ElseIf when selecting a file.
How shall I proceed? Thanks for your help.
Call ShowDialog once, save the result, and then check it. Currently, you're calling ShowDialog twice, which shows the dialog to the user twice.
Dim result As DialogResult = OpenFileDialog1.ShowDialog();
If result = Windows.Forms.DialogResult.OK Then
'some code here
ElseIf result = Windows.Forms.DialogResult.Cancel Then
OpenFileDialog1.Dispose()
End If
I guess, when you cancel the dialog, you want to exit procedure. In this case you just need to check if the result is Cancel:
If OpenFileDialog1.ShowDialog() = DialogResult.Cancel Then Exit Sub
After that line the result is OK, so you can safely get file path.
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
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
I'm making this music player thing and I need help with some coding. I want my program to play a certain file when Checkbox1 is checked. I'm using OpenFileDialog but i'm not sure that's the right thing to do. I can't get it to work. Here's my code:
If CheckBox1.Checked = True Then
OpenFileDialog1.OpenFile()
AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
AxWindowsMediaPlayer1.Ctlcontrols.play()
ElseIf CheckBox1.Checked = False Then
AxWindowsMediaPlayer1.Ctlcontrols.stop()
End If
Can someone please help me?
You need to show the dialog:
If CheckBox1.Checked Then
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
AxWindowsMediaPlayer1.Ctlcontrols.Play()
End If
Else
AxWindowsMediaPlayer1.Ctlcontrols.Stop()
End If
If you want the dialog to display so the user can select a file, use ShowDialog() and check the return value to ensure a file was actually selected by the user:
If CheckBox1.Checked = True Then
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
AxWindowsMediaPlayer1.Ctlcontrols.play()
End If
ElseIf CheckBox1.Checked = False Then
AxWindowsMediaPlayer1.Ctlcontrols.stop()
End If
When I try to run this code it opens an open dialog with no results, then it opens another and does what I want it to do. Help?
Private Sub mnuOpen_Click(sender As Object, e As EventArgs) Handles mnuOpen.Click
Dim DidWork As Integer = openFD.ShowDialog()
openFD.InitialDirectory = "C:\"
openFD.Title = "Open a text file"
openFD.Filter = "Text Files|*.txt|Word Docs|*.doc"
openFD.ShowDialog()
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = openFD.FileName
MsgBox(strFileName)
End If
End Sub
You don't need the DidWork variable in your example (and as pointed out, you are calling ShowDialog twice).
I would favor checking for Ok instead of Cancel in the DialogResult:
openFD.InitialDirectory = "C:\"
openFD.Title = "Open a text file"
openFD.Filter = "Text Files|*.txt|Word Docs|*.doc"
If openFD.ShowDialog() = DialogResult.Ok Then
strFileName = openFD.FileName
MsgBox(strFileName)
Else
MsgBox("Dialog Canceled")
End If