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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to smack a developer! Help!

Status
Not open for further replies.

MicahDB

Programmer
Jul 15, 2004
96
US
Okay, I've been a QA for years and I'm sure there is a way to do what I need done but I have a developer saying it's not possible. He's a decent developer, but not the greatest ever. Certainly not one to do any research, so I thought I would. Here's the situation:

There's a piece of my app that is a basically a contacts area. You can either ADD a contact if you know their systems contactID or you can search for them by Name, State or Plant Code and once you find them then add them to your contacts list. So with that said, there's a section that you can simply enter a contactID and hit the 'ADD' button and another section where you enter search criteria and enter a 'SEARCH' button to return results. My question is that if my focus (cursor) is in the contactID text box and I hit 'ENTER' I want the contactID to be added. If my focus is on one of the three search text boxes and I hit 'ENTER', I want that action to return results of my search criteria. My developer says you can determine only one button on the page to be defined as the 'ENTER' Action and I'm sure there has to be away to use focus to dynamically set what the 'Enter' button does.

I hope that makes sense.

Thanks,
Micah
 
You can change the AcceptButton property of the form at any time and in response to any event.

Alternatively, you could use a private variable of type control to track the active control and determine the behaviour of the AcceptButton based on the active control.

The tricky bit is picking up exactly what is happening in the form i.e. is the control with focus actually the control that you want to respond to or has the user just clicked off the contactID textbox onto another control before pressing 'ENTER'? Another problem for your developer...
 
You could use the keypress event of the ContactID textbox control, and test for the Enter key being pressed...

Code:
Private Sub txtContactID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtContactID.KeyPress
   If e.KeyChar = Chr(13) Then
      'Do whatever to add the contact
   End If
End Sub
 
Unless I've missed something the TextBox.KeyPress event won't be fired for the 'ENTER' key when the form.AcceptButton property is set.
 
Just clear the form.acceptbutton property. Use the code that rJoubert posted to handle the keypress events of the different text boxes.

As for "impossible"... almost nothing is impossible in code, but many things are impractical. My standard response for any "Can you...?" questions is: "Yes, how much money do you have?"

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Here is some code that will work...use the keypress event

Private Sub txtContactId_KeyPress(ByVal eventSender As _ System.Object, ByVal eventArgs As _ System.Windows.Forms.KeyPressEventArgs) Handles _ txtContactId.KeyPress

Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
If KeyAscii = System.Windows.Forms.Keys.Return Then
addbutton.performclick()
GoTo EventExitSub
EndIf
EventExitSub:
eventArgs.KeyChar = Chr(KeyAscii)
If KeyAscii = 0 Then
eventArgs.Handled = True
End If
end sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top