Hello i am trying to add a click event to chart points but i am getting the following Error when i click the chart "Object reference not set to an instance of an object"
here is my code
Private Sub Chart1_Click(sender As Object, e As System.EventArgs) Handles Chart1.Click
Try
Dim pointindex As Integer
If result.ChartElementType = ChartElementType.DataPoint Then
pointindex = result.PointIndex
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
result = Chart1.HitTest(e.X, e.Y)
End Sub
If the mouse cursor is above a control only the control will receive events but not the form (for workarounds see e.g. this question: Winforms : Intercepting Mouse Event on Main Form first, not on Controls).
So Form1_MouseDown will not fire and result will still be Nothing in Chart1_Click.
A workaround could look like this:
Private Sub Chart1_Click(sender As Object, e As System.EventArgs) Handles Chart1.Click
Try
Dim pointindex As Integer
Dim result As HitTestResult
result = Chart1.HitTest(Cursor.Position.X, Cursor.Position.Y)
If result.ChartElementType = ChartElementType.DataPoint Then
pointindex = result.PointIndex
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Related
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
This was the error after I set the Image to my PictureBox. Its working fine but later on the error just pop-out.
Here is my code.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim cap As New Capture() 'first line
PictureBox1.Image = cap.QueryFrame.ToBitmap 'this line AccessViolationException
Catch ex As Exception
Timer1.Stop()
MsgBox("CAMERA ERROR " & ex.Message)
End Try
End Sub
Private Sub MetroTile1_Click(sender As Object, e As EventArgs) Handles MetroTile1.Click
Try
Dim cap As New Capture() 'first line
Select Case MetroTile1.Text
Case "Capture"
Timer1.Start()
MetroTile1.Text = "OK"
Case "OK"
Timer1.Stop()
frmStudentAddEdit.picImage.Image = PictureBox1.Image
MetroTile1.Text = "Capture"
Me.Close()
End Select
Catch ex As Exception
Timer1.Stop()
End Try
End Sub
The cap.QueryFrame.ToBitmap is the AccessViolationException was unhandled error.
How can I Fix this ? What causing this error ? Please Help.
Aim for something like the following.
Capture is a member of the form (not created new each time)
oldImage is disposed of after replacing
Private mCapture As Capture
Private Sub Form12_Load(sender As Object, e As System.EventArgs) Handles Me.Load
mCapture = New Capture()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim oldImage = PictureBox1.Image
Dim newFrame = mCapture.QueryFrame.ToBitmap
PictureBox1.Image = newFrame.ToBitmap
If oldImage IsNot Nothing Then oldImage.Dispose()
Catch ex As Exception
Timer1.Stop()
MsgBox("CAMERA ERROR " & ex.Message)
End Try
End Sub
Private Sub MetroTile1_Click(sender As Object, e As EventArgs) Handles MetroTile1.Click
Try
Select Case MetroTile1.Text
Case "Capture"
Timer1.Start()
MetroTile1.Text = "OK"
Case "OK"
Timer1.Stop()
frmStudentAddEdit.picImage.Image = PictureBox1.Image
MetroTile1.Text = "Capture"
Me.Close()
End Select
Catch ex As Exception
Timer1.Stop()
End Try
End Sub
I have a Button and a DataGridView. When I press the button I want the DataGridview to be visible and when I press it again be invisible
This is what I have tried so far:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim buttonId As New Button
Dim dvg As New DataGridView
Try
dvg = DirectCast(sender, DataGridView)
dvg.Visible = True
Catch ex As Exception
End Try
End Sub
I know the question might sound very basic, but I am quite inexperienced, so the help will be very much appreciated
first off, i would have the datagridview object as a member of your class.
then i would turn it on and off like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If dgv.Visible Then
dgv.Hide()
Else
dgv.Show()
End If
End Sub
hope this helps
Not sure adding controls dynamically is a good idea, but...
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Try
Dim dgv As New DataGridView
dgv.Name = "dgvN"
dgv.Size = New Size(Me.ClientSize.Width - 20, 300)
dgv.Top = Me.ClientSize.Height / 2 - dgv.Height / 2
dgv.Left = Me.ClientSize.Width / 2 - dgv.Width / 2
dgv1.BringToFront()
Me.Controls.Add(dgv) ' use Controls() of desired container
Dim newButton As New Button
newButton.Text = "DGV On/Off"
newButton.Width = TextRenderer.MeasureText(newButton.Text, newButton.Font).Width + 20
newButton.Tag = "dgvN"
Me.Controls.Add(newButton) ' use Controls() of desired container
AddHandler newButton.Click, AddressOf DGVVisibleButtonClick
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub DGVVisibleButtonClick(sender As Object, e As EventArgs)
Dim dgv As DataGridView = Me.Controls(sender.tag)
dgv.Visible = Not dgv.Visible
End Sub
Whenever I run the following code it shows the error as "object reference not set to an instance of an object" :
(This codes change the value of DatagridViewComboBox as per other DatagridViewComboBox in same row and sharing same databse table.)
Private Sub dgv1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellValueChanged
Try
Dim currentrowindex As Integer = dgv1.CurrentRow.Index
Dim obj As Object = dgv1.CurrentCell.Value
Me.dgv1(1, currentrowindex).Value = obj
Me.dgv1(2, currentrowindex).Value = obj
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub dgv1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv1.CurrentCellDirtyStateChanged
dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub
Please tell me how can I fix this error ???
thanks to everyone now I have done this as :
Private Sub datagridview2_cellvaluechanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
Try
Dim currentrowindex As Integer = dgv2.CurrentRow.Index
Dim obj As Object = dgv2.CurrentCell.Value ' we can take STRING or OBJECT var is mandatory
Me.dgv2(4, currentrowindex).Value = obj
Me.dgv2(5, currentrowindex).Value = obj
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
and add a handler in form load :
AddHandler dgv2.CellValueChanged, AddressOf datagridview2_cellvaluechanged
So I have my code, and it's kind of working... It shows the contextmenu on the listview when I right click, but I don't think the select case is working, when I click on either edit or delete, nothing happens. Here's my code
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ComboBox1.SelectedItem = "TRUE"
Dim ctx As New ContextMenu
Dim i1 As New MenuItem("&Edit")
Dim i2 As New MenuItem("&Delete")
AddHandler i1.Click, AddressOf ContextMenuHandler
AddHandler i2.Click, AddressOf ContextMenuHandler
ctx.MenuItems.Add(i1)
ctx.MenuItems.Add(i2)
Me.ListView1.ContextMenu = ctx
End Sub
Private Sub ContextMenuHandler(ByVal Sender As Object, ByVal e As EventArgs)
Dim mi As MenuItem = DirectCast(sender, MenuItem)
Select Case mi.Text.ToLower()
Case "edit"
ListViewToText()
Case "delete"
Try
If ListView1.SelectedItems.Count > 0 Then
ListView1.Items.Remove(ListView1.SelectedItems(0))
End If
Catch ex As Exception
End Try
End Select
End Sub
Thank you!
The Select Case block isn't working because the text is "&edit" and "&delete". The & will appear as part of the Text property.
Note that if you are going to customize the ContextMenuHanndler function for every item that is clicked then a better strategy is to just have a different handler for each one
AddHandler i1.Click, AddressOf EditHandler
AddHandler i2.Click, AddressOf DeleteHandler
Private Sub EditHandler(ByVal Sender As Object, ByVal e As EventArgs)
ListViewToText()
End Sub
Private Sub DeleteHandler(ByVal Sender As Object, ByVal e As EventArgs)
Try
If ListView1.SelectedItems.Count > 0 Then
ListView1.Items.Remove(ListView1.SelectedItems(0))
End If
Catch ex As Exception
End Try
End Sub
Dim i1 As New MenuItem("&Edit")
Dim i2 As New MenuItem("&Delete")
Private Sub ContextMenuHandler(ByVal Sender As Object, ByVal e As EventArgs)
Dim mi As MenuItem = DirectCast(sender, MenuItem)
Select Case mi.Text.ToLower()
Case "edit"
ListViewToText()
Case "delete"
Try
If ListView1.SelectedItems.Count > 0 Then
ListView1.Items.Remove(ListView1.SelectedItems(0))
End If
Catch ex As Exception
End Try
End Select
Change the Case name from "Edit" to "&Edit" same as the declared name above..
I am using vb.net designer to connect to access database .
On my Form1 I have a DataGridView And Two Button For Add And Edit
I Make Form2 To Add Data Into Database And Worked OK ..
Imake Form3 Wiht Same form2 Content
Now I need When i selcet row in DataGridView And Clic Edit Button The data of selected row show on form3 for Edit it
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.SalesTableAdapter.Fill(Me.OrdersDataSet.sales)
Me.DateTimePicker1.Value = Date.Today
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
SalesBindingSource.Filter = String.Format("date = '{0}'", DateTimePicker1.Value.ToShortDateString())
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form3.Show()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub SalesDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles SalesDataGridView.CellContentClick
End Sub
End Class
You need to approach this in a modal/dialog way. You only need one form for both add and edit.
Add/Edit form
Add a parameterized constructor to the form.
Public Sub New(row As DataRowView)
Me.InitializeComponent()
'Me.ctlAge: NumericUpDown control.
'Me.ctlBirthday: DateTimePicker control.
'Me.ctlName: TextBox control.
If (row Is Nothing) Then
'Add mode, set default values:
Me.ctlAge.Value = 0
Me.ctlBirthday.Value = Date.Now
Me.ctlName.Text = String.Empty
Else
'Edit mode, set current values:
Me.ctlAge.Value = CDec(row.Item("AGE"))
Me.ctlBirthday.Value = CDate(row.Item("BIRTHDAY"))
Me.ctlName.Text = CStr(row.Item("NAME"))
End If
End Sub
You also need an accept button and a cancel button.
Friend Sub btnAcceptClicked(sender As Object, e As EventArgs) Handles btnAccept.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Friend Sub btnCancelClicked(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Main form
Add method:
Private Sub btnAddClicked(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
Using f As New AddOrEditForm(CType(Nothing, DataRowView))
If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then
Dim view As DataView = TryCast(Me.SalesDataGridView.DataSource, DataView)
If (view Is Nothing) Then
Throw New InvalidCastException()
End If
Dim viewRow As DataRowView = view.AddNew()
viewRow.EndEdit()
viewRow.Item("AGE") = f.ctlAge.Value
viewRow.Item("BIRTHDAY") = f.ctlBirthday.Value
viewRow.Item("NAME") = f.ctlName.Text
viewRow.EndEdit()
End If
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Edit method:
Private Sub btnEditClicked(sender As Object, e As EventArgs) Handles btnEdit.Click
Try
Me.SalesDataGridView.EndEdit()
If (Me.SalesDataGridView.SelectedRows.Count > 0) Then
Dim gridRow As DataGridViewRow = Me.SalesDataGridView.SelectedRows(0)
Dim viewRow As DataRowView = TryCast(gridRow.DataBoundItem, DataRowView)
If (viewRow Is Nothing) Then
Throw New InvalidCastException()
End If
Using f As New AddOrEditForm(viewRow)
If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then
viewRow.BeginEdit()
Try
viewRow.Item("AGE") = f.ctlAge.Value
viewRow.Item("BIRTHDAY") = f.ctlBirthday.Value
viewRow.Item("NAME") = f.ctlName.Text
viewRow.EndEdit()
Catch ex As Exception
viewRow.CancelEdit()
Throw ex
End Try
End If
End Using
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub