Scroll Bar Issues Automatically Scrolling Down To The Last Control - visual-studio-2010

Alright, I designed a form to fit onto the current screen size I use, with the form having a vertical scroll bar to view items further down on the form.
At the bottom of the form, I have a couple of checkboxes a user has to select before clicking the submit button.
Once the user hits the submit buttom, the user can't scroll back up to the beginning of the form. The user can scroll back up, but when they stop scroll, it scrolls to the bottom where the last checkbox was checked off.
I assume by checking this last checkbox is that's setting the focus of that control?
Any suggestions on how to fix the scrolling issue?

Keep a note of the last scroll position, and reapply it on Form_Activate
Try this:
'' Declare at form level
Private LastAutoScrollPos As System.Drawing.Point
Private Sub Form1_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
Me.AutoScrollPosition = LastAutoScrollPos
End Sub
Private Sub Form1_Scroll(sender As Object, e As System.Windows.Forms.ScrollEventArgs) Handles Me.Scroll
If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then
LastAutoScrollPos = New Point(LastAutoScrollPos.X, e.NewValue)
ElseIf e.ScrollOrientation = ScrollOrientation.HorizontalScroll Then
LastAutoScrollPos = New Point(e.NewValue, LastAutoScrollPos.Y)
End If
End Sub

Related

Programatically click a row in a listbox

When I click the a command button to close the form, the code requery's a cbo and listbox, moves the focus to the lstbox and a particular row. What I need to do is add some code that "clicks" the current row in the listbox that has the focus.
Here is the code I have on the click event.
If Forms![frmmain]![txtHidden] = "addok" Then
Forms![frmmain]![cboAuthor].Requery
Forms![frmmain]![LstAuthor].Requery
Forms![frmmain]![LstAuthor] = Me.AuthorID
Forms![frmmain]![txtHidden] = "AddDone"
DoCmd.Close acForm, "frmAddAuthorFly"
Forms![frmmain]![cboAuthor].Requery
Forms![frmmain]![LstAuthor].Requery
Forms![frmmain].[LstAuthor].SetFocus
<NEED TO INSERT SOMETHING HERE TO CLICK THE ROW"
Else
MsgBox "txt hidden is not addok"
DoCmd.Close acForm, "frmaddauthorfly"
End If
A click does nothing by itself, but you may have some OnClick code that runs. To do so, just call the OnClick event.
But all this requiring - and indeed "clicking" - shouldn't be needed; it seems as if you need to rethink your concept.

Disable MDI child Form buttons

Is there possible to disable child's form buttons from parent form?
For example, I have 2 radio-buttons in parent form, one is True second False, when I choose one of them fires radiobutton.CheckedChanged event and there I have code what goes like this, but it's not working:
ChildForm.Button1.Enabled = False
where seems to be the problem? Can anyone help with this?
You would need to create an instance of the childform.
So...
ChildForm cf = new ChildForm();
cf.Button1.Enabled = false;
You have to keep in mind though, it could be a different instance than the child form that is currently being shown.
To be sure, depending on your code (which I can't see) and how your program is laid out I would probably do something like this...
ChildForm cf = new ChildForm();
cf.show();
cf.Button1.Enabled = false;
so here I know that the ChildForm that is showing is the one that has the button disabled.
In VB6 the following project works:
1 MDI form:
Option Explicit
Private Sub MDIForm_Click()
Form1.Option1.Enabled = False
End Sub
Private Sub MDIForm_Load()
Form1.Show
End Sub
Form1 is a MDI child form with 2 radio buttons on it

Tabbing between radio buttons in VB6

I have a form which consists of six radio buttons within a frame which are mutually exclusive and one command button.
I already gave different tab-index to each radio button but at the run time by pressing tab focus skipped out of the radio buttons.
so how to give focus to another radio button by pressing TAB?
As others have said above this is intended behaviour. If you really wish to achieve this then the only way I can think to do this is place each radio button on a separate picture box (BorderStyle = None, TabStop = False). This will then work but you won't be able to use arrow keys to move between the radio buttons, only tabbing.
Private Sub Option1_KeyPress(KeyAscii As Integer)
If KeyAscii = 9 Then
Option2.SetFocus
End If
End Sub
KeyAscii=9 is the code for the Tab key. But you must do it for all of your radio buttons.
If you add your radio buttons belonging to the same radio button having indices 0, 1, 2 you can do it like this:
Private Sub Option1_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = 9 Then
If Index < Option1.Count - 1 Then
Option1(Index + 1).SetFocus
Else
Option1(0).SetFocus
End If
End If
End Sub

DataGridView: how to make scrollbar in sync with current cell selection?

I have a windows application with DataGridView as data presentation. Every 2 minutes, the grid will be refreshed with new data. In order to keep the scroll bar in sync with the new data added, I have to reset its ScrollBars:
dbv.Rows.Clear(); // clear rows
SCrollBars sc = dbv.ScrollBars;
dbv.ScrollBars = ScrollBars.None;
// continue to populate rows such as dbv.Rows.Add(obj);
dbv.ScrollBars = sc; // restore the scroll bar setting back
With above codes, the scroll bar reappears fine after data refresh. The problem is that the application requires to set certain cell as selected after the refresh:
dbv.CurrentCell = dbv[0, selectedRowIndex];
With above code, the cell is selected; however, the scroll bar's position does not reflect the position of the selected cell's row position. When I try to move the scroll bar after the refresh, the grid will jump to the first row.
It seems that the scroll bar position is set back to 0 after the reset. The code to set grid's CurrentCell does not cause the scroll bar to reposition to the correct place. There is no property or method to get or set scroll bar's value in DataGriadView, as far as I know.
I also tried to set the selected row to the top:
dbv.CurrentCell = dbv[0, selectedRowIndex];
dbv.FirstDisplayedScrollingRowIndex = selectedRowIndex;
The row will be set to the top, but the scroll bar's position is still out of sync. Not sure if there is any way to make scroll bar's position in sync with the selected row which is set in code?
I found an answer to resolve issue. As I mentioned that the control does not have methods or properties to set the correct scroll bar value. However, the scroll bar and the DatagridView content will display correct if there is an interaction directly towards to the UI such as touch the scroll bar or grid cell. It looks like that the control needs to be refocused and a repaint.
Simply use the following codes does not cause the scroll bar reset:
dgv.Select();
// or dbv.Focuse();
The way I found is that I have to make the DatagridView control disappear to back again. Fortunately, I have a tab control with several tabs. Then I switch the tab to get scroll bar reset:
int index = myTabCtrl.SelectedIndex;
if (index == (myTabCtrl.TabCount)) {
dgv.SeletecedIndex = 0;
}
else {
dgv.SelectedIndex = index + 1;
}
myTabCtrl.SelectedIndex = index;
If you don't have any way to hide the DatagridView on your form, you could simply minimize the form and then restore it back.
The problem is that there will be a fresh on the UI.
It seems, TAB, SHIFT+TAB, END keys don't always bring last column into the visible view.
The following code inside the CurrentCellChanged event handler seems to fix this issue (vb.net):
If Me.MyDataGridView.CurrentCell IsNot Nothing Then
Dim currentColumnIndex As Integer = e.MyDataGridView.CurrentCell.ColumnIndex
Dim entireRect As Rectangle = _
Me.MyDataGridView.GetColumnDisplayRectangle(currentColumnIndex, False)
Dim visiblePart As Rectangle = _
Me.MyDataGridView.GetColumnDisplayRectangle(currentColumnIndex, True)
If (visiblePart.Width < entireRect.Width) Or (entireRect.Width = 0) Then
Me.MyDataGridView.FirstDisplayedCell = Me.MyDataGridView.CurrentCell
'OR Me.MyDataGridView.FirstDisplayedScrollingColumnIndex = currentColumnIndex
End If
End If

How to display data in one form upon double-clicking an item in another form's listview

I have 2 forms (2 windows). In first window,I have a tree view (the children are the tables in DB.). On click of a any child, a list view (the data present in the table ) will be displayed on the right side of the tree view.
On double clicking on any row in the list view, another form will be opened. The current requirement is to display the data in the list-view in form2 opened on double clicking a single record.
Private Sub LV_DblClick()
Dim a As New Form2
Display_Temp_DATA
a.Show vbModal
End Sub
LV_DblClick() is in the form1 and it opens the form2. Now, Display_Temp_DATA has SQL query, which
fetches the record from the table and should be displayed in a list view in form2.
Private Sub Display_Temp_DATA()
On Error GoTo errHandler
'Dim liItem As ListItem
'Set liItem = a.ListView1.ListItems.Add(, , "Abcd")
Display_List_Two_Data "Select start, stop FROM tblsignal"
Exit Sub
errHandler:
MsgBox Err.Description
End Sub
I am able to open the form2, but I am finding difficult to display the data in the form2 list view. Any ideas on how to resolve this?
you can add module and variable to it and assign the value to that variable access it on the target form.
not sure but you can also use FormName.VariableName for static variable on the form.

Resources