Is it possible to display radio buttons without one being selected? - vb.net

I have two radio buttons in a group box. I don't want either of them selected, so the user is forced to make a choice. (I have code in place to ensure one has been selected before moving to the next form.)
I set the Checked property to False for both buttons, but when I run the form it still displays the top button as selected. I added to following code to the Load event of the form but it STILL shows one as selected.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RBArchive.Checked = False
RBCopy.Checked = False
End Sub
How can I get both radio buttons to remain unselected?

In the designer just set the default for each button = false. The other issue is that the Group cannot be the FIRST control on your form (tab index = 0). If the radio group is the first control, then put/place/move something else to hold the tab index 0 position.
If the grouping is the first control, then on form load the first of the group will be selected - even if all radio buttons are defaulted to false. However, if the group control is NOT the first control on the form, then you should not have to do anything other then just set the controls to false. This is for winforms, and not WPF.
In asp.net? then setting the group to -1 works rather well. So if this is WPF, then I would give setting the group control to -1 a try. In standard winforms, the group control does not spit out a index number (like it does for asp.net grouped radio buttons).
So, I tend to use this:
Private Function GetGroupBox(grpb As GroupBox) As Integer
' return seleted control - starts at 0
For Each ctrl As RadioButton In grpb.Controls
If ctrl.Checked Then Return ctrl.TabIndex
Next
Return -1 ' if none checked
End Function
And above assumes that the tabindex for all radiobuttons in the group is set to 1
So, in code, if there are 5 buttons, I can go:
If GetGroupBox(Me.gRadioGroupProvider) = 0 Then
strProvider = "Microsoft.ACE.OLEDB.12.0"
Else
strProvider = "Microsoft.Jet.OLEDB.4.0"
End If
So, now you get a group return value like you get in asp.net, VB6/VBA
Regardless, if all radio buttons have a default of false, and the radio group is not the first control on the form, then none should show as selected.
If this is WPF, then all bets are off.

Related

DataGridView resets to top when clicked after mouse scroll

I have a DataGridView control on a TabPage of a Windows Form application.
When the user moves the mouse over the DataGrid and uses the scroll wheel, the grid scrolls as expected. But when the user clicks in a cell on the screen, instead of the cell receiving focus, the DataGrid resets to the top and requires the user to scroll down again. This response is non-intuitive since it's not immediately obvious that the cell you thought you clicked on isn't there anymore.
I would be happy to prevent the DataGrid from responding to the scroll wheel until the user clicks in the grid, or preferably to maintain the current actions except not resetting to the top when first clicked.
From what I've researched here, it appears that the DataGrid is rebinding because I'm resetting the binding when the tabpage is entered (since the database might have been updated by one of the other tabs.
Private Sub TabPage1_Enter(sender As Object, e As EventArgs) Handles TabPage1.Enter
LoadTACTable()
End Sub
In LoadTACTable():
dbGetList("spSelectTACList", dtTACs, 0, 100000, Nothing) ' Record numbers are 0 based
bsTACs.DataSource = dtTACs
With gridTACs
' TOTAL Grid width = 1380
.DataSource() = bsTACs
.
.
.
(Showing only part of the code for brevity.
Is there a way to see if the TabPage is already displayed when entered? Or, is unnecessary to reset the gridTAC datasource every time I retrieve the data from the SQL database to the dtTACs datatable using my dbGetList() sub?
There are several possible solutions to your problem. One would be to not automatically rebind the datagrid but let the user do it by clicking some refresh button. That way the user would not see non-intuitive behavior.
You mentioned that the contents of one tab may need to be refreshed when the contents of other tabs are changed. Whenever the contents of a tab is changed and can affect other tabs, you could flag these other tabs (for example, by adding a star to their titles) to indicate that they no longer have the latest data. The user would then know that the tab needs to be refreshed.
There might be other solutions, but it is difficult to tell without knowing more about your use case.
With the guidance above, I believe I solved the issue:
I created a flag:
Dim TabDirty As Boolean
Then I set it in the TabPage.Leave handler:
Private Sub TabPage1_Leave(sender As Object, e As EventArgs) Handles TabPage1.Leave
dtTACs.Dispose()
TabDirty = True
End Sub
Then I just check it when I enter the TabPage:
Private Sub TabPage1_Enter(sender As Object, e As EventArgs) Handles TabPage1.Enter
If TabDirty = True Then
TabDirty = False
LoadTACTable()
End If
End Sub
So far, this appears to work - the grid is not getting reset when clicked, but I will do a bit more testing to confirm that the data is refreshed when necessary.

DataGridView: EditOnEnter selection mode allowing row deletion

By default, a DataGridView is set to EditOnKeystrokeOrF2 edit mode. This means two or three clicks (spaced further apart than the user's double click interval) are required to change the value of a combobox in this view. As this is rather strange for a UI object, you would tend to think the control doesn't work.
Fortunately, you can change the selection mode to EditOnEnter. This will immediately select a cell when clicking on it, not first select the row, reducing the amount of clicks by 1. However, DataGridViews are implemented somewhat strangely. There is a '-1th' cell that is not manually selectable in each row.
When this '-1th' cell is selected, in the normal selection mode the row is selected, but in the "EditOnEnter" mode the 1st cell in the row is selected instead. If the DataGridView is set to enable row deletion using the "Del" key, then using EditOnEnter makes it impossible to use this functionality.
How do I get both to work? I.e.: I don't have a view where the user can have click up to 6 times (users tend to click more rapidly when they have to do so a lot of times) to open a box, while at the same time allowing row selection using the special -1th column?
One would need to programmatically toggle between both edit modes when any cell in the row is clicked. However, the CellClick event fires too late: after the row is already selected. Naively just toggling the EditMode would mean that the first click on the row selection box doesn't work, while the second would, which would appear as buggy behaviour.
The trick is to do much more manually. The following event handler, when attached to the CellClick event, will resolve almost all issues.
Private Sub CellSelect(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles myDataGridView.CellClick
Dim dgv As DataGridView = CType(sender, DataGridView)
If dgv.Rows.Count = 0 Then
Return
End If
Dim rowToSelect As Integer = e.RowIndex
Dim columnToSelect As Integer = e.ColumnIndex
If e.RowIndex = -1 Then
rowToSelect = 0
End If
If rowToSelect >= dgv.Rows.Count Then
rowToSelect = 0
End If
If columnToSelect = -1 Then
dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgv.CurrentCell = Nothing
dgv.Rows(rowToSelect).Selected = True
Else
If columnToSelect >= dgv.Rows(rowToSelect).Cells.Count Then
columnToSelect = 0
End If
dgv.EditMode = DataGridViewEditMode.EditOnEnter
dgv.SelectionMode = DataGridViewSelectionMode.CellSelect
dgv.Rows(rowToSelect).Cells(columnToSelect).Selected = True
End If
End Sub
It works by unsetting the selected cell, then setting the selected row programmatically. As the EditMode was changed beforehand, it will select the entire row, not just the first cell, even the first time that the row-selection box is clicked.
There are also a whole bunch of edge cases where a user that clicks quickly enough can create click events on cells that do not exist. So we assume those clicks are on the cell [0,0] so at least our application won't blow up.
This isn't a perfect solution (yet). With this solution: A small graphical glitch remains; for about one frame the DataGridView will flicker between edit modes, very briefly appearing as though the row is selected.

option button in selected mode in vba access 2010

I have a check box in a access form and i want when this check box selected , a option button in my form becomes selected.
Sorry, I know this is a amateur question but i need an answer. I used this but it doesn't work :
If (Me.Check86 = True) Then
Option107.OptionValue = 1
Else
Option110.OptionValue = 0
End If
Use the value property instead.
OptionValue is used when several option buttons are grouped together. It allows you to determine which of the option buttons has been selected.
Example
Private Sub Check86_Click()
' Update option buttons based on value of checkbox.
Option107.Value = Me.Check86.Value ' Sync check box and option.
Option110.Value = Not Option107.Value ' Ensures only one option button is selected at a time.
End Sub
This event is fired each time the check box is checked/unchecked. It checks/unchecks Option107 to match. It then sets Option110 to the reverse setting. I'm assuming you only want one option button checked at a time.
I've used the not operator to ensure Option110 and Check86 hold different values. When Check86 is true Option110 is not true, ie false.

Make a button have multiple uses

okay... How do I explain this without being totally confusing?... Alright, I have this form that has MenuScripts (top-levels and second-levels). The problem that I am having is one of the second-levels is "Add" which brings you to another form when clicked. This other form has a button ("Record") and text boxes. This other form allows the user to input data and when the record button is clicked, the inputted data is written into a text file. Ok, so back to the first form. Another second-level MenuScript is "Update" which also brings the user to the other form; but first, the user has to click an item within a listbox to proceed. How do I get the data from the selected item to appear in the appropriate textboxes and how do I get the record button to update data instead of being confused and thinking it is only a add-data button?
Is there a way to use an "if" statement to say something like "if mnuAdd is clicked then" "elseif mnuUpdate is clicked then". Would something like that work for giving the record button multiple uses?
Also, if someone can give me some pointers on making sure the user selects an item within the listbox would definitely be a plus! Thanks, guys!
Unfortunately, I cannot add images since my reputation is too low.
Here is a visual representation of my ultimate goal
Easiest way: before displaying the second form set it's Tag property to something distinct – say "Add" or "Update" – depending on which menu item is selected. Then you just test the Tag value in the button's Click event and proceed accordingly.
As for determining whether a list item is selected: well if there isn't the ListBox's SelectedIndex property will be set to -1.
You need to put a public property on the second form (Details) which specifies which mode it is in. For instance, you could create a mode enumeration like this:
Public Enum EntryModes
AddBook
UpdateBook
End Enum
Then, define a public mode property on the second form, like this:
Public Property EntryMode As EntryModes
Get
Return _entryMode
End Get
Set(ByVal value As EntryMode)
_entryMode = value
End Set
End Property
Private _entryMode As EntryMode
Then, when you show the second form from the menu, just set the property first, before showing it:
Private Sub mnuAdd_Click(sender As Object, e As EventArgs)
Dim dialog As New DetailsDialog()
dialog.EntryMode = EntryModes.AddBook
dialog.ShowDialog()
End Sub
Private Sub mnuUpdate_Click(sender As Object, e As EventArgs)
Dim dialog As New DetailsDialog()
dialog.EntryMode = EntryModes.UpdateBook
dialog.BookToUpdate = ListBox1.SelectedItem
dialog.ShowDialog()
End Sub
As you can see, in the Upate menu click, I also added a line that passes the information for which book should be updated.

Clean way to display/hide a bunch of buttons based on a ComboBox selection

I'm writing a standalone application in VB.NET using Visual Studio 2005.
I want to display/hide a bunch of Buttons based on the selected value of a ComboBox. Each selection would have a different set of Buttons to display, and I'd like to have them arranged in a nice grid.
Driving a TabControl with the ComboBox value would be the kind of behavior I want, but I don't want it to look like a TabControl to the user because it might be confusing.
Is there a way to do this?
Basically, I'd like Selection1 of the ComboBox to show Buttons 1-4, Selection2 to show Buttons 5-11, Selection3 to show (maybe) Buttons 1, 3, 5, 6, and 8, etc., have them arranged nicely, and have the GUI show only the ComboBox and the buttons.
Thanks in advance as always!
Use a Panel control (or multiple if the items aren't grouped right next to each other) and set the visibility accordingly.
(Added)
You CAN stack panels on top of each other, so that the buttons all look like they're in the same location. but it becomes a maintenance nightmare and I don't recommend it.,
Hack warning - the following is a hack, but it works.
Another option is to use a tab control, but hide the tab buttons. (You can do this by positioning a panel over the buttons, but you have to be careful of letting the user resize the form.) Then you set the TabIndex based on the drop-down changing.
Edit again - added per comment
If you use the hack, you can add this into the ComboBox's selected index changed event....
(code may be wrong, as I'm not at my dev pc and can't check, but you get the idea)
TabControl1.SelectedIndex = ComboBox1.SelectedIndex
You could put all of your buttons on a panel on your form. Then when the SelectedIndex event of the combobox fires, you can loop through the buttons on the panel and turn them on and off based on their Tag property.
For this example you would set the Tag property of each button equal to the combobox index or indexes that you want it to turn on for. If you want it visible for more than one combo selection just comma seperate the index values in the tag property.
You don't have to key off of the combobox index. You could use the selected text for example. If you did that, just put the texts to show the button for in the tag property and change the code from ComboBox1.SelectedIndex.ToString to ComboBox1.SelectedText.
The buttons will turn on and off where they are placed at design time, but you could add some code here to arrange them dynamically as well so that all the visible buttons are neatly arranged.
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
For Each ctrl As Control In Me.Panel1.Controls
If TypeOf ctrl Is Button Then
If Array.IndexOf(Split(ctrl.Tag, ","), ComboBox1.SelectedIndex.ToString) > -1 Then
ctrl.Visible = True
Else
ctrl.Visible = False
End If
End If
Next
End Sub
Maybe using a FlowLayoutPanel will help you display the buttons.
You can use a jagged array to define which buttons belong to which combo-box item.