Results 1 to 31 of 31

Thread: Button clicking

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Post Button clicking

    I am a beginner of the dot net framework as well as the visual basic language.

    When I add an "&" before the text of a button such as &Click then i can access the button using the "Alt + C" tabs.What to do if i want to access it using the"Ctrl+C" or some other tabs in combination with the "Ctrl" tab?

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Button clicking

    That is standard windows functionality to automatically provide those accelerator keys using Alt. If you want to change it, you would have to manually trap the keypresses you wanted in your form and then perform the appropriate action.

    Also note that Ctrl+C is standard windows functionality for copying text, so you probably want to avoid keystrokes that have other system uses, to avoid confusion.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    I cant manually trap the keypress.Can u give me the codings?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    Is this a WinForms app or an ASP.NET app? If you don't specify then people will assume that it's WinForms because ASP.NET questions should be posted in the ASP.NET forum. You've started two other threads about ASP.NET so I'm guessing that this one is too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    ya this a windows form application...

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Button clicking

    Can u give me the codings?
    You make it sound like there is a definitive answer - as with all non-trivial problems there are several ways of doing it.

    If you look at this thread from earlier today you will see a couple of ways to do it.

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Button clicking

    Mnemonic key combination's are different from handling Key events. If you set your Form's (form that Button is hosted on) KeyPreview property to True, you can use this:

    vb.net Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.         If e.Control AndAlso e.KeyCode = Keys.C Then 'Set the ''shortcut'' here
    3.             Me.SomeMethodButtonClickRuns()
    4.         End If
    5.     End Sub
    6.  
    7.     Private Sub SomeMethodButtonClickRuns()
    8.         MessageBox.Show("Success")
    9.     End Sub
    10.  
    11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         Me.SomeMethodButtonClickRuns()
    13.     End Sub

    As -0 said, Ctrl + C is a poor combination choice.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    i used the following code but its not working:

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.Control = True And e.KeyCode = Keys.P Then
    Me.Button1.PerformClick()
    End If
    End Sub
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    MessageBox.Show("This is a message box")
    End Sub

    can you give the reason why its not working?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    First up, you should use this:
    vb.net Code:
    1. If e.KeyData = (Keys.Control Or Keys.P) Then
    rather than this:
    vb.net Code:
    1. If e.Control = True And e.KeyCode = Keys.P Then
    That's because the first snippet will trap only Ctrl+P while the second will also trap Ctrl+Shift+P, Ctrl+Alt+P and Ctrl+Alt+Shift+P.

    As for why it's not working, the only reason I can think of is that you haven't set the form's KeyPreview to True, as instructed in post #7.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    ok boss....
    thanks a lot
    now this is working

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    Can u tell me how to implement the functional keys then?

    I tried it but i cant get the syntax......

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    What do you mean by "the functional keys"? Do you mean F1, F2, etc.? If so then it's exactly the same. You just have to use the appropriate values from the Keys enumeration. Not surprisingly, thos values are Keys.F1, Keys.F2, etc. Just pay attention to Intellisense as you type and it will show you the options.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    thnx

  14. #14
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Button clicking

    Quote Originally Posted by jmcilhinney View Post
    First up, you should use this:
    vb.net Code:
    1. If e.KeyData = (Keys.Control Or Keys.P) Then
    rather than this:
    vb.net Code:
    1. If e.Control = True And e.KeyCode = Keys.P Then
    That's because the first snippet will trap only Ctrl+P while the second will also trap Ctrl+Shift+P, Ctrl+Alt+P and Ctrl+Alt+Shift+P.
    That is exactly why I think he should use

    vb.net Code:
    1. If e.Control = True And e.KeyCode = Keys.P Then

    and not

    vb.net Code:
    1. If e.KeyData = (Keys.Control Or Keys.P) Then

    Quite often different combinations of Ctrl, Shift and Alt do different things and so using Keys.Control Or Keys.P limits your ability to do that.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    Quote Originally Posted by keystone_paul View Post
    That is exactly why I think he should use

    vb.net Code:
    1. If e.Control = True And e.KeyCode = Keys.P Then

    and not

    vb.net Code:
    1. If e.KeyData = (Keys.Control Or Keys.P) Then

    Quite often different combinations of Ctrl, Shift and Alt do different things and so using Keys.Control Or Keys.P limits your ability to do that.
    No, the opposite is true. Using your code, SomeMethodButtonClickRuns will be executed if the user presses Ctrl+P, Ctrl+Alt+P, Ctrl+Shift+P or Ctrl+Alt+Shift+P. Using my code, the method will only be executed if the user presses Ctrl+P. If you want all four key combinations to do the same thing then your code would be OK but that would be a very rare thing and basically bad programming.

    For instance, I'm typing this in Firefox and if I press Ctrl+Alt+P then nothing happens, while Ctrl+P brings up the Print dialogue. Using your code Ctrl+Alt+P would bring up the Print dialogue too.

    Using code like yours you'd have to do this:
    vb.net Code:
    1. If e.Control = True And e.Alt = False And e.Shift = False And e.KeyCode = Keys.P Then
    or, better:
    vb.net Code:
    1. If e.Control AndAlso Not e.Alt AndAlso Not e.Shift AndAlso e.KeyCode = Keys.P Then
    I think:
    vb.net Code:
    1. If e.KeyData = (Keys.Control Or Keys.P) Then
    is a better option.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Button clicking

    Quote Originally Posted by jmcilhinney View Post
    No, the opposite is true. Using your code, SomeMethodButtonClickRuns will be executed if the user presses Ctrl+P, Ctrl+Alt+P, Ctrl+Shift+P or Ctrl+Alt+Shift+P. Using my code, the method will only be executed if the user presses Ctrl+P. If you want all four key combinations to do the same thing then your code would be OK but that would be a very rare thing and basically bad programming.

    For instance, I'm typing this in Firefox and if I press Ctrl+Alt+P then nothing happens, while Ctrl+P brings up the Print dialogue. Using your code Ctrl+Alt+P would bring up the Print dialogue too.

    Using code like yours you'd have to do this:
    vb.net Code:
    1. If e.Control = True And e.Alt = False And e.Shift = False And e.KeyCode = Keys.P Then
    or, better:
    vb.net Code:
    1. If e.Control AndAlso Not e.Alt AndAlso Not e.Shift AndAlso e.KeyCode = Keys.P Then
    I think:
    vb.net Code:
    1. If e.KeyData = (Keys.Control Or Keys.P) Then
    is a better option.
    My apologies you are quite right - ignore me. My brain is on holiday somewhere today!

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    Quote Originally Posted by keystone_paul View Post
    My apologies you are quite right - ignore me. My brain is on holiday somewhere today!
    Once we get on a track, it's sometimes hard to get off. We always assume that our previous assumptions are true.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    i am trying to access the open menu by pressing the ctrl+o key
    so i did the following:
    [IMG]C:\Users\Gautam\Desktop\Untitled.jpg[/IMG]

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    If e.Control = True And e.KeyCode = Keys.O Then
    Me.OpenToolStripMenuItem.PerformClick()
    End If
    End Sub

    but this is not working

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    i cant attach images to my post.....
    can you suggest how can i do it?

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    The point of setting the form's KeyPreview property to True has been brought up several times and you are yet to confirm that you have done so.

    Also, because you're handling the KeyUp event that code will do nothing if you release the Control key first. e.KeyCode wil only be equal to Keys.O when you release the O key and e.Control will not be True if you've already released the Control key. You should follow Windows convention and handle the KeyDown event. Handling the KeyUp event will make your app behave differently to other apps and differently to the Windows standard. Unless you have a very good reason for the difference, that's a bad thing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    Quote Originally Posted by gautamshaw View Post
    i cant attach images to my post.....
    can you suggest how can i do it?
    Maybe you should read the FAQ. That's what it's for.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  22. #22

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    you get me wrong.........

    consider a notepad,ok

    when we click the open button then a form appears and at the bottom right hand side there are two buttons:Open and Cancel.

    so for this i did the following:

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    If e.Control = True And e.KeyCode = Keys.O Then
    Me.OpenToolStripMenuItem.PerformClick()
    End If
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    System.Diagnostics.Process.Start("C:\Users\Gautam\Documents")
    End Sub


    But here i am not gettin the one that appears when we click "Ctrl+O" in the notepad

    now what should i do?

  23. #23
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    No, it's you who are getting me wrong. I will try to explain this as clearly as possible. Every form has a KeyPreview property. If that property is set to False, which it is by default, then the form will not raise any KeyDown, KeyPress or KeyUp events if a control has focus. You need to set your form's KeyPreview property to True so that your form DOES raise those events.

    Having said that, it's not necessary to do this manually for menu items anyway. The menu item has a ShortCutKeys property that you can simply set to Ctrl+O in the designer and it will be handled automatically.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  24. #24

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    If e.Control = True And e.KeyCode = Keys.O Then
    Me.OpenToolStripMenuItem.PerformClick()
    End If
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    System.Diagnostics.Process.Start("C:\Users\Gautam\Documents")
    End Sub

    in the above code,i want to pop up a form having two buttons "Open" and "Cancel" when i clicl the "Ctrl+O" keys

    I can access the new form having the path mentioned above using the "Ctrl+O" keys but what to do for the "Open" and "Cancel" buttons in the form that comes after clicking the button?

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    You already know how to do that because it was the point of this thread in the first place. You just have to put the code in that form. Putting code in Form1 to access Buttons on a different form is no use. If the form containing those Buttons has focus then it will be raising the keyboard events so you must handle the events of that form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  26. #26

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    i think i cant explain what i exactly want to do.....

    let me learn first how to post images in this site and then i will try my best to explain you what exactly i want to do

    thanx a lot for the help

  27. #27
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    I'm starting to get the feeling that you're asking for something that has nothing to do with the topic of this thread. If so, that would explain why you're not getting the answers you want. You need to keep each thread to a single topic and each topic to a single thread.

    I think you're asking how to get your Open menu item to display a dialogue that will allow you to open a file. That is completely unrelated to the topic of this thread, which is how invoke a Button Click using the keyboard.

    If that is what you're asking for then you simply need to create and display an OpenFileDialog:
    vb.net Code:
    1. Using ofd As New OpenFileDialog
    2.     'Set properties here.
    3.  
    4.     If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
    5.         'Open file here.
    6.     End If
    7. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  28. #28

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Question Re: Button clicking

    by the below coding:

    Attachment 71846


    the output i m getting when i click "Ctrl+O"
    is like this:


    Attachment 71848


    But i want to get the output as:

    Attachment 71845


    i.e,i want two buttons "OPEN" and "Cancel" at the bottom right

    How to do this?
    Last edited by gautamshaw; Feb 21st, 2010 at 01:23 PM.

  29. #29
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    I explained how in my previous post. In future, please keep each topic to a single thread and each thread to a single topic.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  30. #30

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Button clicking

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    Using ofd As New OpenFileDialog
    If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
    End If
    End Using
    End Sub

    can someone explain me the above codings please?
    it worked for me

  31. #31
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button clicking

    It creates an OpenFileDialog, displays it and then destroys it. You have conveniently omitted the comments I provided, which you are supposed to replace with actual code that does what the comments suggest. If you want to know how to configure the OpenFileDialog then you should read about it, what properties it has and what they do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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