Vb messagebox click no and keep text properties - vb.net

I have a button that clears all text and combobox fields, I would like to give the user a yes or no option to have a yes clear all fields and no leave them the way they were when the button was clicked. I have the code for the yes option i just need the no part. Thanks.
Private Sub btnNewForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewForm.Click
MsgBox("Are you sure you would like to clear the form", MsgBoxStyle.YesNo, "Confirm Delete")
If MsgBoxResult.Yes Then
For Each ctrl In Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
ctrl.Text = String.Empty
End If
Next
ElseIf MsgBoxResult.No Then
??????????????????
End If
End Sub

You need to check the results of your MessageBox:
Dim results As DialogResult = MsgBox("Are you sure you would like to clear the form", MsgBoxStyle.YesNo, "Confirm Delete")
If results = DialogResult.Yes Then
For Each ctrl In Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
ctrl.Text = String.Empty
End If
Next
End If
There is no reason to do anything for "No" since you don't want to change anything in regards to that answer.

You don't use that correctly. What you are actually doing there is....: Compare MsgboxResult.Yes with True.
Note that MsgBox() is actually a function that returns the button the user has clicked.
So do
Dim res as MsgBoxResult = MsgBox(...)
If res = MsgBoxResult.Yes then
'Code for Yes
Else
'Code for No
Endif
The MsgBox() function is also legacy VB6 stuff. It would be better to use
Dim res as DialogResult = MessageBox.Show("Message", "Title", ...)

Related

Close VB.Net form without message box if textboxes are empty

I am currently trying to fix the form closing event in VB.Net so that when there is text in txtInput1 and txtInput2 when the user tries to exit the form it will exit with a warning prompt from the message box. This works, however it should not show this prompt if there is nothing in either box, the form should just close. Here is the form close event i have so far, but it is not working:
'FormClosing Event
Private Sub MyForm_Closing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If (txtInput1.Text = "" And txtInput2.Text = "") Then
Me.Close()
End If
If MessageBox.Show("Are you sure you sure you want to exit?", "Exit", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
End Sub
Firstly, don't call Close in the FormClosing event handler. If that event is raised then the form is already closing UNLESS you specifically prevent it, i.e. unless you set e.Cancel to True.
As for the issue, if you want to display the message ONLY IF a certain condition is true then it should be obvious that the code that displays it must be inside an If block. Think about what it is you want to do and do that. Write it down if you need to, so you have something to actually compare your code to. You want to display the message if only one TextBox is empty, so that's what you need to test for and do:
If (txtInput1.Text = String.Empty AndAlso txtInput2.Text <> String.Empty) OrElse
(txtInput1.Text <> String.Empty AndAlso txtInput2.Text = String.Empty) Then
If MessageBox.Show("Are you sure you sure you want to exit?", "Exit", MessageBoxButtons.YesNo) = DialogResult.No Then
e.Cancel = True
End If
End If
If you think a bit harder about the condition then you can simplify that code somewhat. If you create a list of the Text values from the TextBoxes then you only want to display the message if that list contains one empty String:
If {txtInput1.Text, txtInput2.Text}.Count(Function(s) s = String.Empty) = 1 Then
If MessageBox.Show("Are you sure you sure you want to exit?", "Exit", MessageBoxButtons.YesNo) = DialogResult.No Then
e.Cancel = True
End If
End If

VB.NET: DialogResult.No is not closing the form

Using Message Box DialogResult.No Condition for closing the form doesnot perform as expected.
The formclosing event aske user whether to save the document or not before closing.
The following is my FormClosing event.
Private Sub PDFViewSimple_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
Handles Me.FormClosing
If doc.IsModified Then
Dim message As String = "The document is modified, would you like to save it?"
Dim caption As String = "File Not Saved"
Dim buttons As MessageBoxButtons = MessageBoxButtons.YesNo
Dim DefaultButton As MessageBoxDefaultButton = MessageBoxDefaultButton.Button1
Dim icon As MessageBoxIcon = MessageBoxIcon.Question
Dim result As DialogResult
' Displays A MessageBox.
result = MessageBox.Show(message, caption, buttons, icon, DefaultButton)
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
Me.Close()
ElseIf (result = DialogResult.No) Then
Me.Close() ''Should I replace with (Application.Exit)
End If
End If
End Sub
There's all sorts wrong with that code. Firstly, given that there are only two options, using ElseIf is pointless, although not strictly wrong. If it's not Yes then it must be No, so you'd only need an Else:
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
Me.Close()
Else
Me.Close()
End If
Next, even an Else is pointless because you're calling Close regardless of the outcome. All you need to do is check for Yes, do anything specific to Yes and then call Close regardless:
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
End If
Me.Close()
Finally, you shouldn't be calling Close at all. You're in the FormClosing event handler so the form is already closing. You only have to do something if you want the form to NOT close. So, all you need is this:
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
End If
If you wanted the form to NOT close then you would set e.Cancel to True.

Disable textboxes with the corresponding checkboxes in VB.net

Public Class Form1
Dim controlNames() As String = {"Tea", "Cola", "Coffee", "Orange", "Water", "VanillaCone", "VanillaShake", "StrawberryShake", "ChocolateMilkshake", "Fries", "Salad", "Hamburger", "OnionRings", "ChickenSalad", "FishSandwich", "CheeseSandwich", "ChickenSandwich"}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
For Each ctrl As Control In Me.Panel2.Controls
If TypeOf ctrl Is TextBox Then
ctrl.Enabled = False
End If
Next
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs)
For i = 0 To 16
For Each ctrl As Control In Me.Panel2.Controls
If TypeOf ctrl Is CheckBox Then
If ctrl.Name = "chk" & controlNames(i) Then
If DirectCast(ctrl, CheckBox).CheckState = CheckState.Checked Then
If TypeOf ctrl Is TextBox Then
If ctrl.Name = "txt" & controlNames(i) Then
ctrl.Enabled = True
End If
End If
End If
End If
End If
Next
Next
End Sub
I have a VB.NET assignment and I am attempting to enable textboxes based on whether or not the checkbox with the same name was checked.
This is my code so far and it obviously doesn't work. What I want to do essentially:
All textboxes start as disabled. Then, textboxes only get enabled if the corresponding checkboxes are checked. For example, if chkTea is checked, then it enables txtTea.
I am aware I can copy paste something like "if chkTea = checked then txt.tea = enabled". I do not want to do this as this seems like a poor way to go about this. I want to know if I can do something like my barely readable code shows.
If you're renaming all your controls to something different to the default name with a number, this code should work just fine.
You dont need a timer, it just fires when any of the CheckBoxes have their state changed.
In the example code, I've created a CheckedChanged handler and set it to handle some of the CheckBoxes (You'll want to add all those you want to handle). If you click on any of these CheckBoxes, the handler will fire. The handler then passes which checkbox has been changed to the SyncTextBoxWithCheckBoxState method. This then finds the matching textbox using the FindMatchingCheckBox method and sets the .Enabled state of the Text box to the same as the .Checked state of the CheckBox
Private Sub chkTea_CheckedChanged(sender As Object, e As EventArgs) Handles chkTea.CheckedChanged, chkCoffee.CheckedChanged, chkCola.CheckedChanged, chkOrange.CheckedChanged, chkTea.CheckedChanged, chkVanillaCone.CheckedChanged, chkVanillaCone.CheckedChanged, chkWater.CheckedChanged
SyncTextBoxWithCheckBoxState(CType(sender, CheckBox))
End Sub
Private Sub SyncTextBoxWithCheckBoxState(chkBox As CheckBox)
Dim txtBox As TextBox = FindMatchingTextBox(chkBox)
txtBox.Enabled = chkBox.Checked
End Sub
Private Function FindMatchingTextBox(chkbox As CheckBox) As TextBox
For Each ctrl As Control In Panel2.Controls
If ctrl.GetType Is GetType(TextBox) And ctrl.Name.Contains(chkbox.Name.Substring(3)) Then
Return CType(ctrl, TextBox)
End If
Next
Return Nothing
End Function
EDIT
To have the code target more than one panel, add all the checkboxes you want to detect to the event handler like before, and in the FindMatchingTextBox method, just add another loop around the existing one to loop through each panel. Like so ..
Private Function FindMatchingTextBox(chkbox As CheckBox) As TextBox
'This is the new loop which loops through the two panels. It's
'a bit quick and dirty, but it works
For Each pnl As Panel In New Panel() {Panel2, Panel3}
'In this line note that the reference to Panel2 now refers to pnl
For Each ctrl As Control In pnl.Controls
If ctrl.GetType Is GetType(TextBox) And ctrl.Name.Contains(chkbox.Name.Substring(3)) Then
Return CType(ctrl, TextBox)
End If
Next
'End point of the new loop
Next
Return Nothing
End Function
After you've looped through each checkbox, you are only concerned with that checkbox, so when you find the correct checkbox, you need to then RELOOP back through all the controls on the form checking if they are the corresponding textbox.
Something like the below should work, or at the very least start you off down the right path:
Dim controlNames() As String = {"Tea", "Cola", "Coffee", "Orange", "Water", "VanillaCone", "VanillaShake", "StrawberryShake", "ChocolateMilkshake", "Fries", "Salad", "Hamburger", "OnionRings", "ChickenSalad", "FishSandwich", "CheeseSandwich", "ChickenSandwich"}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
For Each ctrl As Control In Me.Panel2.Controls
If TypeOf ctrl Is TextBox Then
ctrl.Enabled = False
End If
Next
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs)
For i = 0 To 16
For Each ctrl As Control In Me.Panel2.Controls
If TypeOf ctrl Is CheckBox Then
If ctrl.Name = "chk" & controlNames(i) Then
If DirectCast(ctrl, CheckBox).CheckState = CheckState.Checked Then
For Each ctrl As Control In Me.Panel2.Controls
If TypeOf ctrl Is TextBox Then
If ctrl.Name = "txt" & controlNames(i) Then
ctrl.Enabled = True
End If
End If
Next
End If
End If
End If
Next
Next
End Sub

make control for button

I have wanted to ask my problem.
I make 3 buttons, button 1, 2 and 3. so when I click one button automatic button changes color. I'm using code like this
For Each ctrl As Control In frm.Controls
If ctrl = button Then
ctrl.backcolor = color.red
End If
Next
but still error. please help me
The right code would be:
For Each ctrl As Control In frm.Controls
If TypeOf ctrl Is Button Then
DirectCast(ctrl,Button).BackColor = Color.Red
End If
Next
Use following code :
For Each ctrl As Control In Controls
If TypeOf ctrl Is Button Then
ctrl.BackColor = Color.Red
End If
Next
What you are doing wrong is compare an instance with a type. What you need to do is compare Type of an instance to another Type.
This isn't the best way. Have a look at the below option.
Sub buttons_click(sender as Object, e as event) Handles button1.Click,
_ button2.Click,
_ button3.Click
sender.backcolor = color.red
End Sub
Sorry if the syntax is a bit off, it's a while since i've done vb.
Hope this helps.

Help with events

I have multiple textboxes which I want them to perform the same thing upon clicking them. By default I can use the handles textbox1.click for 1 single textbox as shown below but I am not sure how to do handle multiples of them. Of course I can write a handler for every single textbox but I have about 50 of them. I am sure there must be a more efficient way. Please advice. Thanks.
Sub TextBox1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
If Button9.Text = "Make Changes" Then
If TextBox2.Text <> "" Then
Frm_Cine1.Show()
Frm_Cine1.chooseCine(ComboBox1.SelectedItem)
Else
MsgBox("Please check input!")
Exit Sub
End If
End If
End Sub
why don't you create a customizable textbox?
If Button9.Text = "Make Changes" Then
If TextBox2.Text <> "" Then
These two lines are going to be same for all those 50 buttons?
If yes, then I think you can assign same event handler to each of the button's click event.
Other way is, create one private method which takes one string as an argument and returns boolean value depending on whether your string is blank or not and call this method from all the 50 button's click event.
Thanks for all your advices, I am not sure if this is what you guys are suggesting but apparently it is how I wanted it to work:
Sub TextBoxs_click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles TextBox2.Click, TextBox3.Click, TextBox4.Click 'This part is disturbing if I have 50 textboxes...
'For Each obj As Control In Panel2.Controls
If sender.GetType.ToString = "System.Windows.Forms.TextBox" Then
Dim txtbox As TextBox = sender
textbox_verification(txtbox)
End If
'Next
End Sub
Sub textbox_verification(ByVal txtbox As TextBox)
If Button9.Text = "Make Changes" Then
If txtbox.Text <> "" Then
Frm_Cine1.Show()
Frm_Cine1.chooseCine(ComboBox1.SelectedItem, "FILE1-->This should be a variable")
Else
MsgBox("Please check timings input!")
Exit Sub
End If
End If
End Sub
If you indeed need to use the same click handler for multiple test boxes, you can use the AddHandler command to associate the click event of each test box with the handler routine, as shown:
AddHandler TextBoxX.Click AddressOf TextBox1_Click
You will need to add this statement to your program (maybe in the form load routine) once for each text box you want to handle. (Using the name of each text box in place of the "TextBoxX" in the above code.)