I am enhancing a previously written VB.NET WIndows forms application.
In one of the forms, there is a DataGridView populated from a stored procedure at Form Load.
If the user has the privileges, when the user right clicks on a record in the datagridview, I want to show a context menu allowing the user to delete the record.
Here is my code:
Private Sub m_SetEnabledContextMenu()
' for Delete record
If Not objUser.HasAuthority("Delete Record") Then
Me.DataGridView1.ContextMenu = Nothing
Me.mnuContextEquationSubmission.Enabled = False
Else
' Me.DataGridView1.ContextMenu = Me.mnuContextEquationSubmission
Me.mnuContextEquationSubmission.Enabled = True
End If
'Rest is not problem
End Sub
Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
Handles DataGridView1.CellMouseDown
If e.Button = System.Windows.Forms.MouseButtons.Right Then
For Each row As DataGridViewRow In DataGridView1.SelectedRows
row.Selected = False
Next
DataGridView1.Rows(e.RowIndex).Selected = True
'MessageBox.Show("DataGridView1_CellMouseDown on row " & e.RowIndex.ToString)
m_intSelRow = e.RowIndex
m_intSelCol = e.ColumnIndex
m_strRecordID = DataGridView1.Rows(e.RowIndex).Cells(0).Value
'mnuContextEquationSubmission.Show(DataGridView1, e.Location)
mnuContextEquationSubmission.Show(CType(sender, Control), e.Location)
End If
End Sub
as you can see, in the procedure 'm_SetEnabledContextMenu' I determine whether of not the user will have the authority to delete records, and thus the context menu will be activated (or at least, that's what I want) - whilst of the user doesn't have the authority, the menu should be invisible and disabled.
In the event handler DataGridView1_CellMouseDown, if it's a right-click and if the user has clicked in a data row, then I want the context menu to display where the mouse is, NOT at the top!
Use mnuContextEquationSubmission.Show(Me, Me.PointToClient(MousePosition))
Related
I need a checkbox which will ask the user:"Do you need to add an address?" and if user clicks on it then address label and textbox appears on the form ( I mean By default they were invisible and then I have changed this state), I have implemented this functionality by CheckboxSate event but what if the user doesn't click on the checkbox at all, in this case after submission external details I am not able to move forward ( i mean after clicking on the next button my form don't call another subform, but when I check/uncheck checkbox it works correctly), is there any way to edit/update form validation or any default checkbox property in order to get rid of this bag?, here is my CheckState Event's code:
Private Sub AddAddress_CheckedStatrChange_1(sender As Object, e As EventArgs) Handles AddAddress.CheckStateChanged
If AddAddress.CheckState = CheckState.Checked Then
AddAddressLabel.Visible = True
AddAddressTextBox.Visible = True
AddAddressTextBox.Enabled = True
ElseIf AddAddress.CheckState = CheckState.Unchecked Then
AddAddressLabel.Visible = False
AddAddressTextBox.Visible = False
AddAddressTextBox.Enabled = False
End If
END Sub
That's how you would do:
Sub AddFTP_CheckedChanged() Handles AddFTP.CheckedChanged
AddAddressLabel.Visible = Not AddAddressLabel.Visible
AddAddressTextBox.Visible = Not AddAddressTextBox.Visible
AddAddressTextBox.Enabled = Not AddAddressTextBox.Enabled 'It's useless since it's not visible
End Sub
VB student that needs some help with a simple two page application that:
1.) Create a sessions counter to show how many registrations are saved each time the "Submit" button is clicked
CODE FOR SESSION COUNT
If Not IsPostBack Then
If Session("key") Is Nothing Then
Session("key") = 0
Else
Session("key") = Session("key") + 1
End If
Response.Write(Session("key"))
LoadData()
End If
2.) Create a "Display" button that will show the above count on a new page when the user clicks it.
CODE TO DISPLAY SECOND PAGE WITH SESSION COUNT
Response.Redirect("PageTwo.aspx")
But nothing is displaying on the second page when I click the "Display" button. I have tried different ways to adjust my code but it doesn't work. Instead of me listing all of my changes and attempts I made I just listed my core code and need someone to please show me how I can get this to work.
I changed my IF Statement for the "Submit" button to something easier to work with and read.
Protected Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
'if session is not set then set the session and assign value to 1
If (Session.Item("numberofRegistration") Is Nothing) Then
'setting session variable
Session.Item("numberofRegistration") = 1
Else
'taking value from session and incrementing value by 1
Session.Item("numberofRegistration") = Convert.ToInt32(Session.Item("numberofRegistration").ToString()) + 1
End If
End Sub
Then for my Display Button, a simple redirect to "PageTwo"
Protected Sub DisplayButton_Click(sender As Object, e As EventArgs) Handles DisplayButton.Click
Response.Redirect("PageTwo.aspx")
End Sub
On Page Two I used a Message Label and another IF statement
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'checking if session is set or not
If (Session.Item("numberofRegistration") Is Nothing) Then
'if session variable is null then
'display error message on the lable
MessageLabel.Text = "Please set the session variable"
Else
'display value from session variable on the lable with id=lblMessage
MessageLabel.Text = String.Format("Total Number of Registrations : {0}", Session.Item("numberofRegistration").ToString())
End If
End Sub
I'm struggling with some functionality I want to use on my Windows form.
( Just for info, this is for an AutoDesk Inventor AddIn. )
This is my form layout.
The current workflow
The top 4 list-boxes are filled with available parameter names. The user chooses the parameter(s) he/she wants to use and drags and drops it into one of the driving parameter text-boxes ( marked with the <1> label ).
The code that relates to the drag and drop operations
Private Sub lstTemp_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles lbModelParameters.MouseDown,
lbUserParameters.MouseDown,
lbReferenceParameters.MouseDown,
lbLinkedParameters.MouseDown
' In order to access a specific item in a listbox.itemcollection, you must think of it
' as an array of data or a collection and access it in the same manner by always
' letting VB know which item you intend to use by identifying it with its index location
' within the collection. And this is better than taking up basket weaving :-)
lbModelParameters.DoDragDrop(sender.Items(sender.SelectedIndex()).ToString, DragDropEffects.Move)
End Sub
Private Sub txtTemp_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles tbParameter1.DragEnter,
tbParameter2.DragEnter,
tbParameter3.DragEnter,
tbParameter4.DragEnter,
tbParameter5.DragEnter
'Check the format of the incoming data and accept it if the destination control is able to handle
' the data format
'Data verification
If e.Data().GetDataPresent(DataFormats.Text) Then
e.Effect() = DragDropEffects.Move
Else
e.Effect() = DragDropEffects.None
End If
End Sub
Private Sub txtTemp_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles tbParameter1.DragDrop,
tbParameter2.DragDrop,
tbParameter3.DragDrop,
tbParameter4.DragDrop,
tbParameter5.DragDrop
'This procedure receives the dragged data once it passes the data verification handled by the DragEnter method
'Drops the data onto the destination control
sender.Text() = e.Data().GetData(DataFormats.Text).ToString()
End Sub
New functionality
Now I would like to decrease the user mouse movement for ergonomic reasons and speed. But I would also like to keep to the drag and drop functionality. As it can overwrite a value that has already been added by the user.
I would like to be able to DoubleClick a item in the listbox, and that item should be added to the first empty textbox. I named my textboxes with a number so it's easy to loop over them all to check if it's empty.
I tried doing it with this code, but my double click event never gets fired. It always goes to the drag and drop. How do you do handle this, that the double click gets fired instead of drag drop?
Private Sub ParameterAddDoubleClick(sender As Object, e As EventArgs) _
Handles lbModelParameters.DoubleClick,
lbUserParameters.DoubleClick,
lbReferenceParameters.DoubleClick,
lbLinkedParameters.DoubleClick
Dim oControl As Windows.Forms.ListBox
oControl = DirectCast(sender, Windows.Forms.ListBox)
' Add line in likedparameters listbox
If oControl.SelectedIndex <> -1 Then
' Loop trough all the controls to see if one is empty
' if it's empty add parameter, else go to next
' if all textboxes are used do nothing.
For i = 1 To 6
Dim oTextbox As Windows.Forms.TextBox =
CType(gbDrivingParameters.Controls("tbParameter" & i),
Windows.Forms.TextBox)
If oTextbox.TextLength = 0 Then
' Add the sender item into the linked listbox
oTextbox.Text = oControl.Items.Item(oControl.SelectedIndex)
End If
Next
End If
End Sub
I hope my question is clear, and well prepared. If there is additional information needed, please let me know in a comment.
Mousedown triggers the DoDragDrop, wich stops the doubleclick-event from firing.
To identify if a user doubleclicks or wants to perform a dragdrop, consider the following:
Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
' Determine whether we are performing a drag operation OR a double click
If e.Clicks = 1 Then
TextBox1.Text = "mousedown"
Else
TextBox1.Text = "dblclick"
End If
End Sub
I am a new user to vb.net and need to hide a row when a user right clicks on a contextmenu and selects hide. I have googled this but have yet to find a way to do it.
At the moment, when a user clicks on an entry in the grid, the value is entered into a text box which is fine. What I need to do is hide the entry the user right clicked on and hide the selection. As I am new I am finding it hard going to code this as I have just finished my first course which entailed the basics. Any help would be appreciated or if you need anymore code, then please ask.
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked
'Get the text of the item that was clicked on.
'Dim text As String = txtCustomerActive.Text
Try
'txtCustomerActive.Visible = False
pnlContextMenuStrip1.Visible = False
MessageBox.Show(txtCustomerActive.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
You could use Rows.Item() to hide specific DataGridViewRow, like:
If (UserDataGridView.Rows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
End If
I am assuming you are using FullRowSelect here.
If you are not using FullRowSelect you could have this alternative code which could catch both Cell being Selected or Row being Selected:
If (UserDataGridView.SelectedRows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
ElseIf (UserDataGridView.SelectedCells.Count > 0) Then
For Each cell As DataGridViewTextBoxCell In UserDataGridView.SelectedCells
UserDataGridView.Rows.Item(cell.RowIndex).Visible = False
Next
End If
To Unhide everything let's say from a Button Click you could have this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In UserDataGridView.Rows
If (row.Visible = False) Then
UserDataGridView.Rows.Item(row.Index).Visible = True
End If
Next
End Sub
As far as I know, you can not make a server-side handler for right mouse click (as you did for HideToolStripMenuItem_Click, which works as part of .NET postback mechanism).
However, I believe that such feature could be done with some client-side javascript progamming.
Hope this helps!
If a user clicks the blank row at the bottom of a DataGridView and moves focus away from the DataGridView, the row click in now is in a state that indicates a change is made to that row.
Is it possible to tell the DataGridView to un-mark this row as being changed?
Is it possible to reset this row when focus is off the DataGridView?
We are using the following event handler to alert the user if the Invoiced On is left blank:
Private Sub dataGridViewPayments_CellValidating(ByVal sender As Object, _
ByVal e As DataGridViewCellValidatingEventArgs) _
Handles DataGridViewPayments.CellValidating
Dim headerText As String = _
DataGridViewPayments.Columns(e.ColumnIndex).HeaderText
' Validate the Invoiced On cell and display the error if it's empty.
'-------------------------------------------------------------------
If (String.IsNullOrEmpty(e.FormattedValue.ToString()) And
headerText.Equals("Invoiced On")) Then
DataGridViewPayments.Rows(e.RowIndex).ErrorText = _
"Please enter an Inoiced On date."
e.Cancel = True
End If
End Sub
Looks like we need a way to stop this from executing if the user simply clicks in the grid then clicks somewhere else in the form.
You could try something like this:
Private Sub dg_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles dg.CellValidating
Dim headerText As String = dg.Columns(e.ColumnIndex).HeaderText
'Try this --------------------------------------------------------------
Dim vClicked As Boolean = False
If (Control.MouseButtons And MouseButtons.Left) = MouseButtons.Left Then
vClicked = True
End If
'-----------------------------------------------------------------------
'Validate the Invoiced On cell and display the error if it's empty.
'And put vClicked here
If Not vClicked AndAlso (String.IsNullOrEmpty(e.FormattedValue.ToString()) And headerText.Equals("Invoiced On")) Then
dg.Rows(e.RowIndex).ErrorText = "Please enter an Inoiced On date."
e.Cancel = True
End If
End Sub
Please, let me know if it helped. =)