my button can't handle a click event all it says is "'btnLogin_click' cannot handle event 'click' because they do not have a compatable signiture" - vb.net

I keep getting an error saying method 'btnLogin_click' cannot handle event Click because they do not have a compatible signature.
If anyone has some insight as to why its doing this, please enlighten me.
Public Sub btnLogin_Click(sender As Object, e As EventArgs, messageBox As MessageBox) Handles btnLogin.Click
Dim strUser As String = txtUser.Text
Dim strpass As String = txtPass.Text
If (strUser = "drake" And strpass = ("1")) Then
MessageBox.Show("This is where we take the user to the bard lounge.")
ElseIf Not (strUser = "drake" And strpass = ("1")) Then
txtPass.Text = ("") And MessageBox.Show("Incorrect username and/or password.")
End If
End Sub
This was supposed to be a simple login screen to open a windows form if you use the right username and password. I made this by following a tutorial for a login screen from youtube, and then optimizing the code as much as possible, with some help from my uncle who is a coding veteran of 20 years.
Once I got it cleaned up, I started working on my own features. I tried to make the txtPass text box clear itself if the pass/user were incorrect upon clicking the login button btnLogin. But as soon as I try to run the program it gives me an error that didn't show up in the development tab before. I already attempted to use Clear() rather than txtPass.text = "" however it only gave me more errors I couldn't understand.

It's not the Button that is the problem but your code. When an event is raised, the object raising the event calls the method registered as the event handler. You can't just put whatever parameters you want in that method. In your case, how is the Button supposed to pass an argument for that third parameter when the person who wrote the code for the Button class years ago had no idea that parameter would exist? You're not even using that parameter inside the method anyway, so what is it for? Just change the method declaration to what it should have been in the first place and it will work:
Public Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
That's what would have been generated in the first place if you had just double-clicked the Button in the designer. You can't arbitrarily add parameters to an event handler. The object raising the event expects a specific signature so your method has to have that signature.

Related

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.

More info on click event in VB

I would like to create a bot that repeats the same action many times. Is there any way I can get more info about a click event. Lets say I click somewhere on the opened file explorer window. Can the program tell that I clicked on a specific area or button in that window? Can I make the program click or type on a specific(opened) window?
Thanks
Yes the program can tell when you click on a specific button. Whatever button it is that you clicked on will have its click event fired, if it exists. In order to manually tell the program to perform this action, you can implement the phrase..
btn.PerformClick()
where "btn" is the name of whatever button you are needing to click. Whenever this phrase is called, the btn_Click event handler will be fired just the same as if you yourself actually clicked the button.
To do the same thing with an actual window or form in your program is trickier because there are no built in methods you can call to trigger a windows form click event. But it is possible. The code that will define a window's event handler and trigger it will be..
Private Sub myWindow_Click() Handles MyBase.Click
'code you want to run on click event here
End Sub
myWindow_Click() 'where you want the click to be triggered
The above would be much simpler to simulate in a method call however.
And lastly to simulate the typing of text into a window, you could simply access the text property of the control you are wanting to alter, and then change that text property to the desired text. But judging from your question, you are wanting this to be done in a similar fashion to how a human would.
This can also be accomplished with some work, using a timer interval that appends to the text of the control.
Lets say for example, you wanted "hello world" to be typed into a text box on screen as a person would. Add a timer control to your form and set its interval to 250 or however fast you want the word to be type and in a method..
Dim str as String = ""
Dim pos as Integer = 0
Public Sub humanType(word as String)
str = word
Timer1.enabled = true
End Sub
public Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
myTextBox.Text = myTextBox.Text + str[pos]
pos = pos + 1
If pos = str.length Then
Timer1.enabled = false
End If
End Sub
If you have never worked with timer intervals before, this tutorial has some good info on them for VB.Net. http://www.techrepublic.com/article/perform-actions-at-set-intervals-with-vbnets-timer-control/
Hope it helps!

Working with passed data between forms

Currently in my windows form, I have a few WinForms to work with. One WinForm acts as a main menu and is supposed to call another form as a secondary window on its own.
Private Sub btnMainGame_Click(sender As Object, e As EventArgs) Handles btnMainGame.Click
' This is the button to call up the main game controller. So simply hide this form aned then open the new form.
Dim frmController As New frmControllerScreen
frmController.Show()
Me.Hide() ' Happens on .Close as well
End Sub
The above code invokes another WinForm which is used to handle more options. When the user clicks on a particular button, a sub form is created again.
Dim OpenNewGameWindow As New frmGameConfig
OpenNewGameWindow.ShowDialog(Me)
Me.DialogResult = DialogResult.None ' Used to prevent the subform from closing the main form when it catches a dialog result.
Now in the frmGameConfig, the program is supposed to take data and pass it back to the form that called it.
Private Sub btnNewGameStartGame_Click(sender As Object, e As EventArgs) Handles btnNewGameStartGame.Click
' ... Skipped code...
frmControllerScreen.MasterQuestionList = QuestionList
frmControllerScreen.blnBankedTime = cbBankedTime.Checked
' ... Skipped code...
End Sub
However, when the frmController tries to reference MasterQuestionList... it returns a nullreference error as if it was not set.
Here's where things get funny...
When I made this code, frmControllerScreen was actually the startup form. Now when I change this form back to frmMainMenu, I get NullReference errors constantly.
My question: How am I supposed to pass information from one form to the next form if it was instantiated from a parent form. (Note I even moved the declartion to Public as a "module-wide" variable... and nothing happens but the same result.) The same error happens even if I go ahead and declare frmController.MasterQuestionList as well.
Instead of trying to pass data back from the called form to the caller, you can reference the called form's controls from the calling code after .ShowDialog.
Dim OpenNewGameWindow As New frmGameConfig
If OpenNewGameWindow.ShowDialog() Then
MasterQuestionList = OpenNewGameWindow.QuestionList
blnBankedTime = OpenNewGameWindow.cbBankedTime.Checked
End If
In OpenGameWindow button click:
Private Sub btnNewGameStartGame_Click(sender As Object, e As EventArgs) Handles btnNewGameStartGame.Click
Me.DialogResult = True
End Sub

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?

Manipulate text in a loaded html-page of the Webbrowser control in the DocumentCompleted event (vb.net)

I already tried some solutions provided here, but I cannot get it to work. On my winform I have a webbrowser control which should load a webpage (aspx). In case the webpage isn't found, I want to let the user know that this page isn't found. To get this to work I use the following code:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If (WebBrowser1.Document.Url.ToString().StartsWith("res:")) Then
Dim curDir As String = Directory.GetCurrentDirectory()
Dim Url As Uri = New Uri(String.Format("file:///{0}/Html/PageNotFound.html", curDir))
WebBrowser1.Navigate(Url)
End If
End Sub
This is working fine. The page PageNotFound.html is shown. However, I would like to provide the user with some additional information which I want to insert into the PageNotFound.html at realtime (i.e. using document.getElementById to manipulate a Label-tag). I just don't know how I can do this, or if it is even possible. Maybe I use the wrong event. something I also tried is:
With WebBrowser1
.Navigate("about:blank")
.Document.OpenNew(False)
.Document.Write(HtmlString)
.Refresh()
End With
Where the HtmlString contains a complete webform.(like: "")
Maybe someone put me the right direction? TIA