|
-
Nov 4th, 2008, 05:16 PM
#1
Thread Starter
Addicted Member
[RESOLVED]How can i do this? (something.string)
I want to be able to change the thing after the "." An example to show you what i mean:
Dim TheString As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TheString = Text
Label1. TheString = Test
End Sub
Will show up as:
Dim TheString As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TheString = Text
Label1.Text = Test
End Sub
Last edited by n00b scripter; Nov 4th, 2008 at 06:02 PM.
-
Nov 4th, 2008, 05:18 PM
#2
Re: How can i do this? (something.string)
The only way to do that is through reflection, hold in ill throw together an example.
Edit: No wait I posted something like this not a long time ago...here's the thread. The code is in C# but the VB.NET equivallent would be:
VB.NET Code:
Private Sub SetProperty(ByVal ctrl As Control, ByVal propName As String, ByVal propValue As Object)
Dim propInfo As Sytem.Reflection.PropertyInfo = ctrl.GetType().GetProperty(propName)
If Not propInfo Is Nothing Then
propInfo.SetValue(ctrl, propValue, null)
End If
End Sub
Though often when you think you need to use reflection, all you need is to rethink your logic
Last edited by Atheist; Nov 4th, 2008 at 05:21 PM.
-
Nov 4th, 2008, 05:19 PM
#3
Re: How can i do this? (something.string)
what are you really trying to do?
-
Nov 4th, 2008, 05:25 PM
#4
Thread Starter
Addicted Member
Re: How can i do this? (something.string)
 Originally Posted by dbasnett
what are you really trying to do?
What I'm really doing is working with a keyboard hook. So what i want is to change key wich works like this:
e.KeyCode = Keys.TheKey
So for and example:
e.KeyCode = Keys.F1
So I want a string which I can change So that if the string's F1 then it will be like:
e.KeyCode = Keys.F1
And if it's A then it would be:
e.KeyCode = Keys.A
And so on
-
Nov 4th, 2008, 05:30 PM
#5
Re: How can i do this? (something.string)
i think e.keycode is read only.
-
Nov 4th, 2008, 05:30 PM
#6
Re: How can i do this? (something.string)
Well thats different and wont work with my code, as you're trying to access members of an enumeration type, but in your original post you where refering to accessing properties.
Why not just keep a variable for the key like so:
According to all the information you've given us sofar, this is what youre after...being able to dynamically set what key you should be comparing against.
-
Nov 4th, 2008, 05:31 PM
#7
Re: How can i do this? (something.string)
why don't you press the entire event you are working on.
-
Nov 4th, 2008, 05:34 PM
#8
Thread Starter
Addicted Member
Re: How can i do this? (something.string)
 Originally Posted by Atheist
Well thats different and wont work with my code, as you're trying to access members of an enumeration type, but in your original post you where refering to accessing properties.
Why not just keep a variable for the key like so:
According to all the information you've given us sofar, this is what youre after...being able to dynamically set what key you should be comparing against.
It just gives me the error:
Code:
Error 5 'myKey' is not a member of 'System.Windows.Forms.Keys'.
-
Nov 4th, 2008, 05:37 PM
#9
Re: How can i do this? (something.string)
No my code isnt giving you any errors. Your code is giving you errors and you should always post them when they show up. Fortunately this error tells us enough to understand that you're trying to do this:
Which isnt going to work. Post your entire subroutine.
-
Nov 4th, 2008, 05:41 PM
#10
Thread Starter
Addicted Member
Re: How can i do this? (something.string)
 Originally Posted by Atheist
No my code isnt giving you any errors. Your code is giving you errors and you should always post them when they show up. Fortunately this error tells us enough to understand that you're trying to do this:
Which isnt going to work. Post your entire subroutine.
The code isn't gonna help you much...
I simply wanna know how i can set the code after the . using a string. Or if there's some other way.
But well here's the code:
Class:
PHP Code:
Imports System.Runtime.InteropServices Imports System.Reflection Imports System.Threading Imports System.Windows.Forms
Public Class KeyboardLowLevelHook Implements IDisposable 'Works only on Windows NT, version 4.0 SP3 and later
Private Declare Function UnhookWindowsHookEx Lib "user32" _ (ByVal hHook As Integer) As Integer
Private Declare Function SetWindowsHookEx Lib "user32" _ Alias "SetWindowsHookExA" (ByVal idHook As Integer, _ ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _ ByVal dwThreadId As Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" _ (ByVal vKey As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" _ (ByVal hHook As Integer, _ ByVal nCode As Integer, _ ByVal wParam As Integer, _ ByVal lParam As KBDLLHOOKSTRUCT) As Integer
Private Structure KBDLLHOOKSTRUCT Public vkCode As Integer Public scanCode As Integer Public flags As Integer Public time As Integer Public dwExtraInfo As Integer End Structure
' Low-Level Keyboard Constants Private Const HC_ACTION As Integer = 0 Private Const LLKHF_EXTENDED As Integer = &H1 Private Const LLKHF_INJECTED As Integer = &H10 Private Const LLKHF_ALTDOWN As Integer = &H20 Private Const LLKHF_UP As Integer = &H80
'KeyUp/KeyDown constants Private Const WM_KEYDOWN = &H100 Private Const WM_KEYUP = &H101 Private Const WM_SYSKEYDOWN = &H104 Private Const WM_SYSKEYUP = &H105
Private Const WH_KEYBOARD_LL As Integer = 13& Private KeyboardHandle As Integer Private disposed As Boolean = False
Private Delegate Function KeyboardHookDelegate( _ ByVal Code As Integer, _ ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _ As Integer
'Our keydown and keyup events Public Event KeyDown As KeyEventHandler Public Event KeyUp As KeyEventHandler
<MarshalAs(UnmanagedType.FunctionPtr)> _ Private callback As KeyboardHookDelegate = New KeyboardHookDelegate(AddressOf KeyboardCallback)
'Keyboard callback function Private Function KeyboardCallback(ByVal Code As Integer, _ ByVal wParam As Integer, _ ByRef lParam As KBDLLHOOKSTRUCT) As Integer
If (Code = HC_ACTION) Then 'Call the appropriate event
Dim KeyData As Keys Dim args As KeyEventArgs
KeyData = lParam.vkCode
'Check if the ALT key is pressed If CBool(GetAsyncKeyState(Keys.Menu) And &H8000) Then 'Alt key pressed KeyData = KeyData Or Keys.Alt End If
'Check if the Ctrl key is pressed If CBool(GetAsyncKeyState(Keys.ControlKey) And &H8000) Then 'Ctrl key pressed KeyData = KeyData Or Keys.Control End If
'Check if the Shift key was pressed If CBool(GetAsyncKeyState(Keys.ShiftKey) And &H8000) Then 'Shift key pressed KeyData = KeyData Or Keys.Shift End If
'Create the event args args = New KeyEventArgs(KeyData)
'Raise the appropriate event If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then OnKeyDown(Me, args) ElseIf wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then OnKeyUp(Me, args) End If
'Determine if we should block the key If args.Handled Then Return 1 End If End If
'Call the next hook in the hook chain and return the value Return CallNextHookEx(KeyboardHandle, _ Code, wParam, lParam)
End Function
Protected Sub OnKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) RaiseEvent KeyDown(sender, e) End Sub
Protected Sub OnKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) RaiseEvent KeyUp(sender, e) End Sub
Public Overridable Sub Dispose() Implements IDisposable.Dispose If Not disposed Then disposed = True
'Unhook the keyboard UnhookWindowsHookEx(KeyboardHandle) Else Throw New ObjectDisposedException("KeyboardHook") End If End Sub
Public Sub New() 'Create a keyboard low-level hook
KeyboardHandle = SetWindowsHookEx( _ WH_KEYBOARD_LL, callback, _ Marshal.GetHINSTANCE( _ [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0) End Sub
Protected Overrides Sub Finalize() MyBase.Finalize()
If Not disposed Then Dispose() End If End Sub End Class
Form:
PHP Code:
Imports KBHook
Public Class KBHookTestForm Inherits System.Windows.Forms.Form Dim TheKey As String
Private WithEvents m_Hook As New KeyboardLowLevelHook
Private Sub m_Hook_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles m_Hook.KeyDown
If (e.KeyCode = Keys.TheString) Then e.Handled = True End If
End Sub
Private Sub m_Hook_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles m_Hook.KeyUp
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click TheKey = TextBox1.Text End Sub
Private Sub lbxEvents_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbxEvents.SelectedIndexChanged
End Sub
Private Sub KBHookTestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub chkBlock_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBlock.CheckedChanged
End Sub
End Class
-
Nov 4th, 2008, 05:46 PM
#11
Re: How can i do this? (something.string)
You're trying to do this:
VB.NET Code:
If (e.KeyCode = Keys.TheString) Then
e.Handled = True
End If
Which will not work. If you simply replace Keys.TheString with the variable I showed above, it'd look like this:
VB.NET Code:
If (e.KeyCode = myKey) Then
e.Handled = True
End If
Now just set myKey to anything you want, anytime you want.
-
Nov 4th, 2008, 05:50 PM
#12
Re: How can i do this? (something.string)
atheist, keeping it simple! WTG
-
Nov 4th, 2008, 05:51 PM
#13
Thread Starter
Addicted Member
Re: How can i do this? (something.string)
 Originally Posted by Atheist
You're trying to do this:
VB.NET Code:
If (e.KeyCode = Keys.TheString) Then e.Handled = True End If
Which will not work. If you simply replace Keys.TheString with the variable I showed above, it'd look like this:
VB.NET Code:
If (e.KeyCode = myKey) Then e.Handled = True End If
Now just set myKey to anything you want, anytime you want.
Okey seems to work fine F1 seems to be 112.
But I still wanna change the thing after the . using a textbox...
So how can i do this:
myKey = Keys. & TextBox1.Text
TheKey = "Keys." & TextBox1.Text
Didn't work either.
Last edited by n00b scripter; Nov 4th, 2008 at 05:56 PM.
-
Nov 4th, 2008, 05:58 PM
#14
Re: How can i do this? (something.string)
I wouldnt advice you to do it that way. You'd need the user to know the exact format of the enum name member in order to set a key. Instead, make use of the fact that when the user presses a key in the textbox, you'll be able to get its Keys-enumeration value. Here's an example:
VB.NET Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = True
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
myKey = e.KeyCode
TextBox1.Text = myKey.ToString()
End Sub
By handling the KeyPress event and setting e.Handled to true, we assure ourselves that the user can not enter anything by themselves into the textbox. And by handling the KeyUp event we can capture the pressed key and assign it to the myKey variable AND display it in the textbox.
-
Nov 4th, 2008, 06:01 PM
#15
Thread Starter
Addicted Member
Re: How can i do this? (something.string)
 Originally Posted by Atheist
I wouldnt advice you to do it that way. You'd need the user to know the exact format of the enum name member in order to set a key. Instead, make use of the fact that when the user presses a key in the textbox, you'll be able to get its Keys-enumeration value. Here's an example:
VB.NET Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = True
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
myKey = e.KeyCode
TextBox1.Text = myKey.ToString()
End Sub
By handling the KeyPress event and setting e.Handled to true, we assure ourselves that the user can not enter anything by themselves into the textbox. And by handling the KeyUp event we can capture the pressed key and assign it to the myKey variable AND display it in the textbox.
Dude thats ****ing beautiful! 
Big love <3
Swedish: Tackar ödmjukast för hjälpen
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|