How to add datetimepicker control in datagridview - vb.net-2010

I am doing my college project ,i have some problem in my project.
how to add datetimepicker control in datagridview vb.net,,i try so many option but its not working ..pls tell any solution.
coding for vb.net
Regards,
Prasanth.s
8098077937

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
'//Adding DateTimePicker control into DataGridView
DataGridView1.Controls.Add(oDTP)
'// Setting the format (i.e. 02/07/2017 - mm/dd/yyyy)
oDTP.Format = DateTimePickerFormat.Short
'// It returns the retangular area that represents the Display area for a cell
Dim oRectangle = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
'//Setting area for DateTimePicker Control
oDTP.Size = New Size(oRectangle.Width, oRectangle.Height)
'// Setting Location
oDTP.Location = New Point(oRectangle.X, oRectangle.Y)
'// Read value from DataGridView into DateTimePicker
oDTP.Value = DataGridView1.CurrentCell.Value
'// Now make it visible
oDTP.Visible = True
'// Force to change date value at oDTP_ValueChanged Event.
AddHandler oDTP.ValueChanged, AddressOf oDTP_ValueChanged
End Sub
Private Sub oDTP_ValueChanged(sender As Object, e As System.EventArgs)
DataGridView1.CurrentCell.Value = oDTP.Value
End Sub
Private Sub DataGridView1_CellLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
oDTP.Visible = False
End Sub

Related

Detect multiple control changed in VB.Net

how can I control some controls that will be created automatically during the execution of the application?
My add control
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim NewCheckbox As CheckBox
For i As Integer = 2 To 14
NewCheckbox = New CheckBox
NewCheckbox.Size = New Drawing.Size(15, 14)
NewCheckbox.Location = New Point(98, 40)
NewCheckbox.Name = "cbcard" & i
Me.Controls.Add(NewCheckbox)
next
My checkbox control add, I gave an example to create a checkbox control, but it should be valid for any form of control, be it textbox or button.
how can i detect these controls that are not created but will be created during execution?
checkbox name will be cbcard1, cbcard2, cbcard3 up to 14.
but I want this to be in the form of a handler, or it should probably work with a timer.
Private Sub Checkbox_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Checkbox.Changed
If Checkbox.checked = true
MsgBox("")
End If
End Sub
You can subscribe to the event using the AddHandler syntax.
For i As Integer = 2 To 14
NewCheckbox = New CheckBox
NewCheckbox.Size = New Drawing.Size(15, 14)
NewCheckbox.Location = New Point(98, 40)
NewCheckbox.Name = "cbcard" & i
Me.Controls.Add(NewCheckbox)
'subscribe to the CheckChanged event (as an example)
AddHandler NewCheckbox.CheckChanged, AddressOf Checkbox_Changed
Next
'all dynamically created checkboxes were subscribed to this event
Private Sub Checkbox_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs)
'sender is the specific checkbox that was changed out of all subscribed checkboxes
Dim checkBox = DirectCast(sender, CheckBox)
If checkbox.checked = true Then MsgBox(checkBox.Name)
End Sub

How to trigger a gotfocus datagrid combobox event while grid is readonly?

I have a datagridview and I'm trying to get the gotfocus event for my comboboxcells to still call despite the datagrid being on readonly.
This gotfocus event changes the colour of every combobox with a similar value (including itself) and I would like to it to still call while on readonly (the datagridview comboboxes can still be selected/highlighted while on readonly but it is not called(?))
Below is the basics of how I am currently handling the events (for some reason this shortened version makes multiple event calls at an exponential rate & also requires three clicks to trigger... I'm not sure why it does this but my full project works fine in this regard)
Public Class Form1
Private Sub cell_gotfocus(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Dim CurrentCombo = TryCast(e.Control, ComboBox)
If Not CurrentCombo Is Nothing Then
RemoveHandler CurrentCombo.GotFocus, AddressOf Combo_Focus 'remove event handler so we don't accumulate multiples
AddHandler CurrentCombo.GotFocus, AddressOf Combo_Focus 'add eventhandler
End If
End Sub
Private Sub Combo_Focus()
Console.WriteLine("Got Focus")
Console.beep
End Sub
Private Sub Make_Readonly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Make_Readonly.Click
DataGridView1.Rows(0).ReadOnly = True 'toggle readonly
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For y As Integer = 1 To 4 'add extra columns into datagrid
Dim cmb As New DataGridViewComboBoxColumn
cmb.HeaderText = "c" & DataGridView1.Columns.Count
cmb.Name = "c" & DataGridView1.Columns.Count
cmb.FlatStyle = FlatStyle.Flat
DataGridView1.Columns.Add(cmb)
Next
End Sub
End Class

Exposing DataGridViewComboBoxEditingControl

I would like to know how to use the DataGridViewComboBoxEditingControl with vb.net
i need a routteen to run when the user select an item from the datagridviewcomboboxcolumn that i have configured. I am unsure how to attach the object to the column i create manually
I have implemented the following from examples on the internet but this only appears to trigger when the user click on the combobox within the column.
Private Sub dgvTicketDetail_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvTicketDetail.EditingControlShowing
Dim editingComboBox As ComboBox = TryCast(e.Control, ComboBox)
If editingComboBox IsNot Nothing Then
AddHandler editingComboBox.SelectedValueChanged, AddressOf EditingComboBox_DropDown
End If
End Sub
Private Sub EditingComboBox_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.WriteLine("A ComboBox in the DataGridView just dropped down.")
End Sub
any help would be appreciated as i cant seem to find much reference material for this
Thanks in advance
Just realised that i have never posted the answer to my question
Placed the following in the EditingControlShowing to capture request
Private Sub dgvTicketDetail_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvTicketDetail.EditingControlShowing
Try
If dgvTicketDetail.CurrentCell.ColumnIndex = 1 Then
Dim cmbox As ComboBox = TryCast(e.Control, ComboBox)
AddHandler cmbox.SelectionChangeCommitted, AddressOf Update_StockInfo
strSelectedText = cmbox.SelectedText
End If
Catch ex As Exception
End Try
End Sub
then this added items to the combobox cell
Private Sub Update_StockInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim cmbClickedCell = DirectCast(sender, DataGridViewComboBoxEditingControl)
Dim cmbComboBox = DirectCast(sender, ComboBox)
If dgvTicketDetail.CurrentRow.Index = cmbClickedCell.EditingControlRowIndex And dgvTicketDetail.CurrentCell.ColumnIndex = 1 Then
Debug.WriteLine(cmbClickedCell.EditingControlRowIndex & cmbComboBox.SelectedValue)
Dim dtStock As DataTable = CropTrackMod.GetWeight(cmbComboBox.SelectedValue)
Dim dgvcc As DataGridViewComboBoxCell
dgvcc = dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells(2)
dgvcc.Items.Clear()
For Each row As DataRow In dtStock.Rows
dgvcc.Items.Add(row.Item("UnitName"))
Next row
If CropTrackMod.IsStockVATAble(cmbComboBox.SelectedValue) = True Then
dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells("VATRate").Value = CropTrackMod.dblVATRate
Else
dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells("VATRate").Value = "0.00"
End If
End If
End Sub
It works really well for me, unfortunately the user wanted something different in the end ha. Oh well at least i learnt something

Winforms ListBox Control Not Updating After Source Changes

I have a ListBox (LB) with a DataTable (DT) DataSource in the Form Class globally populated in the Form_Load event.
Private Sub frmEditPresets_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DT.Columns.Add("DisplayText")
DT.Columns.Add("PresetID")
For Each TSI As ToolStripItem In Presets.DropDownItems
If TSI.Name.IndexOf("preset_") > -1 Then
DT.Rows.Add(TSI.Text, TSI.Name)
End If
Next
LB.DataSource = DT
LB.DisplayMember = "DisplayText"
End Sub
When I use my Rename button. It updates the menu item and the Data Source but the Listbox doesn't refresh until I click another item in the listbox.
Rename code:
Private Sub btnRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRename.Click
Dim R As DataRowView = LB.SelectedItem
Dim S As String = InputBox("Preset Name", "Rename", R("DisplayText"))
If S.Trim.Length = 0 Then Exit Sub
If Presets.DropDownItems.ContainsKey(R("PresetID").ToString) Then
Presets.DropDownItems(R("PresetID").ToString).Text = S
End If
R("DisplayText") = S
End Sub
I'm sure this is a simple question with a simple answer but I can't seem to figure it out. I've tried Refresh(). I've tried setting the DataSource again. I read this StackOverflow question Winforms listbox not updating when bound data changes but ResetBindings() doesn't seem to be an available method in this context.
*Edit. I gave Steve credit for the answer as he mentioned BindingContext. Although, that led me to find BindingContext(DT).EndCurrentEdit() which updated my LB display and maintained the selection.
Tried with this, and it works.....
Private Sub btnRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRename.Click
Dim R As DataRowView = LB.SelectedItem
Dim S As String = InputBox("Preset Name", "Rename", R("DisplayText"))
If S.Trim.Length = 0 Then Exit Sub
If Presets.DropDownItems.ContainsKey(R("PresetID").ToString) Then
Presets.DropDownItems(R("PresetID").ToString).Text = S
End If
R("DisplayText") = S
BindingContext(DT).EndCurrentEdit()
End Sub

How to check focused TextBox in vb.net winforms?

I have multiple textbox in a form. How do I know what textbox the cursor currently is?
Trying to do something like this:
If TextBox2.Focus() = True Then
MessageBox.Show("its in two")
ElseIf TextBox3.Focus = True Then
MessageBox.Show("its in three")
End If
But I think its not working.
TextBox.Focus actually assigns the focus to the given textbox. What you're looking for is TextBox.Focused. :)
In fact, all form controls have the Focused property.
I know this already has an accepted answer but I just think this method is a bit easier and should be up here for people who find this through Google or whatever.
Public focussedTextBox As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() focussedTextBox = textBox
End If
Next
End Sub
This way you can then just refer to the focussedTextBox at any time. You should make sure that you check that there is a focussedTextBox before you do however becuase when the application first loads there will not be. You can do this using:
If Not focussedTextBox Is Nothing Then
...
End If
Alternatively, you could set focussedTextBox to a TextBox of your choice on form load, either by setting its value or by focussing the TextBox.
Obviously, it will not work if you are calling your code in a Button_Click because when you click the Button then the focus is itself goes to the Button which you have clicked.
You can do two things:
Make a combined Focus event for all TextBoxes and check its Sender object.
Private Sub TextBox_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter
Dim currTextBox As TextBox = sender
If currTextBox.Equals(TextBox2) Then
MessageBox.Show("it's in two")
ElseIf currTextBox.Equals(TextBox3) Then
MessageBox.Show("it's in three")
End If
End Sub
OR
Take a global string variable, and set its value at each TextBox_Focus event, then check string value in the button click event.
Dim str As String
Private Sub TextBox2_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
str = "two"
End Sub
Private Sub TextBox3_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter
str = "three"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("it's in " & str)
End Sub