Results 1 to 8 of 8

Thread: [RESOLVED] [2008] DataGridView focus problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Resolved [RESOLVED] [2008] DataGridView focus problem

    Hi,
    I've got a DataGridView which isn't receiving the focus.
    I've got a bound DataGridView, and bound TextBoxes, so when the user scrolls through the grid, the text boxes display data associated with whatever is selected in the grid.
    The user can press "U" (to update) the data, and a TabControl containing the text boxes becomes enabled so the user can make some changes.
    What I'm finding is that if the user abandons the changes (where all the TextBoxes, Combo's revert back to their original state), I can't put the focus back to the DataGridView UNLESS I put a messagebox in (perhaps saying "Changes abandoned" - which might be ok, anyway).
    If I don't put the message box in, the focus moves to the menus.
    Its as if delaying the DataGridView.Focus, via the messagebox, allows the .Focus to work.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2008] DataGridView focus problem


    Show us the code surrounding the .Focus method.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] DataGridView focus problem

    This works, but if I comment out the MessageBox, the focus isn't set to dgv.
    Code:
    Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ....
    
            If e.KeyCode = Keys.F10 Then
                bind.CancelEdit()
                Me.TabControl1.Enabled = False
                Me.dgv.Enabled = True
                Me.dgv.Focus()
                MessageBox.Show("Changes have been abandoned", "Abort", MessageBoxButtons.OK, MessageBoxIcon.Information)
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  4. #4
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2008] DataGridView focus problem



    Have you tried commenting out the msgbox line and stepping through the code after the call to dgv.focus? I'm just curious as to whether it's hitting some other event code somewhere else in the app.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] DataGridView focus problem


    I didn't try that, but I'm glad I did now!
    REALLY bizarre. I break on .Focus, and stepping through shows that it does everything that it should through the end of the sub. If I continue as normal when the sub has been processed, I go back to the form, and the dgv has the focus!!!
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  6. #6
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2008] DataGridView focus problem

    I wonder if an application.doevents in the place of the msgbox will help?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] DataGridView focus problem

    Errmmm, yes, this works.
    Now that was strange!
    Many thanks for the suggestion!
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  8. #8
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2008] DataGridView focus problem

    No prob. And I agree fully... it's wierd. I suspect this isn't the 'best' solution, but hey, it works.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width