msgbox button type in visual studio - vb.net

How to set a message box which has buttons like "ok" and "cancel"? Also can we name our own names for the buttons? And can we write source codes for those buttons, like
ok.click, form1.show() and if cancel.click form5.show()
msgbox()

You could use MessageBoxButtons enum to specify different kinds of buttons on messagebox and use DialogResult after the button is clicked to specify different actions for each type of messagebox button.
Or create your own MessageBox

Try this:
MessageBox.Show("put your text here", "here the title of you message", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand)
after the second comma [,] you can put the type of buttons and at the last you can put the icon you think it will best suit your message box
regards

This should work:
If (MsgBox("Your Text", MsgBoxStyle.OkCancel, "MessageBox Title") = MsgBoxResult.Ok) Then
Form1.Show()
Else
form5.show()
End If

This is a simple message box:
If MessageBox.Show("some msg", "caption", MessageBoxButtons.OKCancel) = DialogResult.Cancel Then
' you cancel it, so i will show another message box
EndIf

Related

Using a Messagebox like Form with yes/no

Hey there i like to use a nice designed Form with yes no Buttons to use it like the normal yes/no Messagebox. To be clear its not a Messagebox its a Form with a Background Picture and Label (for the Textbody) and 2 Buttons (yes / no).
But what must i do, that it opens the Form and send back the userĀ“s choice that i can work with.
Messagetext = "This is the Text that is shown in the custBox"
Dim custBox As New custBox
CustBox.ShowDialog()
Select Case DialogResult
Case Windows.Forms.DialogResult.Yes
'code if clicked yes
Case Windows.Forms.DialogResult.No
'Code if clicked no
End Select
But i think it does not Work like this.
The DialogResult is the RETURN value from ShowDialog(), so you can do:
Dim cb As New custBox
Dim result As DialogResult = cb.ShowDialog()
Select Case result
Case Windows.Forms.DialogResult.Yes
'code if clicked yes
Case Windows.Forms.DialogResult.No
'Code if clicked no
End Select
Hopefully you're actually setting DialogResult in your other form, when one of the buttons is clicked:
' ... in your "dialog" form, Yes/No Button Handler ...
Me.DialogResult = DialogResult.Yes ' or DialogResult.No

How do I go about trying to validate a textbox upon pressing a button?

I'm having a bit of trouble trying to think of ways to validate this textbox so that it will prevent users from entering numbers or special symbols. I want it so that, upon clicking the "Save" Button (btnSaveJob), it will validate the textbox for the Name. The textbox is for entering names, so I'd like to try and validate it. Here is the code:
Private Sub btnSaveJob_Click(sender As Object, e As EventArgs) Handles btnSaveJob.Click
Dim txtName As TextBox
If String.IsNullOrEmpty(txtName.Text) Then
End If
Try
Dim result As DialogResult
result = MessageBox.Show("Do you want to save the selected job?", "Save Job",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If (result = DialogResult.Yes) Then
Validate()
TimetableBindingSource.EndEdit()
TableAdapterManager.UpdateAll(Me.JobTimetableDataSet)
MessageBox.Show("The Job has been saved successfully.", "Save Job", MessageBoxButtons.OK, MessageBoxIcon.Information)
RefreshData()
btnAddJob.Text = "Add New Job"
Else
'Exit Sub
Return
End If
Catch ex As Exception
MessageBox.Show("Save Job Failed: " & ex.Message.ToString(),
"Save Job", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
To answer the question as asked:
If txtName.Text.Any(Function(ch) Char.IsNumber(ch) OrElse Char.IsSymbol(ch)) Then
'The text is invalid.
End If
That checkes whether any character is a number or a symbol. That may not be exactly what you want though. You can adjust the Boolean expression however you like, while the principle remains the same, e.g.
If txtName.Text.All(Function(ch) Char.IsLetter(ch) OrElse {" "c, "'"c}.Contains(ch)) Then
'The text is valid.
End If
That last one checks that all characters are either letters, spaces or apostrophes. You can add whatever combination of conditions you want.

Issue with radio buttons and message boxes in Visual Basic

In my application for Visual Basic, I have two radio buttons on the third TabPage. I scripted the "No" button to make a message box pop-up if you click it, but when I test it, instead of just showing the message once, it showed the same message again when I selected other option, "Yes".
I tried doing multiple things, but nothing worked. For the radio button, I did a simple line of code like this at first:
MsgBox("insert text here", MsgBoxStyle.OkOnly, "insert title here")
After I found out it appeared when you changed the selection to Yes, I tried doing this:
If RadioButton26_Select() = True Then
MsgBox("insert text here", MsgBoxStyle.OkOnly, "insert title here")
End If
Obviously, that didn't work either. In the first line of code for that radio button, I changed the RadioButton26_CheckedChanged to RadioButton26_Select:
Private Sub RadioButton26_Select(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton26.Select
That line I changed didn't have the () after the Selects, so I put the () after after all the Selects. That didn't work either.
So, I'm really confused here. Any help would be appreciated.
You want to use the RadioButton.Checked property. It indicates whether the RadioButton is "selected" or not.
You should also do it in the CheckedChanged event since that is raised every time the Checked value changes.
Private Sub RadioButton26_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton26.CheckedChanged
If RadioButton26.Checked = True Then
MessageBox.Show("insert text here", "insert title here", MessageBoxButtons.OK)
End If
End Sub
As you see I'm using MessageBox.Show() rather than the MsgBox() function. I recommend you to do so as well since the MsgBox() function exists purely for backwards compatibility with VB6, whereas MessageBox.Show() is the native .NET-way of doing it.

Visual Basic 2010 issue in Clearing Text

I am using Visual Basic 2010.
I have create a simple Textbox which can take multiline input with three buttons clear Text,Date and Show Message.
Assuming the TextBox contains some text.
I want to clear the textbox by clicking "Clear Text" button.
Button1 is the name of the control "Clear Text"
here is the code i used.
Private Sub Button1_Click()
Button1.Text = ""
End Sub
But when i start debugging and click "Clear Text" instead of Textbox getting Cleared the "Clear Text" Control button itself gets Cleared.
Thankyou
because you are clearing the text from button1.
replace Button1.Text = ""
with textbox1.text=""
(textbox1 or the real name of your text box control...)
You can also use the Clear() method:
Clears all the content from the text box.
So that would look like:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Clear()
End Sub

e.Cancel fires but other code also fires when a click event occurs

A form has many buttons. One of those buttons contains code to update the TableAdapterManager. One of the many TextBox controls has code in a Validating event handler. There is code to make sure a US phone number is formatted properly.
If the user tabs out of the TextBox the Validating code works perfectly and show a message to the user if the phone number is not properly formatted and the focus is place in the offending TextBox.
If the user clicks on a button that has code to update the TableAdapterManager the Validating code fires but instead of focus remaining on the offending TextBox, the code in the button Click handler also fires.
I would like to stop the button code from firing.
Here is the code for the Validating event of the TextBox:
Private Sub TextBoxPrimaryPhone_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBoxPrimaryPhone.Validating
' Make sure the phone is formatted correctly.
'--------------------------------------------
If PhoneFormat(TextBoxPrimaryPhone.Text) = "Fix Phone Number" Then
' Alert the user.
'----------------
MessageBox.Show("Please enter a 7 or 10 digit phone number.", _
"Entry Error", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
e.Cancel = True
Else
' Format according to the length of the phone number entered by the user.
'------------------------------------------------------------------------
TextBoxPrimaryPhone.Text = PhoneFormat(TextBoxPrimaryPhone.Text)
End If
End Sub
What additional coding to I need to include so focus remains on the TextBox?
After e.Cancel = True is called do this to set the focus of the offending textbox:
TextBoxPrimaryPhone.Focus()
In your button click event handler you can do this:
TextBoxPrimaryPhone_Validating(sender, New System.ComponentModel.CancelEventArgs)
If Not TextBoxPrimaryPhone.Focused Then
'Do Work
End If