Performing operations on out of focus windows forms VB 2005

I have a Mdi application I am coding. One screen is a datagridview and from that the user double clicks a record to open a new window. On the new window I have next and previous buttons. Is it possible have the highlight on the datagrid move with the click of the next and previous buttons without leaving the focus of the details window?
[338 byte] By [CoderD] at [2007-12-28]
# 1

One way would be to make a public sub on the form with the datagridview to move the highlight and call this sub from the buttons on your other form.

The sub could look like:

Public Sub ChangeRow(ByVal UpDown As Boolean)
' UpDown True moves Up
' UpDown False moves Down
Dim Row As Integer = DGV1.CurrentRow.Index
Dim Column As Integer = DGV1.CurrentCell.ColumnIndex
If Row = 0 And UpDown Then Exit Sub
If Row = DGV1.RowCount - 1 And Not UpDown Then Exit Sub
If UpDown Then
DGV1.CurrentCell = DGV1(Column, Row - 1)
Else
DGV1.CurrentCell = DGV1(Column, Row + 1)
End If
End Sub

Dave299 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
thanks I'll give it a try.
CoderD at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

So I have been giving it a try but it seems when I try to get back to the datagrid screen from the details screen the col and row properties are set back to 0 for the gridview object.

Actually they go to 0 when the grid is out of focus.

CoderD at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Public Sub ChangeRow(ByVal UpDown As Boolean)
' UpDown True moves Up
' UpDown False moves Down
Static Row As Integer = DGV1.CurrentRow.Index
Static Column As Integer = DGV1.CurrentCell.ColumnIndex
If Row = 0 And UpDown Then Exit Sub
If Row = DGV1.RowCount - 1 And Not UpDown Then Exit Sub
If UpDown Then
DGV1.CurrentCell = DGV1(Column, Row - 1)
Else
DGV1.CurrentCell = DGV1(Column, Row + 1)
End If
End Sub

Regards,

S_DS

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
Could you explain what you mean by the col and row properties - a datagridview does not have properties called col and row.
Dave299 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
Setting those two variables to static doesn't work either I still get null out of range exceptions. The rowcount still goes to zero when it is out of focus.
CoderD at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
Sorry I am talking about the datagridview rowcount, and currentcell.rowindex and the column index properties . I fixed the index properties by declaring them as shared member variables. I tried using selectedcells and that didn't work for me either. But the rowcount is the problem now.
CoderD at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8

Sorry still confused.

The RowCount is the number of rows in your datagrid - so are you saying that they all disappear.

The currentcell rowindex and columnindex are readonly properties of the grid whose values change as the user navigates round the grid. You cannot fix them or declare them as shared member variables.

I think it would help if you were to post some code so we can see what you are doing.

Dave299 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9
Hi when I debugging the properties on the datagrid are not available when the form is out of focus. When the datagrid form is in focus the properties are restored. The shared variables thing I was talking about was that I stored the values of the rowindex and columnindex properties on my double click event. So here are some snipplets of what I am doing.

Datagrid form:

Private Sub dgvDatagrid_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDatagrid.CellDoubleClick

Dim strSubID As String = dgvViewSubscriber.SelectedCells.Item(1).Value.ToString
m_intGridRow = dgvViewSubscriber.CurrentCell.RowIndex
m_intGridCol = dgvViewSubscriber.CurrentCell.ColumnIndex
...
...
...
Dim detailsForm As new DetailsForm
detailsForm.show()
....
...

End Sub

Here is the function as per your suggestion.

Public Sub ChangeCursor(ByVal boolUp As Boolean)

If m_intGridRow = 0 And boolUp Then Exit Sub
If m_intGridRow = dgvDatagrid.RowCount - 1 And Not boolUp Then Exit Sub

If boolUp Then
dgvDatagrid.CurrentCell = dgvDatagrid(m_intGridCol, m_intGridRow - 1)
Else
dgvDatagrid.CurrentCell = dgvDatagrid(m_intGridCol, m_intGridRow + 1)
End If

End Sub

Details Form:

Private Sub NextClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

....

Dim frmGrid As DatagridForm
frmGrid.GetForm()
' My code to get the current opened form. I also tried without the function as well. It is used to allow single instance of the form.

frmGrid.ChangeCursor(false)
....
End Sub

So this throws a ArgumentOutOfRange Exception. While debugging the properties for the datagrid are 0. However when the grid form is in focus the properties such as rowcount are back to 100 which they should be. I hope this gives some sort of idea of what I am trying to do.


CoderD at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10

OK - I can see a couple of problems there.

1. You are storing the currentcell row and column index in m_intGridRow and m_intGridRow. However when you set them in the cell double click event you are setting them to the values of a different datagrid (dgvViewSelection). So the first change would be to change those two lines to:

m_intGridRow = dgvDataGrid.CurrentCell.RowIndex
m_intGridCol = dgvDataGrid.CurrentCell.ColumnIndex

2. However it still won't work because you are not updating these values when the selection changes. So you need to handle the CurrentCellChanged event as well so that they are kept up to date. e.g.

Private Sub dgvDataGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvDataGrid.CurrentCellChanged
m_intGridRow = dgvDataGrid.CurrentCell.RowIndex
m_intGridCol = dgvDataGrid.CurrentCell.ColumnIndex
End Sub

In fact once you have that event handled you don't need to set the values in the double click event. Having done this it works ok for me.

Dave299 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 11
Thanks I'll give it a try. the data grids are the same name I guess I just forgot to change those variable names when I pasted it in. I was trying to make it a bit clearer.
CoderD at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 12

I tried using the currentcellchanged event and it seems that event gets raised when the grid loads and when I try to set m_intGridRow and m_intgridCol I get a nullReferenceException for the dvgDataGrid object. {"Object reference not set to an instance of an object."}

Thanks for all the help and bearing with me.

CoderD at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 13

The currentcellchanged event is only raised when the current cell is changed. Setting the m_intGridRow and m_intgridCol values in the currentcellchanged event cannot produce the exception you are experiencing.

Are you trying to set these values somewhere else, such as the Form load event - that will produce the exception your are seeing. I cannot think of any reason why you would need to set these values other than in the cellchanged event so try removing the code that is setting them elsewhere and see if that solves the problem. If not then please post all the code where these values are being set.

Dave299 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 14
I am using the Load event. I am not setting any of the values for the grid though. In my load event however I am calling my LoadGrid function which is the function I use that executes a stored procedure and binds the records to the datagrid. Could that be it? I don't set any of the grid variables though except for the DataSource. I would post code but I am not sure what I would that would be helpful.
CoderD at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...