Results 1 to 15 of 15

Thread: [RESOLVED]How can i do this? (something.string)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Resolved [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.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Private Sub SetProperty(ByVal ctrl As Control, ByVal propName As String, ByVal propValue As Object)
    2.     Dim propInfo As Sytem.Reflection.PropertyInfo = ctrl.GetType().GetProperty(propName)
    3.     If Not propInfo Is Nothing Then
    4.         propInfo.SetValue(ctrl, propValue, null)
    5.     End If
    6. 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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How can i do this? (something.string)

    what are you really trying to do?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: How can i do this? (something.string)

    Quote 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

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How can i do this? (something.string)

    i think e.keycode is read only.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    VB.NET Code:
    1. Dim myKey As Keys
    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How can i do this? (something.string)

    why don't you press the entire event you are working on.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: How can i do this? (something.string)

    Quote 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:
    VB.NET Code:
    1. Dim myKey As Keys
    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'.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    VB.NET Code:
    1. Keys.myKey
    Which isnt going to work. Post your entire subroutine.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: How can i do this? (something.string)

    Quote 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:
    VB.NET Code:
    1. Keys.myKey
    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
    (Meargs)
                ElseIf 
    wParam WM_KEYUP Or wParam WM_SYSKEYUP Then
                    OnKeyUp
    (Meargs)
                
    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
    wParamlParam)

        
    End Function

        Protected 
    Sub OnKeyDown(ByVal sender As ObjectByVal e As KeyEventArgs)
            
    RaiseEvent KeyDown(sendere)
        
    End Sub

        
    Protected Sub OnKeyUp(ByVal sender As ObjectByVal e As KeyEventArgs)
            
    RaiseEvent KeyUp(sendere)
        
    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)).ToInt320)
        
    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 ObjectByVal e As System.Windows.Forms.KeyEventArgsHandles m_Hook.KeyDown

                
    If (e.KeyCode Keys.TheStringThen
                    e
    .Handled True
            End 
    If

        
    End Sub

        
    Private Sub m_Hook_KeyUp(ByVal sender As ObjectByVal e As System.Windows.Forms.KeyEventArgsHandles m_Hook.KeyUp

        End Sub

        
    Private Sub btnClear_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles btnClear.Click
            TheKey 
    TextBox1.Text
        End Sub

        
    Private Sub lbxEvents_SelectedIndexChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles lbxEvents.SelectedIndexChanged

        End Sub

        
    Private Sub KBHookTestForm_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandles MyBase.Load


        End Sub

        
    Private Sub chkBlock_CheckedChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles chkBlock.CheckedChanged

        End Sub

    End 
    Class 

  11. #11
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: How can i do this? (something.string)

    You're trying to do this:
    VB.NET Code:
    1. If (e.KeyCode = Keys.TheString) Then
    2.                 e.Handled = True
    3.         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:
    1. If (e.KeyCode = myKey) Then
    2.                 e.Handled = True
    3.         End If

    Now just set myKey to anything you want, anytime you want.
    VB.NET Code:
    1. myKey = Keys.A
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How can i do this? (something.string)

    atheist, keeping it simple! WTG
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: How can i do this? (something.string)

    Quote Originally Posted by Atheist
    You're trying to do this:
    VB.NET Code:
    1. If (e.KeyCode = Keys.TheString) Then
    2.                 e.Handled = True
    3.         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:
    1. If (e.KeyCode = myKey) Then
    2.                 e.Handled = True
    3.         End If

    Now just set myKey to anything you want, anytime you want.
    VB.NET Code:
    1. myKey = Keys.A
    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.

  14. #14
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         e.Handled = True
    3.     End Sub
    4.  
    5.     Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    6.         myKey = e.KeyCode
    7.         TextBox1.Text = myKey.ToString()
    8.     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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: How can i do this? (something.string)

    Quote 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:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         e.Handled = True
    3.     End Sub
    4.  
    5.     Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    6.         myKey = e.KeyCode
    7.         TextBox1.Text = myKey.ToString()
    8.     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
  •  



Click Here to Expand Forum to Full Width