VB (simulate) mouse click (out of form) - vb.net

I have tried to make a programm that simulates mouse clicks and react on mouse click events in Visual Basic but I can't find what i need. I have written the code what i dont know logicly and marked it pls tell me the command of code what i need. Every where where is a # i don't know the code. Sorry for my brilliant englisch ;D my teacher is bad :P
Public Class Form1
Public Tick As Integer = 0
Public MaxTick As Integer = 0
Public active As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RadioButton1.Select()
ComboBox1.SelectedIndex = 0
ComboBox2.SelectedIndex = 0
End Sub
Private Sub RadioButton1_Change(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.Click, RadioButton2.Click
If RadioButton1.Checked Then
GroupBox1.Enabled = True
GroupBox2.Enabled = False
Timer1.Interval = TextBox1.Text
Else
GroupBox2.Enabled = True
GroupBox1.Enabled = False
Timer2.Interval = TextBox3.Text
End If
End Sub
Private Sub Me_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Tab) Then
If RadioButton1.Checked Then
active = False
If Timer1.Enabled Then
Timer1.Stop()
Else
Timer1.Start()
End If
e.Handled = False
Else
active = True
End If
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If CheckBoxLeft.Checked Then
###Perform Mouseclick Left###
End If
If CheckBoxMiddle.Checked Then
###Perform Mouseclick Middle###
End If
If CheckBoxRight.Checked Then
###Perform Mouseclick Right###
End If
End Sub
###Private Sub OnClick_Event()###
###If Mouseclick == Right Then###
If CheckBoxRight.Checked Then
Timer2.start()
End If
###Else if Mouseclick == Middle Then###
If CheckBoxMiddle.Checked Then
Timer2.Start()
End If
###Else If Mouseclick == Left Then###
If CheckBoxLeft.Checked Then
Timer2.Start()
End If
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
If active Then
Tick += 1
If CheckBoxLeft.Checked Then
###Perform Click Left###
End If
If CheckBoxMiddle.Checked Then
###Perform Click Middle###
End If
If CheckBoxRight.Checked Then
###Perform Click Right###
End If
If Tick = MaxTick Then
Tick = 0
Timer2.Stop()
End If
Else
Timer2.Stop()
Tick = 0
End If
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
MaxTick = TextBox2.Text
End Sub
End Class
So i need a Mouseclick Event and then ask which Mousebutton was clicked and i need a method to perform left middle and right click
LG Niklas

You should look up the mouse_event() p/invoke method. This will allow you to simulate mouse events. It's declaration in VB.Net is
<DllImport("user32.dll")> _
Private Shared Sub mouse_event(dwFlags As UInteger, dx As UInteger, dy As UInteger, dwData As UInteger, dwExtraInfo As Integer)
End Sub
You can read more about it here. And here is a project on codeproject.com that has a complete example.

Related

combobox multiple thread error

I have a problem with my code. I keep getting Multiple thread Error with backgroundworker, because of the combobox item display. Please look at my code below its a very simple code which I am planning to use on big scale, all I want it to do is "If item "1" selected show item "1" in label1. I can only assume that problem exists because Combobox runs in different thread....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.runworkerasync()
BackgroundWorker1.WorkerReportsProgress = True
Me.Cursor = Cursors.WaitCursor 'Cursor changes to wait
End Sub
Public Structure controlwithtext
Public controlname As Control
Public text As String
Public Sub New(ByVal ctrl As Control, ByVal text As String)
Me.controlname = ctrl
Me.text = text
End Sub
End Structure
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If comboBox1.SelectedItem = "1" then
BackgroundWorker1.ReportProgress(5, New controlwithtext(Label1, ComboBox1.SelectedItem))
End If
End Sub
Private Sub SetBackgroundWorker_ProgressChanged(ByVal sender As Object,
ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If TypeOf e.UserState Is controlwithtext Then
Dim cwt As controlwithtext = CType(e.UserState, controlwithtext)
cwt.controlname.Text = cwt.text
End If
End Sub
Here's an example of how to read from and write to controls from the BackgroundWorker thread:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While True
System.Threading.Thread.Sleep(250)
Dim selection As String = Me.Invoke(Function()
If Not IsNothing(ComboBox1.SelectedItem) Then
Return ComboBox1.SelectedItem.ToString
Else
Return String.Empty
End If
End Function).ToString
If selection = "1" Then
Me.Invoke(Sub()
Label1.Text = ComboBox1.SelectedItem.ToString
End Sub)
Else
Me.Invoke(Sub()
Label1.Text = "something else"
End Sub)
End If
End While
End Sub

Timer acting weird

Hi guys I have another problem which is all about the timer..the form that I would want to show for 3 seconds is only showing for about half a second. Please help me thanks in advance
Main form code:
Private Sub submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit.Click
'question 1
If Label1.Text = "Who invented the airplane?" And TextBox1.Text = "third" Then
Label2.Text = (Label2.Text) + 1
correctmsg.Show()
correctmsg.Hide()
Label1.Text = "Who invented the telephone?"
Return 'Don't do any more checks this time around
ElseIf Label1.Text = "Who invented the airplane?" Then
'Reason ElseIf (In case the question was 'who invented the telephone' then the first errormessage should not not be shown)
wrongmsg.Show()
Return
End If
Splash form code:
Public Class correctmsg
Private Sub correctmsg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim mysplash As New correctmsg
mysplash.Timer1.Interval = 3000
End Sub
End Class
Something like this:
Public Class correctmsg
' correctmsg == mysplash ??? so this is a form??
Private Sub correctmsg_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 3000 ' could be a timer here
End Sub
Public Sub ShowMsg
Timer1.Enabled = true
me.Visible = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Me.Visible = False
End Sub
End Class
I am just showing the form by making it visible. No need to make a new form. When the timer expires, hide the form and disable the timer. To use it:
correctmsg.ShowMsg
' this was hiding the form as soon as it shows:
'correctmsg.Hide()

Close form after set time

The code below allows me to fade in and out when it opens and closes, which is what I want. However, I would like my form to remain open for 10 seconds before the fading starts. I am struggling to get that part done.
Here is what I have so far:
Public Class frmDefinitions
Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
tmr_out.Enabled = True
End Sub
Private Sub frmDefinitions_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
Me.Opacity = 100
tmr_in.Enabled = True
End Sub
Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tmr_in.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
tmr_in.Enabled = False
End If
End Sub
Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tmr_out.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
tmr_out.Enabled = False
Me.Close()
End If
End Sub
End Class
You will need to setup a third Timer to delay the start of your tmr_out Timer. I would trigger the delay as soon as your tmr_in is disabled. You should then get your 10 second delay before you start your fade out. You could also try to use the Form's Shown event to start the Delay but you would need to adjust the 10 seconds to accommodate the fade in delay.
Public Class Form1
Dim tmrDelay As New Timer()
Public Sub New()
InitializeComponent()
tmrDelay.Interval = 10000
AddHandler tmrDelay.Tick, AddressOf tmrDelay_Tick
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.Opacity = 0
tmr_in.Enabled = True
End Sub
Private Sub tmrDelay_Tick(sender As System.Object, e As System.EventArgs)
tmrDelay.Stop()
tmr_out.Start()
End Sub
Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_in.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
tmr_in.Enabled = False
tmrDelay.Start() 'Start Your 10 second delay here.
End If
End Sub
Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_out.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
tmr_out.Enabled = False
Me.Close()
End If
End Sub
End Class

VB Detect Idle time

I'm looking for a way to detect if the user has been idle for 5 min then do something, and if and when he comes back that thing will stop, for example a timer.
This is what i have tried (but this will only detect if form1 has been inactive / not clicked or anything):
Public Class Form1
Private Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'You should have already set the interval in the designer...
Timer1.Start()
End Sub
Private Sub form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MsgBox("Been idle for to long") 'I just have the program exiting, though you could have it do whatever you want.
End Sub
End Class
This is done easiest by implementing the IMessageFilter interface in your main form. It lets you sniff at input messages before they are dispatched. Restart a timer when you see the user operating the mouse or keyboard.
Drop a timer on the main form and set the Interval property to the timeout. Start with 2000 so you can see it work. Then make the code in your main form look like this:
Public Class Form1
Implements IMessageFilter
Public Sub New()
InitializeComponent()
Application.AddMessageFilter(Me)
Timer1.Enabled = True
End Sub
Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
'' Retrigger timer on keyboard and mouse messages
If (m.Msg >= &H100 And m.Msg <= &H109) Or (m.Msg >= &H200 And m.Msg <= &H20E) Then
Timer1.Stop()
Timer1.Start()
End If
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
MessageBox.Show("Time is up!")
End Sub
End Class
You may have to add code that disables the timer temporarily if you display any modal dialogs that are not implemented in .NET code.
This might work by setting it to just call the Reset idk i just want it work all over the program idk how to do it, i just created this code :
Public Class test
Dim IdleTimer As String
Dim testsave As String
Dim idle_TimerSet As String = 60 '<---- Here You choose The timer (1 per Sec)
Private Sub test_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
IdleTimer = idle_TimerSet
End Sub
Private Sub Idle_Tick(sender As Object, e As EventArgs) Handles Idle.Tick
If IdleTimer <= 1 Then
MsgBox("[ Idle Screen ]")
Else
IdleTimer = IdleTimer - 1 '<--- The Counter
IdleTracker.Text = IdleTimer
End If
End Sub
Private Sub test_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
Call Reset_Idle() 'This is on the main Form
End Sub
Private Sub test_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Call Reset_Idle() 'This is on the main Form
End Sub
Public Sub Reset_Idle() '<-- The Reset Action
'Idle.Enabled = False
IdleTimer = idle_TimerSet
'Idle.Enabled = True
End Sub
End Class

Need Basic Visual Studio 2010 Help

So I have a code atm that has 2 combo boxes, one to select the make, then it will enable the model, I have 3 model choices, here's code:
Public Class Form2
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
Private Sub cmb_make_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmb_make.Items.Add("Toyota")
cmb_make.Items.Add("Nissan")
cmb_make.Items.Add("Hyundai")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_finish.Click
If cmb_make.Text = "" Then
MsgBox("Please select your make")
End If
If cmb_model.Text = "" Then
MsgBox("Please select your model")
End If
If cmb_model.Text = ("Supra") Then
Form3.Show()
Me.Close()
End If
If cmb_model.Text = ("MR2") Then
Form4.Show()
Me.Close()
End If
If cmb_model.Text = ("Hilux") Then
Form5.Show()
Me.Close()
End If
If cmb_model.Text = ("R34") Then
Form6.Show()
Me.Close()
End If
If cmb_model.Text = ("R33") Then
Form7.Show()
Me.Close()
End If
If cmb_model.Text = ("R32") Then
Form8.Show()
Me.Close()
End If
If cmb_model.Text = ("Genesis Coupe") Then
Form9.Show()
Me.Close()
End If
If cmb_model.Text = ("RD1 Coupe") Then
Form10.Show()
Me.Close()
End If
If cmb_model.Text = ("Excel") Then
Form11.Show()
Me.Close()
End If
End Sub
Private Sub EditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem.Click
End Sub
Private Sub cmb_make_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_make.SelectedIndexChanged
Dim Strchoice As String
Strchoice = cmb_make.SelectedItem
If Strchoice = "Toyota" Then
End If
cmb_model.Items.Clear()
cmb_model.Items.Add("Supra")
cmb_model.Items.Add("MR2")
cmb_model.Items.Add("Hilux")
If Strchoice = "Nissan" Then
cmb_model.Items.Clear()
cmb_model.Items.Add("R34")
cmb_model.Items.Add("R32")
cmb_model.Items.Add("R33")
ElseIf Strchoice = "Hyundai" Then
cmb_model.Items.Clear()
cmb_model.Items.Add("RD1 Coupe")
cmb_model.Items.Add("Genesis Coupe")
cmb_model.Items.Add("Excel")
Once I have selected my model of the car, it takes me to that specific form with a picture of that model, I then want to display individual parts, with individual prices that will add up in a text box above, can someone help me and tell me how? Please this is due tomorrow I'm freaking out!!
You can put the parts into picture boxes on the model's form and have either check boxes or radio buttons (depending on what functionality you are required to do) underneath that have the prices as labels and then use an if structure to assign a value to them that can be totaled in the text box.
Dim total as double
If chk_part1.checked = true then
total += partPrice
If chk_part2.checked = true then
total += partPrice
txtTotal.text = total
Not saying that this code is perfect but you could implement something along those lines to get the results you're looking for.
You can also do a For Each Statement. Such as:
Dim Form As New Form3.ControlCollection(Me)
For Each CheckBox As CheckBox In Form
If CheckBox.Checked = True Then
total += partPrice
End If
Next
So, In essense, it should cycle through all of the check boxes and, if they are checked, it adds the part price to the total.