Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

spacebar and enter keys acting as a click 2

Status
Not open for further replies.

bouwob

Programmer
Apr 12, 2006
171
US
I have a windows form that has a series of buttons on it. The problem is that if you click on a button then hit the spacebar or enter key it toggles the button. Is there something in vs 1.1 that sets these to keys up to act as a click and how do I go about disableing them?

tia
 

Ok so maybe it is not possable to disable them. :(

I tried the only "hack" I could think of to get around this with throwing

this.btnSpaceEnterFix.Focus();
this.btnSpaceEnterFix.PerformClick();

at the end of every button click event. I made the button invisable and the click event for that button does nothing.

Unfortunatly this didnt work either so I am back to square 1.

anybody else have any ideas on how to get around this?
 
You could use some of the key events to check what key is being pressed (I recommend keypress event).

I don't have C# at work, but I think it goes something like this


Code:
if(e.key == Keys.Enter || e.key = Keys.Space)
{
     //Do Some Action, I believe its' e.Handled = true; ?
}

You may need to substitute ' ' for Keys.Space, I have not really used these events too much.

Hope it helps,

Alex


Ignorance of certain subjects is a great part of wisdom
 
ok so I have this in my

#region Windows Form Designer generated code
private void InitializeComponent()


this.KeyUp += new KeyEventHandler(OnKeyPress);

Code:
public void OnKeyPress(object sender, KeyEventArgs e)
{
	if(e.key == Keys.Enter || e.KeyValue = Keys.Space)
	{
		e.Handled = true;
	}
}

e.key does not exist. Also if I add a stop in the method it never hits OnKeyPress( which leads me to believe that the event handler is not set up correctly.

If I can get into the function I should be able to figue the rest out by doing a quickwatch.

tia
 
Write this in OnKeyDown.
Inside the if write: ((button)object).performClick()
You can also call the click method will null args.
 
I missunderstood.

I though you wanted to click the button by pressing the key inside the textbox...
 
Heya bouwob,

I'm sorry, it should have been e.KeyChar, not e.Key. However, I hacked around for about half an hour last night and could not get it to suppress the 'enter' key.

This seems on the right track, but did not work for your button clicking issue.

Code:
private void txt1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                e.SuppressKeyPress = true;
        }
It seems (to me, I am far from an expert) that the default behavior is to treat the button as clicked before handling the key events. I would think that any 'fix' to this problem could create a lot of problems. Let me know if you do come up with anything, but I think its' probably best to leave this behavior as is.

Good Luck,

Alex

Ignorance of certain subjects is a great part of wisdom
 
I agree. I spent about 6 hours tring to figure out away around it and came up with no solution. Told the boss that is was a microsoft issue and that I could not do anything about it. He didnt believe me at first, but then I had him open 6 other programs from different vendors and they all acted the same way.

Thanks for you help anyways. I really appreciate you guys taking the time to help me out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top