SearchField.Pressed help

My app is almost complete with, I believe, just one more problem to solve. I placed a SearchField in the UI and a search button next to it. I have coded the search button to get the correct record for the name entered into the SearchField, from the SQLite database and it works very well. The problem I need to solve now is, how to let the user choose between clicking on the button or pressing the Enter key to find the record. I obviously can’t put the SearchField.Pressed code into the button action code and I’m not sure where I can put it?

You could have your code in the PushButton’s Pressed event then call the Press method of the PushButton from the SearchField. But a better solution would be to create a method to perform the search and call that from both.

@Anthony_G_Cyphers Thanks, I’ll give the method a try.

I don’t know think I’m doing this right,…
I have my code to search the database in the searchBtn Action, and in the searchBox KeyDown I have this:

If Keyboard.AsyncKeyDown(&h0D) And focus = searchBox Then
End If

When I run the app, I type the search string into the searchBox and press the Enter Key. Istead of running the code in the searchBtn Action, it quits the program.

I am obviously doing this wrong. Can anyone show me how it should be done?

I didn’t do the method way because I wasn’t sure where to put the method or how to call it. The searchBtn.Press seemed like it would be easier but,… maybe not.


You would create a new method on your window, and call that from both controls. For a simple test, here’s what I setup:

Public Sub doSearch(searchFor as String)
  '// Do your SQL search here instead
  var d as new date
  self.Title = "Searching [" + searchFor + "]..." + Str( d.TotalSeconds, "#" )
End Sub

Then the PushButton’s Action event:

Sub Action() Handles Action
End Sub

And finally the SearchField’s Pressed event:

Sub Pressed() Handles Pressed
End Sub

Typically, though, folks group their database code together, so you might have the method in a module and you’d pass the search string as a parameter and return the results to be loaded.

I didn’t replicate your crash issue, but if you can reliably reproduce it then you should open a Feedback Case.

Enter Key is not Return Key (0D)…

And place your search code into a Method you call from both places.

an this looks strange to me:


If you use a SearchField, you can use KeyDown or KeyUp Events. There you get the depressed Key and test wht it is and act accordingly.

Something like:

If Key = Chr(4) Or Key = Chr(13) Then
// Enter or Return was pressed
End If

Not sure what you’re doing here, but the SearchField has a Pressed event that is raised from the Return key. Looks like you’re overthinking it. If you’re trying to capture Return window-wide, set the button’s Default property to True.

(And just saw Emile’s post. Don’t use Key events when there’s already a built-in event)

When I go to insert>Event Handler I do not see a “Pressed” event. Only a KeyDown event.

Are you certain you had the SearchField selected? Are you using a third-party SearchField control? What version of Xojo are you using?

I just checked both Web and Desktop, each SearchField control has a Pressed event in 2020R2.1.

Do you already have a handler for the Pressed event in the left-hand navigator pane under your SearchField instance?

You even said in your first post that you were using the SearchField.Pressed event…

Let me clarify,…
I had been using a TextField, which I named searchBox, with a button. I coded the button Action to take the text from the TextField and search for that record. That worked perfectly.

A lot of people like to press the Enter Key rather than click a button so I was trying to figure out how to execute the code in the button Action if the user pressed the Enter Key. I thought perhaps I could simulate clicking the button if the user pressed the Enter Key. I inserted a KeyDown event and used:

If Keyboard.AsyncKeyDown(&h0D) And focus = searchBox Then
End If

That did not work for the Enter Key. Then I tried:

If Keyboard.AsyncKeyDown(&h24) And focus = searchBox Then
End If

That did not work either however, if I use Keyboard.AsyncKeyDown(&h00), which is the ‘a’ key, and I pressed the ‘a’ key it worked. It also worked for the ESC key. For some reason it just wouldn’t work with the main Enter Key or the Keypad Enter Key.

After seeing your post I removed the TextField and used a SearchField. I inserted a Pressed event and for testing purposes I added the same code that I used in the button Action but it just crashed the app.

I just tried using:

If Key = Chr(4) or Key = Chr(13) Then
End If

And I got the MessageBox as I should, however, If I put in the code to do the search, it crashes, even though it works great in the searchBtn Action.

Yes, follow Emile’s clues and code !

I discouraged your suggestion because their topic title and first post said they were using the SearchField. Since it’s now apparent that they’re not, and their title was wrong, first post was erroneous, and my suggestions for naught, your advice is correct.

No problem Anthony. I could be wrong. This was apparently not the case, but sometimes there are alternate understanding and in these cases, I always takes the wrong one :wink:

I searched in the documentation what looks like what the OP says, found SearchField. But I do not noticed Pressed until you write about it ;-:slight_smile:

At last, discouraging my suggestion for a better one (as it seems) is always welcome !

If I use a searchField, add a Pressed event handler and add this code: MessageBox(“Enter Key was pressed”) then run the app and hit the Enter Key, the app crashes. I don’t know why.

If I use a TextField and I add a KeyDown event handler to the TextField and put this code into it:

If Key = Chr(4) Or Key = Chr(13) Then
MessageBox(“Enter Key was pressed”)
End If

When I run the app and hit the Enter Key it displays the MessageBox as it should, however, when I click the “OK” button, the app crashes. If I change the code to:

If Key = Chr(4) Or Key = Chr(13) Then
End If

The app crashes right away. The code works perfectly when used with the searchBtn so why does it crash when using the SearchField?

If I put the exact same code that is working in the searchBtn Action into the SearchField Pressed event, it crashes. Why?

Since I have only been testing xojo for a week now, it is harder for me to figure out why certain things are happening. I am using Version 2020, Release 2.1.

Under which OS?

Also: what do you mean by “crashes”? Does it:

  1. go to the Xojo debugger

  2. immediately cease execution altogether and vanish

  3. give you an OS crash dump?

  4. something else

I am using Windows. By “crashes” I mean that it goes back into the xojo IDE. No error is flagged.

If I use this:

If Keyboard.AsyncKeyDown(&h35) Then //&h35 = hex for ESC Key on PC
End If

It works fine! If I use this:

If Keyboard.AsyncKeyDown(&h7B) Then //&h7B = hex for Left Arrow Key on PC
End If

It works fine! If I use this:

If Keyboard.AsyncKeyDown(&h0D) Then //&h0D = hex for Enter Key on PC
End If

It crashes! ???

So are you back in the IDE debugger or has the executing app gone away and you’re back to the IDE edit/design panes?

Back to the IDE edit/design panes? It seems that I can use the hex code for any key on the keyboard except the Enter Key and I can’t figure out why.

Tried putting a breakpoint just before that if and then stepping through?