Change cursor VB.NET - vb.net

I can't change the cursor when it's a ToolStripButton.click event.
I got 2 buttons that call "Rechercher".
EDITED : Only the Button is working. It seems that the ToolStripButton cancel my cursor...
Thx for the help
Public Class FenetrePrincipale
Private WithEvents _btnRechercher As New Windows.Forms.ToolStripButton("Rechercher")
Private WithEvents btnRechercherAccesBtn As New Button
Private Sub Rechercher(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _btnRechercher.Click, btnRechercherAccesBtn.Click
Try
Me.Cursor = Cursors.WaitCursor
'WAITING FOR THE CODE TO FINISH (2 sec)
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
End Class

Maybe you should try something sampler like:
Private Sub MainFrame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Search()
End Sub
Private Sub Search()
Try
Me.Cursor = Cursors.WaitCursor
UseWaitCursor = True
Application.DoEvents()
Threading.Thread.Sleep(1000) 'WAITING FOR THE CODE TO FINISH
Finally
UseWaitCursor = False
Me.Cursor = Cursors.Default
Application.DoEvents()
End Try
End Sub
The problem is you don't have any pause where code should execute so it is doing to fast.

This is the only way I got this to work.
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Public Class FenetrePrincipale
Private WithEvents _btnRechercher As New Windows.Forms.ToolStripButton("Rechercher")
Private WithEvents btnRechercherAccesBtn As New Button
Private Sub Rechercher(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechercherAccesBtn.Click
Try
Me.Cursor = Cursors.WaitCursor
'code...
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub RechercherToolStripButton(ByVal sender As Object, ByVal e As System.EventArgs) Handles _btnRechercher.Click
Me.UseWaitCursor = True
SendMessage(Me.Handle, &H20, Me.Handle, New IntPtr(1))
Rechercher(Nothing, Nothing)
Me.UseWaitCursor = False
End Sub
End Class

Related

How to save my wlan settings?

I'm currently using Visual Studio 2012. My form code is given below with design screenshots. I am trying to make a virtual hotspot for my PC but I want to add one feature which takes previous SSID and password. My hotspot opens every time with blank text.
Here's my form design:
Form1 Design:
[
Form1 code:
Imports System.Runtime.InteropServices
Imports System.Collections.ObjectModel
Imports System.Text
Imports NativeWifi
Public Class Form1
Public Const WM_NCLBUTTONDOWN As Integer = &HA1
Public Const HT_CAPTION As Integer = &H2`
<DllImportAttribute("user32.dll")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, _
ByVal Msg As Integer, ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
End Function
<DllImportAttribute("user32.dll")> _
Public Shared Function ReleaseCapture() As Boolean
End Function`
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim wlan = New WlanClient()
Dim connectedSsids As Collection(Of [String]) = New Collection(Of String)()
For Each wlanInterface As WlanClient.WlanInterface In wlan.Interfaces
Dim ssid As Wlan.Dot11Ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid
connectedSsids.Add(New [String](Encoding.ASCII.GetChars(ssid.SSID, 0, CInt(ssid.SSIDLength))))
For Each item As String In connectedSsids
Label1.Text = item
Next
Next
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
ReleaseCapture()
SendMessage(Handle, WM_NCLBUTTONDOWN, _
HT_CAPTION, 0)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If Form2.TextBox1.Text = "" Then
MsgBox("Hotspot name cant be empty", MsgBoxStyle.Critical)
End If
If Form2.TextBox2.TextLength < 8 Then
MsgBox("Password should be 8+ character", MsgBoxStyle.Critical)
If Form2.TextBox2.Text = "" Then
MsgBox("Password cant be empty", MsgBoxStyle.Critical)
End If
Else
Dim process As New Process()
process.StartInfo.Verb = "runas"
process.StartInfo.UseShellExecute = True
process.Start("cmd", String.Format("/c {0} & {1} & {2}", "netsh wlan set hostednetwork mode=allow ssid=" & Form2.TextBox1.Text & " key=" & Form2.TextBox2.Text, "netsh wlan start hostednetwork", "pause"))
Form2.CheckBox1.Enabled = False
Form2.TextBox1.Enabled = False
Form2.TextBox2.Enabled = False
MsgBox("Hotspot started Successfully", MsgBoxStyle.Information)
End If
Catch
MsgBox("Failed to Establish a hotspot", MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Process.Start("CMD", "/C netsh wlan stop hostednetwork")
Form2.CheckBox1.Enabled = True
Form2.TextBox1.Enabled = True
Form2.TextBox2.Enabled = True
MsgBox("Hotspot stopped Successfully", MsgBoxStyle.Information)
End Sub
Private Sub Clsbtn_Click(sender As Object, e As EventArgs) Handles Clsbtn.Click
Me.Close()
End Sub
Private Sub Minbtn_Click(sender As Object, e As EventArgs) Handles Minbtn.Click
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
End Sub
Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
Me.Hide()
Form2.Show()
End Sub
End Class
Form2 Design:
[

VB.Net Hidden Program Hotkeys

Is there a way to register hotkeys to toggle a form from an invisible(hidden) program? I’ve tired normal methods and they only work when the form is either visible, or the active window. Thank you in advance for any help!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.KeyPreview = True
Me.ShowInTaskbar = False
Me.ShowIcon = False
Me.Visible = False
End Sub
This is the code that hides the program.
You want to use Global Hotkeys. Just make sure you unregister when the program closes.
From a MSDN article that helped me in the past:
Firstly, you need to know the Virtual-Key Codes.
http://msdn2.microsoft.com/en-us/library/ms927178.aspx You can then
P/Invoke RegisterHotKey/UnregisterHotKey APIs to register/Unregister
the hotkey. Code sample: Register multiple hotkeys such as Alt+D,
Alt+C, etc.
Imports System.Runtime.InteropServices
Public Class Form1
Public Const MOD_ALT As Integer = &H1 'Alt key
Public Const WM_HOTKEY As Integer = &H312
<DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
ByVal id As Integer, ByVal fsModifiers As Integer, _
ByVal vk As Integer) As Integer
End Function
<DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _
ByVal id As Integer) As Integer
End Function
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
RegisterHotKey(Me.Handle, 100, MOD_ALT, Keys.D)
RegisterHotKey(Me.Handle, 200, MOD_ALT, Keys.C)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Dim id As IntPtr = m.WParam
Select Case (id.ToString)
Case "100"
MessageBox.Show("You pressed ALT+D key combination")
Case "200"
MessageBox.Show("You pressed ALT+C key combination")
End Select
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
UnregisterHotKey(Me.Handle, 100)
UnregisterHotKey(Me.Handle, 200)
End Sub
End Class

Disable/lock Keyboard Permanently

I new to vb.net. I try to create system to disable/lock keyboard.
And also i try many code from google but its all work only in that form. I want It disable/lock permanently.
Public Class Form1
Private Declare Function BlockInput Lib "user32" Alias "BlockInput" (ByVal fBlock As Integer) As Integer
Private Declare Function ShowCursor Lib "user32" (ByVal lShow As Long) As Long
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BlockInput(1)
ShowCursor(0)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BlockInput(0)
ShowCursor(1)
End Sub
End Class
Thanks.
Try this but this will block the mouse and the keyboard so i would put an timer to re-enable it
Public Class Form1
Private Declare Function BlockInput Lib "user32" (ByVal fBlock As Long) As Long
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Interval = 5000 '5 seconds
Timer1.Enabled = True
BlockInput(True)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
BlockInput(False)
MsgBox("You can now use your mouse and keyboard")
End Sub
End Class

VB (simulate) mouse click (out of form)

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.

update form on asynchronous control event

A few weeks ago I wrote a wrapper for the ServiceController control to enhance and streamline the base ServiceController. One of the changes I made was to add a monitoring component using the System.Threading.Timer object. On any change of status, an event is raised to the parent class. The actual monitoring works fine, but when the event is handled in the main form, my program abruptly ends - no exceptions, no warning, it just quits. Here's a skeleton version of the control:
Public Class EnhancedServiceController
Inherits ServiceController
Public Event Stopped(ByVal sender As Object, ByVal e As System.EventArgs)
Public Event Started(ByVal sender As Object, ByVal e As System.EventArgs)
Private _LastStatus As System.ServiceProcess.ServiceControllerStatus
Private serviceCheckTimer as System.Threading.Timer
Private serviceCheckTimerDelegate as System.Threading.TimerCallback
...
Private Sub StartMonitor()
MyBase.Refresh()
_LastStatus = MyBase.Status
serviceCheckTimerDelegate = New System.Threading.TimerCallback(AddressOf CheckStatus)
serviceCheckTimer = New System.Threading.Timer(serviceCheckTimerDelegate, Nothing, 0, 60*1000)
End Sub
Private Sub CheckStatus()
MyBase.Refresh()
Dim s As Integer = MyBase.Status
Select Case s
Case ServiceControllerStatus.Stopped
If Not s = _LastStatus Then
RaiseEvent Stopped(Me, New System.EventArgs)
End If
Case ServiceControllerStatus.Running
If Not s = _LastStatus Then
RaiseEvent Started(Me, New System.EventArgs)
End If
End Select
_LastStatus = s
End Sub
End Class
And the form:
Public Class Form1
Private Sub ServiceStarted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ESC.Started
Me.TextBox1.Text = "STARTED"
End Sub
Private Sub ServiceStopped(ByVal sender As Object, ByVal e As System.EventArgs) Handles ESC.Stopped
Me.TextBox1.Text = "STOPPED"
End Sub
End Class
If I had to guess, I'd say that there's some sort of thread problem, but I'm not sure how to handle that in the form. Any ideas?
IF it is a threading issue then you are probably trying to update the UI from a non-UI thread.
So something like this should solve that...
Private Delegate Sub UpdateTextBoxDelegate(byval tText as String)
Private Sub UpdateTextBox(byval tText As String)
If Me.InvokeRequired Then
Me.Invoke(New UpdateTextBoxDelegate(AddressOf UpdateTextBox), tText)
Exit Sub
End If
TextBox1.Text = tText
End Sub
Private Sub ServiceStarted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ESC.Started
UpdateTextBox ("STARTED")
End Sub
Private Sub ServiceStopped(ByVal sender As Object, ByVal e As System.EventArgs) Handles ESC.Stopped
UpdateTextBox("STOPPED")
End Sub