How do you run private subs from a module? - vb.net

I want to make a public sub in my module to to store a repeating procedure. Specifically key press events.
Private Sub txtPass_KeyPress(sender As Object, e As KeyPressEventArgs) _
Handles txtPass.KeyPress
FunctionKeys(Me, sender, e)
End Sub
Public Sub FunctionKeys(form as object, sender as object, _
e as KeyPressEventArgs)
With form
If e.KeyChar = ChrW(Keys.Enter) Then .btnOk_Click(sender, e)
If e.KeyChar = ChrW(Keys.Escape) Then .btnClose_Click(sender, e)
end with
End sub
I guess it would look something like this. Unfortunately, this tells me it can't find a public sub for formname.btnok_click etc. I want to know if there's a way around this. Looking around the net I found I can use the AcceptButton and CancelButton property. But only if i actually have a button to press. My MDI does not have buttons. Just menu. Also, I'm aware I can use formname.close() for the Keys.Escape. But I'd still have a problem with the OK button.

You can make btnOk_Click and btnClose_Click public on all your forms and that'll work.

Related

How to share events between forms

So i have a tray icon that should behave the same way between 3 forms. I then created this code:
Private Sub TrayForm_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
If e.Button = MouseButtons.Right Then
If Not Application.OpenForms().OfType(Of TrayForm).Any = 1 Then
TrayForm.ContextMenuStrip1.Show(Cursor.Position)
End If
End If
End Sub
Which is used to handle the tray icon. How can i do to share this event between the forms so i don't have to place this same code on every form?
How are event handlers working exactly? I looked online and on MSDN and it is not clear to me.
Thanks
Are you sure that you want to share the event, and not juste the code that will handle the event?
If you don't want to copy and paste your code, which you need to handle the events of more than one form, here's a way to do it:
Declare the sub which contains the code needed to handle the event as a public shared sub. Like this:
Public Shared Sub TrayForm_MouseClick(sender As Object, e As MouseEventArgs)
So, now you have a Sub which can handle the event you want to handle from all three forms.
Now, when you initialize those forms, add a line to make the shared Sub handle the event you want it to handle:
AddHandler NotifyIcon1.MouseClick, AddressOf ProjectName.FileName.TrayForm_MouseClick
ProjectName.FileName is meant here to be the path to refer to the shares Sub inside the file where you put it. I usually name it like ProjectNameUtils.vb or something like that.
If you just want to avoid copy and pasting your Sub so you don't have to modify it at several places every time you change something, this could be a way to achieve that.
As Stipulated by Hans Passant:
Sub Eclass_EventHandler(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
If e.Button = MouseButtons.Right Then
If Not Application.OpenForms().OfType(Of TrayForm).Any = 1 Then
Me.ContextMenuStrip1.Show(Cursor.Position)
End If
End If
End Sub
On the Trayform.VB just did the trick.
But about the shared event. i Have one that would have to be:
Private Sub FormClosingEVENT(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Not FromMenu Then e.Cancel = True
Me.WindowState = FormWindowState.Minimized
'Application.Exit()
End Sub
How should i handle this?

How do I prevent mouse wheel from incrementing numericupdown without over loading?

How do I prevent mouse wheel from incrementing numericupdown without over loading?
I had previously inherited numericupdwon to overload the MouseWheel event with an empty event. This worked for a while, but something happened when I switched to x64 that made the whole inherited class periodically show not found. Not sure because even if I switched back to x86 it was still a problem.
This worked for me..
Private Sub NumericUpDown1_MouseWheel(sender As Object, e As MouseEventArgs) Handles NumericUpDown1.MouseWheel
Dim MW As HandledMouseEventArgs = CType(e, HandledMouseEventArgs)
MW.Handled = True
End Sub
That HandledMouseEventArgs usage does look weird though.. but it works.
https://msdn.microsoft.com/en-us/library/system.windows.forms.handledmouseeventargs(v=vs.110).aspx
I came up with a different solution, using the following code to prevent the increment
Private Sub nup_cores_MouseWheel(sender As Object, e As MouseEventArgs) Handles nup_cores.MouseWheel
nup_cores.Increment = 0
End Sub
And then change it back. In the specific case of a numericupdown, the cursor blinking seems to trigger gotfocus. The same principle could be applied with a short duration timer
Private Sub nup_cores_GotFocus(sender As Object, e As EventArgs) Handles nup_cores.GotFocus
nup_cores.Increment = 1
End Sub
For C#:
For some reason the MouseWheel doesn't show up in the list of events from the GUI. So I had to programmatically add the event in my form load. In the MouseWheel event I do something similar as the above selected answer (but a little different).
private void Form1_Load(object sender, EventArgs e)
{
numericUpDownX.MouseWheel += new MouseEventHandler(handle_MouseWheel);
}
void handle_MouseWheel(object sender, MouseEventArgs e)
{
((HandledMouseEventArgs)e).Handled = true;
}

Running event x from event y

(Newbie VB.NET question)
Here's the specific code behind my simple winforms that I don't fully understand:
Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
'do something
End Sub
Private Sub MainForm_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Enter
okButton_Click(Me, e) '<=== argumanets must be wrong
End Sub
What I'm trying to achieve:
If the user hits Enter when they have the winforms active then I'd like the Click event handler of the okButton to fire.
Obviously from the above my understanding of the arguments I need to supply to the event called okButton_Click is lacking; what are the correct arguments and why?
I think you might use the AcceptButton property of the form. Just set it to the desired button and it should do the trick.
Note that there is also a CancelButton property.
Answering your event-question:
The sender argument marks the sender of the event. Mostly, this is the Me instance of the class. In my opinion, Me seems to be absolutely correct.
The e argument contains the EventArgs of the specific event. If you're not using this argument in your function body, the content of this variable doesn't matter. You could use Nothing or just route the EventArgs (that's what you've done).
Refering to your comment:
EventArgs is a base class for event-specific data. For example, if you're subscribing to a mouse event, ewill be a MouseEventArgs. The MouseEventArg class offers you the mouse buttons that have been pressed and the coordinates of the pointer when the event was fired.
In your case, the events only have EventArgs which provide only basic information about the event. There does not seem to be special information about it.
Note: If you want to combine multiple events into one callback, you can make e of type EventArgs because every e should inherit from EventArgs following the Microsoft guidelines. Therefore, you can combine a Button-Click with a Mouse-Move into one callback because the signature of the delegates match.
A nicer way than just passing Nothing to the target Sub, is to combine two callbacks into one. You can do this in VB.NET using multiple Handles like this:
Private Sub SomeSub(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click, MyBase.Enter
'this is getting called on a okButton.Click and MyBase.Enter
End Sub
(Scroll to the right to see the Handles)
Note that you don't need a second Sub which calls the first one. Everything is in one Sub.
Try this instead:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
If (keyData And Keys.KeyCode) = Keys.Enter Then
okButton.PerformClick()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
You might need to exclude some controls in the check if you want to use enter key with other controls, ie:
If (keyData And Keys.KeyCode) = Keys.Enter AndAlso Not Textbox1.Focused Then

Textbox click event in vb.net

I am trying to write an onclick event for textbox but VB.net does not seem to support textbox1.click()
I want to open a new form every time someone clicks on the textbox.
Opening a new form is no problem, but I cant detect the click.
Is there any event for textbox that detects click event?
I saw something like TextboxBase that has Click but I am able to use it well.
please help!
This is how my class looks :
Partial Public Class TextBoxClick
Inherits System.Web.UI.Page
End Class
It has some basic load and init events.
I am trying to write a Sub like this :
Private Sub incident_clicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles Incident.OnClick
Incident.Click does not work either.
I am guessing I need to import some class to access the Click event but I am not sure which.
Thanks is advance
TextBox has a Click event, using it is no problem. Your Handles clause however uses OnClick, that's not a valid event name. Do make sure this Sub is inside a Form class and not a module.
Public Class Form1
Private Sub TextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
MessageBox.Show("Click!")
End Sub
End Class
You could use onFocus event :)
According to MSDN, your code should work as follows:
Private Sub TextBox1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles TextBox1.Click
' Code to handle the event here
End Sub
However, you could also try the MouseUp event:
Private Sub textbox1_MouseUp(sender As Object, _
e As System.Windows.Forms.MouseEventArgs) _
Handles textbox1.MouseUp
' Code to handle the event here
End Sub
' Will fire if textbox gets focused
Private Sub incident_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles incident.GotFocus
Debug.Print("inciden got focus")
End Sub
' Will fire if textbox gets mouse clicked
Private Sub incident_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles incident.MouseClick
Debug.Print("inciden got clicked")
End Sub
For anyone who is having trouble with this, I fixed it by switching to an asp control. My button now looks like this:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
Not sure why, but I now have a working click event.
For me the Textbox_ click event is triggered only when I type a character in that box.
The textbox click event is triggered only when you type a character in that textbox.
That is disgusting. You may want to try mouse-enter mouse-leave events they are more reliable. Babu V Bassa.

Form Inheritance in Visual Studios designer implementations

I'm in the process of moving a project from Visual Studio 2003 to 2005 and have just seen the
The event Click is read-only and cannot be changed
when using inherited forms regardless of the Modifier on the Base Forms Controls will make all the Controls from the Base Readonly in the designer (Though in 2003 it didn't work this way).
I found this post mentioning that this functionality has been temporarily" disabled
http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/c25cec28-67a5-4e30-bb2d-9f8dbd41eb3a
Can anyone confirm whether this feature is used anymore? Or how to program around it to be able to use the Base Control Events and still have a designer?
This is one way I've found but quite painful when it used to do the plumbing for you. Even just hiding one of the controls you have manually do now.
Public Class BFormChild
Friend Overrides Sub cmdApply_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MyBase.cmdApply_Click(sender, e)
End Sub
Friend Overrides Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MyBase.cmdCancel_Click(sender, e)
End Sub
Friend Overrides Sub cmdOk_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MyBase.cmdOk_Click(sender, e)
End Sub
End Class
Base classes that generate events require the standard event generation pattern. That must be done in code, the designer cannot auto-generate it. It never will.
Public Class BFormBase
Public Event ApplyClicked As EventHandler
Protected Overridable Sub OnApplyClicked(ByVal e As EventArgs)
'--- Possible default implementation here
'...
RaiseEvent ApplyClicked(Me, e)
End Sub
Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click
OnApplyClicked(e)
End Sub