Results 1 to 9 of 9

Thread: [RESOLVED] .Focus() Timing Issue

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2011
    Posts
    28

    Resolved [RESOLVED] .Focus() Timing Issue

    On the left half of my screen I have a datagridview. On the right half I have a document preview pane. As I change rows on the left half, I use the CurrentCellChanged event to open the document associated to that row in the previewer. This works fine, however, the focus is then placed in the preview pane which prevents the user from being able to use the arrow keys to step down a row at a time.

    To combat this, I made the final event in my CurrentCellChanged routine set the focus back to my datagridview. Unfortunately, even this doesn’t always work because of what seems to be a timing issue. If the document to be previewed takes more than a fraction of a second to open, the code to move the focus to the datagridview is executed but after the image completes drawing, the focus is moved back to my previewer.

    Any ideas? I don’t want to use a timer to force a delay and a DoEvents causes me some other issues. Is there some other event I can use to determine when my preview pane is complete and then and only then set the focus back to my datagrid?

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: .Focus() Timing Issue

    What kind of control is the preview on the left side?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2011
    Posts
    28

    Re: .Focus() Timing Issue

    The previewer (on the right side) is actually multiple controls. One is a web browser for displaying pretty much everything that is NOT a word document. The other is a RichTextBox. If the document to be previewed is a word document, I extract the text and display it in the RTB.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: .Focus() Timing Issue

    I don't see how you you can lose focus from the DGV just by setting the text property of the RTB or call the Navigate method of you WB... Can you post the code in your DGV.CurrentCellChanged event handler?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2011
    Posts
    28

    Re: .Focus() Timing Issue

    Code:
        Private Sub dgvDocuments_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvAllDocuments.CurrentCellChanged
            If chkImageView.Checked Then
                FillDocPreview()
                dgvAllDocuments.Focus()
            End If
        End Sub
    Code:
    Private Sub FillDocPreview()
    
            If chkImageView.Checked AndAlso Not dgvAllDocuments.CurrentCell Is Nothing Then
                Dim previewPath As String = dgvAllDocuments.Item("FileName", dgvAllDocuments.CurrentRow.Index).Value.ToString
                Dim fullPath As String
                Dim displayIn As String
                Dim extension As String
                Dim tempTable As New DataTable
    
                fullPath = imageFileDirectory & "\" & previewPath
    
    
                extension = Path.GetExtension(fullPath)
                sql = "Select * from DocProfileFileTypes where FileType = '" & extension & "' "
    
                tempTable = da.GetDataTable(sql)
    
                If tempTable.Rows.Count > 0 Then
                    displayIn = tempTable.Rows(0).Item("DisplayIn").ToString
                Else
                    displayIn = String.Empty
                End If
    
                If displayIn = "Word" Then
                    Dim appWord As New WordApp.Application
                    Dim docWord As New WordApp.Document
    
                    Try
                        If System.IO.File.Exists(imageFileDirectory & "\" & previewPath) Then
                            docWord = appWord.Documents.Open(imageFileDirectory & "\" & previewPath, [ReadOnly]:=True)
    
                            docWord.ActiveWindow.Selection.WholeStory()
                            docWord.ActiveWindow.Selection.Copy()
    
                            Dim data As IDataObject = Clipboard.GetDataObject()
    
                            rtbDocumentPreview.Text = CStr(data.GetData(DataFormats.StringFormat))
                        Else
                            rtbDocumentPreview.Text = "File not found."
                        End If
    
                    Catch ex As Exception
                        Throw ex
    
                    Finally
                        rtbDocumentPreview.Visible = True
                        appWord.Quit()
                        appWord = Nothing
                    End Try
    
                ElseIf displayIn = "WebBrowser" Then
                    If System.IO.File.Exists(imageFileDirectory & "\" & previewPath) Then
                        wbDocpreview.Navigate(imageFileDirectory & "\" & previewPath)
                        rtbDocumentPreview.Visible = False
                        wbDocpreview.Visible = True
                    End If
                Else
                    rtbDocumentPreview.Text = extension & " File type not supported in preview mode."
                    rtbDocumentPreview.Visible = True
                    wbDocpreview.Visible = False
                End If
            End If
    
            'Use do events to force the screen to redraw before returning the focus to the datagrid
            Application.DoEvents()
            'dgvAllDocuments.Focus()
    
        End Sub

  6. #6
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: .Focus() Timing Issue

    replace this line
    dgvAllDocuments.Focus()
    with as below
    vb Code:
    1. With Me.DataGridView1
    2.             .SelectionMode = DataGridViewSelectionMode.CellSelect
    3.             .CurrentRow.Cells(0).Selected = True
    4.             'it should be noted that mouse should not be clicked on any
    5.             'control & also no other code should execute to set the focus
    6.             'on other controls , i mean like above
    7.         End With
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: .Focus() Timing Issue

    So you lost focus from the DVG only when the WB is used for preview, only when the RTB is used or both? I'm trying to narrow down what is stealing the focus... If it's the WB then you can handle the WB.DocumentCompleted event and send focus back to your DGV. Don't call the Focus method directly. Call the DGV.Select method instead.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2011
    Posts
    28

    Re: .Focus() Timing Issue

    Sorry, should have been more clear. It's only when I use the browser.

    I'll try your suggestion.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2011
    Posts
    28

    Re: .Focus() Timing Issue

    Quote Originally Posted by stanav View Post
    Don't call the Focus method directly. Call the DGV.Select method instead.
    Thanks Stanav, this worked perfectly.

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