call double click on mouse single click - vb.net

how do we call double click on single mouse click event? Iam doing this and it's not working. Any ideas ?
Private Sub RichTextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick
RichTextBox1_MouseDoubleClick(sender, e)
End Sub

In properties windows click Event Icon and find double click and in selected event name select "RichTextBox1_MouseClick"

Have you tried:
Private Sub RichTextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick, RichTextBox1.MouseDoubleClick, RichTextBox1.DoubleClick
'Do stuff here
End Sub

There are basically 2 options. The first is how your doing it, which the click can still do more as well as the double click.
Private Sub RichTextBox1_MouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick
'' i can do stuff here
RichTextBox1_MouseDoubleClick(sender, e)
'' and here
'' as well as do whatever double click is doing
End Sub
Private Sub RichTextBox1_MouseDoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDoubleClick
'' to do something here
End Sub
The other option, if they are always going to do the same, you could do this:
Private Sub RichTextBox1_Single_DoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick, RichTextBox1.MouseDoubleClick
'' do something here
End Sub
Notice the second Handles bit on the end? that function can handle both now :)
Either should work fine for what you want.

Related

Specific keyboard characters to call specific button actions

When I start my program, and push the specified keys to call my button commands, it does not do anything.
The focus remains on one of the buttons and nothing occurs.
I've tried multiple codes from using KeyDown to KeyPress to codes that include vbKey.
I am very new to vb, so it is very likely that I just do understand what I am doing. :(
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = "/" Then
Call Button1_Click(sender, e)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Close Program
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPMN.Click
'Add +1 to PMN Textbox (txtPMN)
txtPMN.Text = (Val(txtPMN.Text) + 1).ToString()
End Sub
End Class
I would like to simulate the clicking of certain buttons (activate the button code) when I press specific keys on the keyboard. For example: If I press "/" I would like Button1_Click to activate as if I clicked the button with the mouse. Then, if I push my next key ".", I would like Button2_Click to activate.
I do not want to use modifiers like: SHIFT, CTRL, ALT
Please try with keychar like this:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = "/" Then
Call Button1_Click(sender, New EventArgs)
End If
end sub
when use keydown, the keycode for "/" key is OEM, depend the keyboard type, so char(e.keycode) may be not "/"

Visual Basic 2008 Check if label is inside a panel

So i've been trying to make a game with arrow keys.
The label is the character, if the label is inside the object, it will do something...
But i got a very hard problem i cannot find on the internet.
How do i check if a label is inside of an object? Example: Picture box, and Panel.
I've tried this one.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Location.X = Panel1.Location.X And Label1.Location.Y = Panel1.Location.Y Then
Me.Close() 'Any code.
End If
End Sub
Doesn't work,
any help would be appreciated.
I am a beginner by the way, i only make simple applications. Like escape the room, maze... etc.
I would use the label's parent property to see if it is inside the panel.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Parent IsNot Nothing AndAlso Label1.Parent Is Panel1 Then
Me.Close() 'Any code.
End If
End Sub
If you are looking to see if the Label is over the Panel I would try something like this. The ClientRectangle property is the rectangle the control takes up. I am assuming the panel is bigger than the Label.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Panel1.ClientRectangle.Contains(Label1.ClientRectangle) Then
Me.Close() 'Any code.
End If
End Sub
If it is smaller you can check if a point is in the Panel's rectangle. For example top left corner
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Panel1.ClientRectangle.Contains(new Point(Label1.Left, Label1.Top)) Then
Me.Close() 'Any code.
End If
End Sub

DragLeave doesn't get called

in my VB.Net application I am try to drag and drop a custom control. It should be dropable onto another instance of the same class. So here is the code I use:
Private Sub HandleMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
Me.DoDragDrop(Me, DragDropEffects.Copy)
End Sub
Private Sub HandleDragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragEnter
e.Effect = If(checkDropData(e), DragDropEffects.Copy, DragDropEffects.None)
End Sub
Private Sub HandleDragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragDrop
addControl(e.Data.GetData(GetType(MyControl)))
End Sub
Private Sub HandleDragLeave(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragLeave
Console.WriteLine("HandleDragLeave: " & sender.ToString)
End Sub
I am able to drop the control but while dragging the DragLeave event never gets called. Did I miss something?
That's very bad... I used the wrong signature but the was nothing which told me I was wrong.
I found the solution here. I used DragEventArgs instead of EventArgs as type of e. After changing the type it works like expected.

Click a button programmatically

I want to code a button that programmatically clicks the other button when I click it.
For example, I have two buttons named Button1 and Button2, what I wanted to do is that immediately after I click Button1, it should click Button2. Is this possible?
Best implementation depends of what you are attempting to do exactly. Nadeem_MK gives you a valid one. Know you can also:
raise the Button2_Click event using PerformClick() method:
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
'do stuff
Me.Button2.PerformClick()
End Sub
attach the same handler to many buttons:
Private Sub Button1_Click(sender As Object, e As System.EventArgs) _
Handles Button1.Click, Button2.Click
'do stuff
End Sub
call the Button2_Click method using the same arguments than Button1_Click(...) method (IF you need to know which is the sender, for example) :
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
'do stuff
Button2_Click(sender, e)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Button2_Click(Sender, e)
End Sub
This Code call button click event programmatically
The best practice for this sort of situation is to create a method that hold all the logics, and call the method in both events, rather than calling an event from another event;
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
LogicMethod()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
LogicMethod()
End Sub
Private Sub LogicMethod()
// All your logic goes here
End Sub
In case you need the properties of the EventArgs (e), you can easily pass it through parameters in your method, that will avoid errors if ever the sender is of different types. But that won't be a problem in your case, as both senders are of type Button.
Let say button 1 has an event called
Button1_Click(Sender, eventarg)
If you want to call it in Button2 then call this function directly.
Button1_Click(Nothing, Nothing)
in c# this is working :D
protect void button1_Click(object sender, EventArgs e){
button2_Click(button2, null);
}
protect void button2_Click(object sender, EventeArgs e){
//some codes here
}
for vb.net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Button2_Click(Sender, e)
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
//some codes here
End Sub

VB.NET SetFocus Equivalent for Enter Button?

I have a textbox called clientFirst and, if I press Enter after I'm done typing in this textbox, I want the Enter button to represent a click on btnAdd. How can I make this happen?
Protected Sub btnAdd_Click(sender As Object, e As ImageClickEventArgs) Handles btnAdd.Click
'Do something
End Sub
There's not much code to post, really. :( But I was suggested to use the KeyDown function by Chase Ernst, which seems like a brilliant idea, except that it keeps telling me that a part of it is undefined. Here's the code I was suggested:
Private Sub clientFirst_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles clientFirst.KeyDown
If e.KeyCode = Keys.Enter Then
btnAdd.PerformClick()
End If
End Sub
It keeps telling me that System.Windows.Forms.KeyEventArgs is undefined. I'm guessing there's an Imports class I have to define at the top? Tried System.Windows, System.Object, System.Windows.Forms, System.KeyEventArgs and nothing works!
Thank you in advance! x
Easy way is to use the HTMLForm.DefaltButton property on the form.
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Page.Form.DefaultButton = "TextBox1"
End Sub
I would use something like this:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
Button1.PerformClick()
End If
End Sub
If the key that is pressed is the enter key, then Button1.PerformClick() will fire.