3 Replies - 6501 Views - Last Post: 22 October 2010 - 12:29 PM Rate Topic: -----

#1 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

touchscreen keyboard, SendKeys, returning focus to original Control

Posted 22 October 2010 - 08:10 AM

This isn't really a VB specific question. It's .Net in general... if you have ideas in another .Net language, let me know... I know most of the .Net languages (I don't know that foxtrot or what not weird ones). Furthermore this isn't a 'I don't know the method on some class I should be using' kind of question, it's a design/problem solving kind of question. I'd like to know how you'd approach the problem with in the boundaries set.






Ok... I have written a custom control, it's a numberpad that dispatches an event when one of its buttons are clicked and supplies the KeyChar that was clicked (it's 0->9 and '.', but can support any character if needed).

I also have a custom Form type called 'AbstractDMWindowForm' and extends System.Windows.Forms.Form. It is used as the base class of any Forms in our project. It implements some common code that all our forms use. One of them is it watches the LostFocus event of all the children controls of itself and remembers which was the last control to have focus before the current one. I can then easily access a property 'LastActiveControl' to find out which control it was. Very useful for several things...


One of those useful things is combining it with the numberpad for an onscreen keyboard (meant for a touch screen). When I click on the number pad and receive the event I use SendKeys to emulate a keyboard keypress. But I want that text to go into the control that was last active, and clicking the numpad steals focus. So I set focus back to that 'LastActiveControl', then send the key stroke.

        Private Sub pad_Numpad_Input(ByVal sender As Object, ByVal e As Controls.NumpadInputEventArgs)

            _bIsNumpadTyping = True

            If Me.LastActiveChildControl IsNot Nothing Then
                Me.LastActiveChildControl.Focus()
            End If

            SendKeys.Send(e.Keys)

            _bIsNumpadTyping = False
        End Sub





Now this works wonderfully for most Controls that can be typed in (ComboBox, TextBox, etc). But one it has a total bitch with (and I hate the damn thing in general, but we use it a lot in the project), and that is the DataGridView with text cells.

What happens is when I return focus to the DataGridView the last cell was selected is still the selected cell. That's good, that's what I expected. BUT now all the text in the cell is selected... ? I'd like to stop this, but don't know exactly how I should go about it.

Any ideas?




The only idea I have so far is to check if the LastActiveChildControl is a DataGridView and that it has a selected cell, and if that cell is a text cell and is activated for edit, then we ensure that it's not SelectAll.

But I find this dirty AND it removes support for AllSelect and typing on the numpad (what if the user did select all the text and now expects this key stroke to replace the whole thing...).

This post has been edited by lordofduct: 22 October 2010 - 08:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: touchscreen keyboard, SendKeys, returning focus to original Control

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: touchscreen keyboard, SendKeys, returning focus to original Control

Posted 22 October 2010 - 10:02 AM

Sorry, I'm not at a computer where I could look this up but I would think you could set up a gotfocus type event on the datagrid and if you're in a textbox, set the selectionlength to 0 and the selectionstart to the length of the text that is in it then add the key from SendKeys.

Another method would be prior to the sendkey, store what is in the field of the last control to have focus and then append your sent key to that and place it back.

These are just two thoughts without being able to play around with it.

This post has been edited by CharlieMay: 22 October 2010 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

Re: touchscreen keyboard, SendKeys, returning focus to original Control

Posted 22 October 2010 - 10:57 AM

That's pretty much what I described at the end of my post. And it comes with the downside of not remembering the state that it was in (if the user did 'select all' on that cell... then they'd expect it to be).

I just did some research, I uncovered some of the reason why it occurs like this... ugh. The main reason is because when a cell needs editing it creates a Control to be used as the editing point, performs the editing, then when leaving it redraws the cell and removes the editing control (hence the EditingControl property). So what happens when you re-enter the grid a new EditingControl is created in default which is fully selected.



Anyways this leads to some really big annoyances when editing. If I want it to ALWAYS append to the end of the line, that's fine. But I want it do accept more, so it's not really a feasable solution.

This post has been edited by lordofduct: 22 October 2010 - 11:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

Re: touchscreen keyboard, SendKeys, returning focus to original Control

Posted 22 October 2010 - 12:29 PM

So I found one hell of a hack to cover this one...

ok.

Whenever a DataGridView is left, I check it's EditingControl. If it exists then I set the forms ActiveControl to the EditingControl (why this works I do not know, hacky my hacks a lot here).

Then when I go to do the 'SendKeys' I check to see if the 'lastActiveControl' is a DataGridView, if it is I don't say 'Focus()', but instead say 'BeginEdit(False)'

like so:

''where __targ is the form... this is in a policy file for a multi use form, so targ is the AbstractDMWindowForm that acts as the view

        Private Sub targ_ChildLeave(ByVal sender As Object, ByVal e As EventArgs)

            Dim cntrl As Control = __targ.LastActiveChildControl

            If TypeOf cntrl Is DataGridView Then

                Dim grd As DataGridView = DirectCast(cntrl, DataGridView)

                If grd.EditingControl IsNot Nothing Then
                    __targ.ActiveControl = grd.EditingControl
                End If
            End If

        End Sub

        Private Sub pad_Numpad_Input(ByVal sender As Object, ByVal e As Controls.NumpadInputEventArgs)

            _bIsNumpadTyping = True

            Dim cntrl As Control = __targ.LastActiveChildControl

            If cntrl IsNot Nothing Then
                If TypeOf cntrl Is DataGridView Then
                    DirectCast(cntrl, DataGridView).BeginEdit(False)

                Else
                    cntrl.Focus()

                End If
            End If

            SendKeys.Send(e.Keys)

            _bIsNumpadTyping = False
        End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1