[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?
Re: .Focus() Timing Issue
What kind of control is the preview on the left side?
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.
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?
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
Re: .Focus() Timing Issue
replace this line
Quote:
dgvAllDocuments.Focus()
with as below
vb Code:
With Me.DataGridView1
.SelectionMode = DataGridViewSelectionMode.CellSelect
.CurrentRow.Cells(0).Selected = True
'it should be noted that mouse should not be clicked on any
'control & also no other code should execute to set the focus
'on other controls , i mean like above
End With
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.
Re: .Focus() Timing Issue
Sorry, should have been more clear. It's only when I use the browser.
I'll try your suggestion.
Re: .Focus() Timing Issue
Quote:
Originally Posted by
stanav
Don't call the Focus method directly. Call the DGV.Select method instead.
Thanks Stanav, this worked perfectly.