Dynamically change a Control's ContextMenuStrip property at runtime? - vb.net

I have a DataGridView that I want to use one ContextMenuStrip if a cell is right-clicked and a different ContextMenuStrip if a Cell Header is clicked. I DO NOT want to just add or remove or make visible or invisible members of the "same" CMS. I thought that it would be as easy as changing the DataGridView's property on a right-click, but I must be mistaken.
Private Sub DgvItems_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DgvItems.CellClick
Dim dgv As DataGridView = DgvItems
If MouseButtons.Right Then
Select Case md.HitTestInfo.Type
Case DataGridViewHitTestType.ColumnHeader
dgv.ContextMenuStrip = CmsDgvItemsColHdrs
dgv.ContextMenuStrip.Show()
Case DataGridViewHitTestType.Cell
dgv.ContextMenuStrip = CmsDgvItemsRows
dgv.ContextMenuStrip.Show()
End Select
End If
End Sub
Any ideas?

Duh. Maybe if I had the RIGHT EVENT the first time it would have worked.
Here's the proper code with NO need to "dgv.ContextMenuStrip.Show()" again...
Private Sub DgvItems_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DgvItems.CellMouseDown
Dim dgv As DataGridView = DgvItems
If MouseButtons.Right Then
Select Case md.HitTestInfo.Type
Case DataGridViewHitTestType.ColumnHeader
dgv.ContextMenuStrip = CmsDgvItemsColHdrs
Case DataGridViewHitTestType.Cell
dgv.ContextMenuStrip = CmsDgvItemsRows
End Select
End If
End Sub

Related

combox selected index didnt work on picturebox

Now what i was doing is when i click a button group box 1 and group box 2 will show out i want to select the combobox item than the picturebox item will load
and this can be used it multiple time like when button-5 clicked the combobox item name will change and the picture will change too.
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
GroupBox1.Show()
GroupBox2.Show()
comboboxselectdiffrent.Items.Add("pizza_ChickenSupreme")
comboboxselectdiffrent.Items.Add("pizza_CockadoodleBacon")
If comboboxselectdiffrent.SelectedIndex = 0 Then
PictureBox1.Image = PIZZA_HUT_SYSTEM_NEW_VER.My.Resources.Resources.pizza_ChickenSupreme
ElseIf comboboxselectdiffrent.SelectedIndex = 1 Then
PictureBox1.Image = PIZZA_HUT_SYSTEM_NEW_VER.My.Resources.Resources.pizza_CockadoodleBacon
End If
End Sub
Can anyone tell me what i was do wrong? i have no idea why it wont work
After looking at your code personality I would have the event trigger from the combobox click, that way it will save the user having to first click on the combo to select their pizza and then having to click on a button to load the picture & details. Nevertheless try this.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("pizza_ChickenSupreme")
ComboBox1.Items.Add("pizza_CockadoodleBacon")
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GroupBox1.Show()
GroupBox2.Show()
Select Case ComboBox1.SelectedItem
Case Is = "pizza_ChickenSupreme"
PictureBox1.ImageLocation = "Pictures/mypic.jpg"
Case Is = "pizza_CockadoodleBacon"
PictureBox1.ImageLocation = "Pictures/mypic1.jpg"
End Select
End Sub
End Class
Using a custom Folder within the solution explorer will be better than using a setting resource. Create a folder and drag your pictures into it and change the name of the image location to suit your needs.
Also i think using the items name is better than the items index because what happens if someone reason that indexed item is to change from 1 to 5, you would have to recode it all, but by using the items name, it has more detail as to what to look for.
If you have any problems, leave a comment and I will do my best to help you out.Happy Coding!

Selecting cells in Datagridview using vb.net

I have a datagridview named datagridview1 inculude a table from ms sql. I want to select a cell and then select another without unselecting the first cell that I have selected before. How can I do that?
I tried this code which is not selecting anything:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If DataGridView1.CurrentCell.Selected = True Then
DataGridView1.CurrentCell.Selected = False
Else
DataGridView1.CurrentCell.Selected = True
End If
End Sub
Any suggestions?
I guess then that you could roll your own datagridview and override the OnCellMouseDown and OnCellMouseUp events to give you this effect without the mouseup cancelling out the currently selected items.
Create a new class in your solution and inherit the datagridview
Public Class MyDataGridView
Inherits DataGridView
Protected Overrides Sub OnCellMouseDown(e As DataGridViewCellMouseEventArgs)
Me.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = Not Me.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected
End Sub
Protected Overrides Sub OnCellMouseUp(e As DataGridViewCellMouseEventArgs)
End Sub
End Class
This should cancel out the what happens when the mouseclick event process fully which in a standard cell, will deselect the previous selection (unless the CTRL key is used).
Add this code, compile it and you should see at the top of your toolbox a MyDataGridView control. Drag it onto your form, populate it and give it a go.

Hide rows if column contains particular text/ string

I have a datagridview wich I have no bound datasource too I also have a button and three rows in my datagridview. If my column named STATUS contains the word CLOSED I would like to hide that entire row but i dont want to delete it just hide it.
If anyone woyuld like to know I am ussing VB.net
How can I do this?
If you are using a bound datasource you want to capture the DataGridView.DataSourceChanged event.
Would look like this.
Private Sub DataGridView1_DataSourceChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.DataSourceChanged
For Each row As DataGridViewRow In DirectCast(sender, DataGridView).Rows
If row.Cells("status").Value.ToString.ToLower.Contains("Closed") Then
row.Visible = False
End If
Next
End Sub
If you are not using a datasource you would want to capture the DataGridView.RowsAdded event.
Would look like this.
Private Sub DataGridView1_RowsAdded(sender As Object, e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
Dim dg As DataGridView = sender
If dg.Columns.Count > 0 And e.RowIndex <> 0 Then
Dim theRow As DataGridViewRow = dg.Rows(e.RowIndex)
If theRow.Cells("status").Value.ToString.ToLower.Contains("closed") Then
theRow.Visible = False
End If
End If
End Sub

How to set the value of a DataGridView's EditingControl

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

datagridview editingcontrolshowing checkboxcell

I want to change the backcolor of the edited cell when I have a checkboxcolumn in my datagridview.
For textboxcolumns the backcolor is easy to set but for some reason I don't catch this event on the checkboxcolumn when it goes into edit mode.
Is there something different I need to do to handle this scenario? I am guessing comboboxcolumns are similarly different?
Based on thetimmer's suggested answer I added the following code to the cellbeginedit event of my datagridview but it has no effect on the coloring.
If e.ColumnIndex = datagridview1.Columns("checkboxcolumn").Index Then
Dim c As DataGridViewCheckBoxCell = datagridview1.CurrentCell
c.Style.BackColor = Color.Red
End If
This works once you've left the cell
Private Sub DataGridView1_CellBeginEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
Me.DataGridView1.CurrentCell.Style.BackColor = Color.Yellow
End Sub