Issue with radio buttons and message boxes in Visual Basic - vb.net

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.

Related

Single code for Tab Key functionality using Enter Key in Vb.Net for a Windows Application

I write this code for tab-like behavior when user presses ENTER on textboxes for every form, which works fine.
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
However, I need to write the code once, perhaps as a sub or function in a module so that I do not have to write the same for every form. I have checked various forums including
Detecting Enter keypress on VB.NET and
Tab Key Functionality Using Enter Key in VB.Net
BUT all I get is either to write code for each textbox or for every individual form.
Has anyone tried out a single code for ALL or may be several selected forms of the application? If yes, please share with me. Writing for every form still works fine for me but i need to take advantage of OOP. Thanks
There are many ways to implement this, one of those is to create a custom textbox User Control
Adding a control to your project is very easy just right click in your project in the solution explorer ->Add->User Control
Give a name to your control ex. "tabedtextbox"
Add a textbox control to your User Control
Place the code in the keypress event of textbox1 of the UserControl
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
End Sub
Compile once in order to update the whole project with the new user control.
Goto to your form and add your new User Control, you can locate it at the Toolbox panel.
Run your program and you will see the behavior of TAB when you press enter on each textbox.
If You wish to allow the user to press enter in a TextBox, instead of pressing a specific button,
you can use the acceptbutton property.
One good way to use it, is to change the property, on the Enter and Leave events of the TextBox. That you call Click event of the Button, when User press Enter inside the TextBox.
Try this code:
Private Sub tbProject_Enter(sender As Object, e As EventArgs) Handles tbProject.Enter
Me.AcceptButton = bSearch
End Sub
Private Sub tbProject_Leave(sender As Object, e As EventArgs) Handles tbProject.Leave
Me.AcceptButton = Nothing
End Sub
Private Sub bSearch_Click(sender As Object, e As EventArgs) Handles bSearch.Click
'.... actions to perfom
End Sub
Screen

vb.net how to solve 2 issues with single and double click on notify icon

I have a vb.net (.NET 3.0) app which has a NotifyIcon in the system tray. I would like the single left-click and double left-click events to do different things; .Click should open the app's context menu, and .DoubleClick should take some default action. So this is my code at the moment:
Private Sub showMenu(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Tray.Click
Debug.Print("click")
If e.Button = MouseButtons.Left Then
Dim mi As MethodInfo = GetType(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance Or BindingFlags.NonPublic)
mi.Invoke(Tray, Nothing)
End If
End Sub
Private Sub defaultAction(ByVal sender As System.Windows.Forms.NotifyIcon, ByVal e As System.EventArgs) _
Handles Tray.DoubleClick
Debug.Print("double click")
doDefaultAction()
End Sub
The first problem is that the .Click handler is fired even for a double-click - it responds to the first click rather than waiting to see if it was actually a double-click. Maybe this is normal behaviour, but is there a 'best practice' way of trapping that occurrence without horrible kludges involving timers? From what I've read on SO, I suspect not. However, that's not the most serious problem...
The second, bigger, problem is that the doDefaultAction() code does various things, one of which is to download an xml file from a specific URL. I'm doing this with this line of code (note not the actual URL:):
Dim reader = XmlReader.Create("http://server.com/genxml.php")
As soon as execution reaches that line, another .Click event is fired, so the debug output looks like this:
click
double click
click
That second click event re-opens the context menu, and because doDefaultAction() goes on to show a modal MessageBox, the menu gets stuck open. I've stepped through in the debugger, and if I 'Step Into' that Dim reader line, I get taken straight to Sub showMenu() above. Very odd. Any ideas what could cause that?

Treeview - Event on Node Click but not Expansion in Visual Basic (VS 2012 V11)

I'm trying to make a help file in Visual Basic. I've decided to go the route of replicating the old style help files with a TreeView panel, to the left, and a RichTextbox , on the right, of the form. (This set-up looks like the help file in PowerShell almost exactly.
I'm trying to make it so that when a TreeView Node is Single Clicked the RichTextbox Text will change to the appropriate text. Here is my code:
Private Sub treeView_NodeMouseClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles TreeViewContents.NodeMouseClick
If e.Node.Text.Equals("Program Help") Then
RTBHelp.Text = Environment.NewLine & "Help text here."
End If
If e.Node.Text.Equals("Program Getting Started") Then
RTBHelp.Text = Environment.NewLine & "Getting Started text here"
End If
End Sub
The problem is that the text will change when simply clicking the Plus or Minus located next to the TreeView Node. But, I want to emulate the PowerShell help behavior, where clicking the Plus or Minus expands or collapses the nodes but does not change the RichTextbox Text. Only when clicking on the Nodes name (Text) itself should the RichTextbox Textchange. I have tried several methods but none seem to work. What do I do?
This might be too late but i just had same problem.
I used the AfterSelect Event.
It is logically that NodeClick Event is fired when one tries to expand the node since we are clicking on the Node by expanding it.
If one is interested on just the Selection done by the mouse then it is necessary to check if e.Action = TreeViewAction.ByMouse.
Private Sub treeView_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeView.AfterSelect
If e.Action = TreeViewAction.ByMouse Then
If e.Node.Text.Equals("Program Help") Then
RTBHelp.Text = Environment.NewLine & "Help text here."
End If
If e.Node.Text.Equals("Program Getting Started") Then
RTBHelp.Text = Environment.NewLine & "Getting Started text here"
End If
End If
End Sub
By using "if TreeViewAction.ByMouse then ...", the code under the if Statement will be excuted if one presses the arrow-keys or the mouse. So the first If Statement is very important if only the mouse selection is to be caught.
Use the AfterSelect event instead.

VB - Change application text with button

I want my button1 to change the text of my form1 from "My Application name" to "My Application name2" by the click of a button.
What I've tried:
form1.text = (textbox1.text)
But no luck.
Any Help?
If you meant about Main Form caption/text .. It has to be work with
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Text = Textbox1.Text
End Sub
You cannot can change the form name at run time. The name of a Form can be anything you want, but it must stay the same throughout. I can't imagine however why would you ever want to change the name in runtime as its not visible to the users. The form's caption on the other hand is a completly different thing, adressed in #matzone's answer.
Try just with following code:
Me.Text = Textbox1.Text
When you say change "Name" you are talking about changing the word that represents the form throughout the coding. This means it can ONLY HAVE ONE NAME because it woudl be a chaos if it had more than one.
When you say change "Text" you are talking about the "Title" of the form. The text that shows up there in the left upper corner.
To change the Name you just need to click the form on the designer and then go to properties and scroll down until you find the Name option.
You can change Text the same way or programatically writing:
Me.Text = "MyApplicationName 2"
Hope it helps.

VB.NET Radio Button Handler Code running twice

I have a group of RadioButtons in VB.NET. I would like to create one function that will handle all of them together. My code is below.
Private Sub employmentStatusChanged(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles rbtnEmployed.CheckedChanged, _
rbtnUnemp.CheckedChanged, rbtnStudent.CheckedChanged, rbtnRetired.CheckedChanged
If rbtnEmployed.Checked Then
Dim employ As New Employer
employ.ShowDialog()
ElseIf rbtnStudent.Checked Then
Dim stud As New Students
stud.ShowDialog()
ElseIf rbtnRetired.Checked Then
Dim employ As New Employer
employ.ShowDialog()
End If
End Sub
This function works fine the first time I click a button. The problem comes when I click on a different button. It fires once for the first button's changed state (from checked to unchecked) and then again for the second button (unchecked to checked).
Any ideas on how to stop this from happening? Thanks in advance!
You can't. However, sender is the radio button that was clicked. Just check the state of that. If sender.Checked is False, return from the event handler.