How to set the value of a DataGridView's EditingControl - vb.net

I need to set the text value of a DataGridView.EditingControl. I've tried
myDGV.EditingControl.Text() = "1/1/2001"
and
myDGV.EditingControl.Text = "1/1/2001"
and
myDGV.EditingControl.Text("1/1/2001")
which causes an InvalidCastException (didn't check that could only be an integer).
There is no Value() property for the control, so how do I set the the value?
(Yes, I've verified that the cell is in edit mode)

You need to handle the EditingControlShowing event.
Private Sub HandleDgvEditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles Dgv.EditingControlShowing
If (TypeOf e.Control Is DataGridViewTextBoxEditingControl) Then
With DirectCast(e.Control, DataGridViewTextBoxEditingControl)
.Text = "1/1/2001"
End With
End If
End Sub
Note that this event will be fired for all editable cells. If you only want to manipulate the edit control for a given column you need to add another condition. Something like this:
If (Me.Dgv.CurrentCell.OwningColumn is Me.DgvFooColumn) Then
If you need to revert the edit from the CellValidating event then store the original value when the edit control is shown.
Private Sub HandleDgvEditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles Dgv.EditingControlShowing
'Cache the edit control text
Me.cachedEditText = e.Control.Text
End Sub
Private Sub HandleDgvCellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles Dgv.CellValidating
'Ensure that the edit control exists
If (Not Me.Dgv.EditingControl Is Nothing) Then
'Validate the edit
If (Not valid) Then
Me.Dgv.EditingControl.Text = Me.cachedEditText
End If
End If
End Sub
Private cachedEditText As String

Related

KeyPress handler being applied to wrong control

I have a .KeyPress event handler which is supposed to limit/control the keys which can be entered in a specific TextBox (more precisely, any of the textboxes in a specific DataGridViewTextBoxColumn)
Private Sub dgv_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
If dgv.CurrentCell.ColumnIndex = myColumn.Index And Not e.Control Is Nothing Then
DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
DirectCast(e.Control, TextBox).MaxLength = 10
AddHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf controlKeyPress
End If
End Sub
Private Sub controlKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim charEnum As Integer = CUInt(Microsoft.VisualBasic.Asc(e.KeyChar))
Dim tb As TextBox = DirectCast(sender, TextBox)
Select Case charEnum
Case 8
' Always permit the keying of backspace (no suppression)
Case 42
' Permit the keying of asterisk (42) but only if it is the first character (otherwise, suppress the key press)
If Not tb.SelectionStart = 0 Then e.KeyChar = ""
Case 46
' Permit the keying of period (46) but only if it is not the first character and the first character is not an asterisk (otherwise, suppress the key press)
If tb.SelectionStart = 0 OrElse tb.Text.FirstOrDefault = "*" Then e.KeyChar = ""
Case 65 To 90, 97 To 122
' Permit the keying of upper-case alpha (65-90) and lower-case alpha (97-122) as long as the first character is not an asterisk (otherwise, suppress the key press)
If tb.Text.FirstOrDefault = "*" Then e.KeyChar = ""
Case Else
' All other characters, suppress the key press (set the KeyChar to nothing)
e.KeyChar = ""
End Select
End Sub
What's weird is, the same handler seems to be getting applied to other TextBox controls in the DataGridView, but in a different column (i.e. not in myColumn) Which is strange because I have a specific condition in the EditingControlShowing event that specifies that the handler should only be applied if the .ColumnIndex of the control matches that of the column to which it should apply (i.e. If dgv.CurrentCell.ColumnIndex = myColumn.Index) So I'm not sure why the same handler is being applied to a TextBox that's not in myColumn?
Also, it doesn't appear to be consistent - when I initially load the DGV, the other textboxes have no restrictions on them (as expected); when I go to edit a row, and the handler is applied to myColumn (as expected), the same handler also seems to be applied immediately to any other textboxes in the same row (but in debugging, I can't seem to trap where this happens, I can only trap the application of the event handler to the correct control)
I'm not sure if I should have a RemoveHandler call somewhere - and if so, where, because I can't find the point at which the handler is being applied erroneously in the first place?
I tried this but it doesn't seem to have any effect (again, while debugging, when I click in a TextBox in myOtherColumn, it does hit that line, but the restriction is still imposed anyway?)
Private Sub dgv_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
If dgv.CurrentCell.ColumnIndex = myColumn.Index Then
DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
DirectCast(e.Control, TextBox).MaxLength = 10
AddHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf controlKeyPress
ElseIf dgv.CurrentCell.ColumnIndex = myOtherColumn.Index Then
RemoveHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf controlKeyPress
End If
End Sub
All suggestions welcome!
The DataGridView control will reuse an editing control if it can to improve performance. You should keep a reference to the editing control from the EditingControlShowing event handler and use a RemoveHandler statement in the CellEndEdit event handler.
Actually, you may not need to keep the reference. You may be able to use the EditingControl property of the grid. Try that first.
EDIT:
I have just tested for myself and the EditingControl property of the grid is Nothing when the CellEndEdit event is raised, so my second suggestion above is out. That means that you need to retain a reference to the editing control from the EditingControlShowing event handler. If you're going to do that though, you may as well not use AddHandler and RemoveHandler. It's simpler to declare the field WithEvents and then use a Handles clause on the event handler, e.g.
Private WithEvents editingControl As TextBox
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 0 Then
editingControl = DirectCast(e.Control, TextBox)
End If
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
editingControl = Nothing
End Sub
Private Sub EditingControl_KeyPress(sender As Object, e As KeyPressEventArgs) Handles editingControl.KeyPress
Console.WriteLine(e.KeyChar)
End Sub
That code will assign the editing control to the field if and only if the cell being edited is in the first column. Any control assigned to that field will have its events handled and the field is always reset when an editing session ends.

Changing typing behavior of ListBox

When typing into a listbox the listbox scrolls to the next item with first letter matching the input character. I want to disable/change this behavior.
I implemented a handler for the keypress event and implemented my new behavior, but I don't see any way to remove the old behavior.
How can I disable the default behavior?
Here is the event handler for the keypress event:
Private Sub FooListBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles FooListBox.KeyPress
If SearchResetTimer.Enabled Then
SearchResetTimer.Stop()
SearchTimer.Stop()
Else
BeginUpdate()
SearchedString = ""
End If
SearchedString = SearchedString & e.KeyChar
SearchResetTimer.Start()
SearchTimer.Start()
End Sub
Private Function SearchFor(listbox As ListBox, target As String) As Integer
For i As Integer = listbox.SelectedIndex To listbox.Items.Count - 1
If listbox.Items(i).ToString().ToLower().StartsWith(target) Then
Return i
End If
Next
For i As Integer = 0 To listbox.SelectedIndex - 1
If listbox.Items(i).ToString().ToLower().StartsWith(target) Then
Return i
End If
Next
Return -1
End Function
Private Sub SearchTimer_Tick(sender As Object, e As EventArgs) Handles SearchTimer.Tick
SearchTimer.Stop()
Dim Found As Integer = SearchFor(FooListBox, SearchedString)
If Found <> -1 Then
FooListBox.SelectedIndex = Found
End If
EndUpdate()
End Sub
Private Sub SearchResetTimer_Tick(sender As Object, e As EventArgs) Handles SearchResetTimer.Tick
SearchResetTimer.Stop()
End Sub
Quick overview: there are two timers, searchTimer that when ticked searches for the string and updates the control, searchResetTimer which resets the searched string and marks the beginning of a new user input.
Note that I am not releasing this code under CC, it is for illustration purposes only

Avoid some functions/events to run when one function/events is clicked or executed

im developing a system using vb.net and i have some question about datagridview function. Is it possible in gridview that when i click the rowheader function it will not execute the cell enter function of it?
Because my problem is in my cell enter event/function there is a code that will show some text box if the user enter on the 1st cell. and i want that when i click the row header of my grid view this cell enter event/function will not be executed. is it possible guys? give me some tips or tricks on how to do it.
i've also done trying like this
Private sub gridview_RowHeaderMouseClick . . .
textbox.visible = false
gridview.endEdit(true)
end sub
the result is okay but it is not good for the client view and i want to improve it more.
This is my code.
Private Sub dgCharges_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgCharges.CellEnter
if e.ColumnIdex >= 0 Then
if e.ColumnIndex = 5 Then
'Show Textbox.
End If
End if
End Sub
Private Sub dgCharges_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgCharges.RowHeaderMouseClick
'Textbox.visible = false //this line i've use to hide the textbox when user
clicked row header.
dgCharges.EndEdit(True)
End Sub
Just put a test in the CellEnter function that only executes the restricted part of the code if the column index >= 0. The header has index -1
Private Sub DataGridView1_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
If e.ColumnIndex >= 0 Then
'Do my stuff
End If
End Sub
------ EDIT ----
To prevent the datagridview cell editors from popping up just set the column to readonly. Alternatively for more control use
Private Sub DataGridView1_CellBeginEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridViewMsg.CellBeginEdit
'Test your criteria
If shouldBeReadonly Then
e.Cancel = True
End If
End Sub

How to make Datagridview Editable and Change it to Number Format?

Good Morning.
I have a Datagridview in a Form and it is connected in Database and it looks like this.
I have no problem with this part.
My question here is like this. How can I make the Fourth Column Editable? I mean I can edit it by clicking this property
and now the output will be like this.
Now here is the real question, I will ask a question based on the flow that my system will do.
1.The 4th Column are the column that will become editable and the rest will be locked or uneditable
2.Lets say I will put 48 how can I make it 48.00 when i leave the cell? That kind of format with .00 at the end.
3.Unable to input Letters on the 4th column.
TYSM for future help
Set the ReadOnly property to True for any column that you don't
want the user to be able to edit.
Set the DefaultCellStyle.Format property of the column to "n2" or "f2".
I'd probably advise against that because the fact that you want to include a decimal point makes it more complex. If you're determined to go ahead then you should research how to allow only numeric input in a regular TextBox, because this will work exactly the same way. You simply need to access the TextBox control used for editing via the appropriate event handlers of the grid.
In the example below, because the editingTextBox field is declared WithEvents, the last method will handle the TextChanged event for the editing control while it's assigned to that field for the duration of the editing session.
Private WithEvents editingTextBox As TextBox
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Me.editingTextBox = DirectCast(e.Control, TextBox)
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Me.editingTextBox = Nothing
End Sub
Private Sub editingTextBox_TextChanged(sender As Object, e As EventArgs) Handles editingTextBox.TextChanged
'...
End Sub
Set the other columns to readonly = true and your numeric column = false and set the defaultcellstyle.format of your numeric column to "###,##0.00" and in your datagridview's cellvalidating event, do the ff:
Private Sub DatagridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DatagridView1.CellValidating
Try
If DatagridView1.IsCurrentCellDirty Then
Select Case DatagridView1.Columns(e.ColumnIndex).Name.ToUpper
Case "<NAME OF YOUR NUMERIC COLUMN>"
If Not IsNumeric(e.FormattedValue) Then
MsgBox("Invalid value.")
e.Cancel = True
Exit Sub
End If
If CType(e.FormattedValue, Integer) < 0 Then
MsgBox("Invalid value.")
e.Cancel = True
Exit Sub
End If
End Select
End If
Catch ex As Exception
ErrMsg(ex)
End Try
End Sub
Private Sub dgvwithdraw_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvwithdraw.CellClick
Select Case dgvwithdraw.Columns(e.ColumnIndex).Name
Case "Select", "Alloted"
dgvwithdraw.ReadOnly = False
Case Else
dgvwithdraw.ReadOnly = True
End Select
End Sub

Button of UserControl vanishes when program is debugged

My UserControl button disappears when I debug my program. I have checked the code including the designer.vb code countless times there's nothing that makes the button .enabled = false or .visible = false. Any ideas why this is happening?
On my UserControl:
Private Sub btn_Begin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Begin.Click
Start_Race()
End Sub
Public Sub Start_Race()
TimeNow(Past_Time)
TimeNow(Start_Time)
lbl_Start_Time_Driver.Text = Past_Time
btn_Begin.BackColor = Color.Green
btn_Begin.Text = "Started!"
End Sub
Public Property Active_bool As Boolean
Get
Return btn_Begin.Visible
End Get
Set(ByVal value As Boolean)
btn_Begin.Visible = value
End Set
End Property
On Form1:
Private Sub btn_Start_All_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Start_All.Click
Dim allActiveUserControls = From uc_Index In Controls.OfType(Of LapTimerGUI)()
Where uc_Index.Active_bool
For Each User_Control In allActiveUserControls
User_Control.Start_Race()
Next
End Sub
I do Google my head off before I post my ridiculous questions here btw :)
This is strange. Does any MsgBoxes pop up if you add this code to your UserControl:
Private Sub UserControl_ControlRemoved(sender As Object, e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlRemoved
MsgBox("Control Removed!")
End Sub
Private Sub Button2_EnabledChanged(sender As Object, e As System.EventArgs) Handles Button2.EnabledChanged
MsgBox("EnabledChanged!")
End Sub
If so, then you can add a breakpoint to these MsgBoxes and lock at the CallStack (CTRL+L) from where it triggers.
Btw: If the control is removed somehow, .PerformClick() still triggers (for me). Thus I bet, that the control is somehow disabled (Enabled = False).
Lastly, if any container of the button (such as your UserControl) is disabled, the button will be disabled too,
After lots of playing around I finally found the problem!
The value was set to =False in my properties. I'm so blonde! Thanks guys for the help ^_^/
Public Property Active_bool As Boolean
Get
Return btn_Begin.Visible
End Get
Set(ByVal value As Boolean)
btn_Begin.Visible = value
End Set
End Property
Although, Something sets the values to =False ever now and then. Very annoying :3
And I can not set the value to =True in the properties... Only in the hidden designer code...