|
-
Jul 7th, 2009, 09:11 PM
#1
Thread Starter
Frenzied Member
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?
-
Jul 7th, 2009, 09:16 PM
#2
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.
-
Jul 7th, 2009, 09:37 PM
#3
Thread Starter
Frenzied Member
Re: Button clicking
I cant manually trap the keypress.Can u give me the codings?
-
Jul 7th, 2009, 10:24 PM
#4
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.
-
Jul 8th, 2009, 10:42 AM
#5
Thread Starter
Frenzied Member
Re: Button clicking
ya this a windows form application...
-
Jul 8th, 2009, 10:47 AM
#6
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.
-
Jul 8th, 2009, 10:56 AM
#7
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:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.Control AndAlso e.KeyCode = Keys.C Then 'Set the ''shortcut'' here Me.SomeMethodButtonClickRuns() End If End Sub Private Sub SomeMethodButtonClickRuns() MessageBox.Show("Success") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.SomeMethodButtonClickRuns() End Sub
As -0 said, Ctrl + C is a poor combination choice.
-
Jul 8th, 2009, 10:36 PM
#8
Thread Starter
Frenzied Member
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?
-
Jul 8th, 2009, 10:47 PM
#9
Re: Button clicking
First up, you should use this:
vb.net Code:
If e.KeyData = (Keys.Control Or Keys.P) Then
rather than this:
vb.net Code:
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.
-
Jul 8th, 2009, 11:37 PM
#10
Thread Starter
Frenzied Member
Re: Button clicking
ok boss....
thanks a lot
now this is working
-
Jul 8th, 2009, 11:39 PM
#11
Thread Starter
Frenzied Member
Re: Button clicking
Can u tell me how to implement the functional keys then?
I tried it but i cant get the syntax......
-
Jul 9th, 2009, 12:54 AM
#12
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.
-
Jul 9th, 2009, 01:20 AM
#13
Thread Starter
Frenzied Member
-
Jul 9th, 2009, 02:49 AM
#14
Re: Button clicking
 Originally Posted by jmcilhinney
First up, you should use this:
vb.net Code:
If e.KeyData = (Keys.Control Or Keys.P) Then
rather than this:
vb.net Code:
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:
If e.Control = True And e.KeyCode = Keys.P Then
and not
vb.net Code:
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.
-
Jul 9th, 2009, 04:44 AM
#15
Re: Button clicking
 Originally Posted by keystone_paul
That is exactly why I think he should use
vb.net Code:
If e.Control = True And e.KeyCode = Keys.P Then
and not
vb.net Code:
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:
If e.Control = True And e.Alt = False And e.Shift = False And e.KeyCode = Keys.P Then
or, better:
vb.net Code:
If e.Control AndAlso Not e.Alt AndAlso Not e.Shift AndAlso e.KeyCode = Keys.P Then
I think:
vb.net Code:
If e.KeyData = (Keys.Control Or Keys.P) Then
is a better option.
-
Jul 9th, 2009, 04:47 AM
#16
Re: Button clicking
 Originally Posted by jmcilhinney
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:
If e.Control = True And e.Alt = False And e.Shift = False And e.KeyCode = Keys.P Then
or, better:
vb.net Code:
If e.Control AndAlso Not e.Alt AndAlso Not e.Shift AndAlso e.KeyCode = Keys.P Then
I think:
vb.net Code:
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!
-
Jul 9th, 2009, 04:52 AM
#17
Re: Button clicking
 Originally Posted by keystone_paul
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.
-
Jul 9th, 2009, 10:24 PM
#18
Thread Starter
Frenzied Member
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
-
Jul 9th, 2009, 10:32 PM
#19
Thread Starter
Frenzied Member
Re: Button clicking
i cant attach images to my post.....
can you suggest how can i do it?
-
Jul 9th, 2009, 10:32 PM
#20
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.
-
Jul 9th, 2009, 10:34 PM
#21
Re: Button clicking
 Originally Posted by gautamshaw
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.
-
Jul 9th, 2009, 10:51 PM
#22
Thread Starter
Frenzied Member
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?
-
Jul 9th, 2009, 10:57 PM
#23
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.
-
Jul 9th, 2009, 11:03 PM
#24
Thread Starter
Frenzied Member
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?
-
Jul 9th, 2009, 11:08 PM
#25
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.
-
Jul 9th, 2009, 11:11 PM
#26
Thread Starter
Frenzied Member
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
-
Jul 9th, 2009, 11:22 PM
#27
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:
Using ofd As New OpenFileDialog 'Set properties here. If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then 'Open file here. End If End Using
-
Jul 9th, 2009, 11:35 PM
#28
Thread Starter
Frenzied Member
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.
-
Jul 9th, 2009, 11:38 PM
#29
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.
-
Jul 10th, 2009, 10:12 AM
#30
Thread Starter
Frenzied Member
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
-
Jul 10th, 2009, 10:23 AM
#31
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.
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
|