0

I have a form with + 50 controls and with key numeric screen to write in only 5 textbox of this form.

I am using this code to write in these textbox using key numeric screen:

Private Sub bt0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt0.Click, bt1.Click, bt2.Click, bt3.Click, bt4.Click, bt5.Click, bt6.Click, bt7.Click, bt8.Click, bt9.Click, btDec.Click
        If TypeOf sender Is DevExpress.XtraEditors.SimpleButton Then
            txtRefe.Focus()
            SendKeys.Send(CType(sender, DevExpress.XtraEditors.SimpleButton).Text)
        End If
    End Sub

The problem: I need to know which of these 5 textbox had the focus before touching the numerical button

I have seen this code from this post Find out the control with last focus to find last focus:

Private _focusedControl As Control
    Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As EventArgs)
        _focusedControl = DirectCast(sender, Control)
    End Sub
    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        If _focusedControl IsNot Nothing Then
            'Change the color of the previously-focused textbox
            _focusedControl.BackColor = Color.Red
        End If
    End Sub

But how I do it in a form with + 50 controls (Controls of many types: buttons, checkbox, combos, textbox, etc.) ?

Community
  • 1
  • 1
aco
  • 819
  • 4
  • 15
  • 32

2 Answers2

0

You can recursively loop through the form's Controls collection and add event handlers.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

The simplest and most trivial solution that comes to anyone's mind is to make a flag holding a datatype of your choice. Let's say int, then what you have to do is to update that flag with a different value for each focus event of each textBox. For the sake of simplicity,

When textBox1 gets focused -> set the flag value to 1
when textBox2 gets focused -> set the flag value to 2
when textBox3 gets focused -> set the flag value to 3
when textBox4 gets focused -> set the flag value to 4
when textBox5 gets focused -> set the flag value to 5

Now you are keeping record for the last focused textBox. You can do a switch statement to handle each of the 5 cases we have

   Select Case flag
    case 1: 'code for textBox1
    case 2: 'code for textBox2
    case 3: 'code for textBox3
    case 4: 'code for textBox4
    case 5: 'code for textBox5
   End Select
dlock
  • 9,447
  • 9
  • 47
  • 67
  • This will not runs. Previosuly i have tried something. WHy will not run? my form has + 50 controls (checkbox, combos, buttons, etc.) if i only handle 5 textbox, when other control get the focus, The variable will have an unreal value stored. – aco Jan 19 '11 at 21:00
  • I don't get you. All you need to have is 5 different focus event handlers for each textBox of the 5. Each event handler will change the flag value to something different. What do the other controls have to do with our matter? – dlock Jan 19 '11 at 21:10
  • for example, if a grid has focus and the user clicks the numeric button, flag contains that textbox has focus and is incorrect. Last focus -> grid – aco Jan 20 '11 at 10:34
  • yae, don't you want to save the focus of textboxes only? wasn't that your question? you are confusing me. – dlock Jan 20 '11 at 14:30