|
-
Nov 17th, 2011, 11:19 AM
#1
Thread Starter
Junior Member
[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?
-
Nov 17th, 2011, 11:42 AM
#2
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 -
-
Nov 17th, 2011, 11:52 AM
#3
Thread Starter
Junior Member
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.
-
Nov 17th, 2011, 12:02 PM
#4
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 -
-
Nov 17th, 2011, 12:09 PM
#5
Thread Starter
Junior Member
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
-
Nov 17th, 2011, 12:17 PM
#6
Re: .Focus() Timing Issue
replace this line 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
-
Nov 17th, 2011, 12:35 PM
#7
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 -
-
Nov 17th, 2011, 12:39 PM
#8
Thread Starter
Junior Member
Re: .Focus() Timing Issue
Sorry, should have been more clear. It's only when I use the browser.
I'll try your suggestion.
-
Nov 17th, 2011, 01:20 PM
#9
Thread Starter
Junior Member
Re: .Focus() Timing Issue
 Originally Posted by stanav
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|