TextBox Focus method

RacingDog

Active Member
Licensed User
I got bored with when I have a table I have to first select the cell I want to update, then select the TextBox the update value goes in. So I tried adding MyTextBox.Focus to the table's SelectionChanged event handler, with the intention of saving the extra click or tap. Sadly, this seemed to be ignored. Checking with the Help file, I found that the method "will try" to move the focus to control. Taking that literally, it implies that in some circumstances the attempt will fail. Is there any rule of thumb as to where this method works and where it doesn't? (Just in case there may be some bodge I could dream up :D)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The problem here is that the focus returns to the table control after the event is consumed (this is done internally by the OS or the control).
To workaround it you can use a timer with a very short interval (1ms should be enough) and in the tick event move the focus:
B4X:
Sub Table_SelectionChanged
  tmrFocus.Enabled = true
End Sub
Sub tmrFocus_Tick
 tmrFocus.Enabled = false
 textbox1.focus
End Sub
 

RacingDog

Active Member
Licensed User
Thanks. I had half a suspicion that might be the case. But it's nice to know for sure about these things.
 
Top