Cognex Insight SetComment Not Persisting - vb.net

I have a job that stores cell locations as comments in particular cells, but I'm running into a situation where the CvsInsight::SetComment method is not persisting.
I'm displaying a form as a dialog wherein the user can change the cell locations that are stored in the comment cells and when the user clicks the save button I'm creating a new instance of a custom class, setting the properties to the new cell locations (set by the user), setting the DialogResult as OK, and then closing the form. Then in the form where I called ShowDialog, I call the SetComment method for each property in the custom class on their respective cell.
This is what I'm doing in the dialog's Save button:
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
' Check if the name, username, password, pass, fail, total, reset, and results are all set
Dim invalidFields As List(Of String) = New List(Of String)()
For Each pair In _requiredFields
If (String.IsNullOrWhiteSpace(DirectCast(Controls.Find(pair.Key, True).FirstOrDefault, TextBox).Text)) Then
invalidFields.Add(pair.Value)
End If
Next
If (invalidFields.Any()) Then
MessageBox.Show($"The following required fields are missing a value: {String.Join(", ", invalidFields)}", "Invalid Form", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
' Set the returned object's values, set the dialog result, and then close the dialog
CameraSettings = New CameraSettings() With {
.FailCell = FailCellLocation.Text,
.FocusCell = FocusCell.Text,
.IsVisible = Visibility.Checked,
.PassCell = PassCellLocation.Text,
.ResetCell = ResetCellLocation.Text,
.ResultsCell = ResultsCellLocation.Text,
.TotalCell = TotalCellLocation.Text
}
DialogResult = DialogResult.OK
Close()
End Sub
And this is what I'm doing in the form that opens the dialog:
Private Sub Settings_Click(sender As Object, e As EventArgs) Handles Settings.Click
Using cameraSettingsDialog As frmCameraSetting = New frmCameraSetting(InsightDisplay.InSight)
With cameraSettingsDialog
If (.ShowDialog = DialogResult.OK) Then
InsightDisplay.InSight.SetComment(New CvsCellLocation(_focusCell), New CvsCellComment(.CameraSettings.FocusCell))
InsightDisplay.InSight.SetComment(New CvsCellLocation(_passCell), New CvsCellComment(.CameraSettings.PassCell))
InsightDisplay.InSight.SetComment(New CvsCellLocation(_failCell), New CvsCellComment(.CameraSettings.FailCell))
InsightDisplay.InSight.SetComment(New CvsCellLocation(_totalCell), New CvsCellComment(.CameraSettings.TotalCell))
InsightDisplay.InSight.SetComment(New CvsCellLocation(_resultCell), New CvsCellComment(.CameraSettings.ResultsCell))
InsightDisplay.InSight.SetComment(New CvsCellLocation(_resetCell), New CvsCellComment(.CameraSettings.ResetCell))
GetSettingCells()
End If
End With
End Using
End Sub
What is happening is that the code executes without throwing any exceptions, but the comment is not set. What's frustrating is that I'm not able to debug because the CvsInsightDisplay's Insight gets set to null anytime I try to access the results in the middle of setting the comment. However, I can verify that the CameraSettings' properties are what I'd expect them to be because if I setup a Console.WriteLine to print the various properties they're right.
Looking through the SDK, I cannot find any documentation as to why it wouldn't set the value without throwing an exception.

For those of you who run into the same issue, the problem resolved around the fact that I was trying to set a cell that was past the maximum row in the job. To fix the issue, I had to change the cells in which I was setting the comments for to ones with a lower row index.
Unfortunately, Cognex does not have this behavior documented in any of their documentation.

Related

datagridview last programmatically changed cell does not get included in changedrows

VB.net app changes values in a datagridview programmatically. The values are all as they should be, but the save routine (dtShipments is the datatable that is the source for the datagridview)
Dim dtChanges As DataTable = dtShipments.getchanges()
If more than one has changed, dtChanges is always missing the last row.
In the routine that changes the cell values, I have tried DatagridView1.EndEdit and DatagridView1.CommitEdit, but the behavior is the same. I even tried adding a SendKeys.Send(vbTab) line, since hitting the tab key when making the changes manually is enough to get all the changes to show up in .GetChanges.
What am I missing?
code per request:
Private Sub btnAssign_Click(sender As Object, e As EventArgs) Handles btnAssign.Click
strModErr = "Form1_btnAssign_Click"
Dim sTruck As String = ""
Try
sTruck = Me.DataGridView2.SelectedCells(0).Value.ToString
For Each row As DataGridViewRow In DataGridView1.SelectedRows
row.Cells("Truck").Value = sTruck
Next
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
Catch ex As Exception
WriteErrorToLog(Err.Number, strModErr + " - " + Err.Description)
End Try
End Sub
Private Function SaveChanges() As Boolean
strModErr = "Form1_SaveChanges"
Dim Conn As New SqlConnection(My.Settings.SQLConnectionString)
Dim sSQL As String = "UPDATE fs_Shipments SET Truck = #Truck, Stop = #Stop WHERE SalesOrder = #SO"
Dim cmd As New SqlCommand(sSQL, Conn)
Dim sSO, sTruck As String
Dim iStop As Integer = 0
Try
DataGridView1.EndEdit()
DataGridView1.ClearSelection()
Dim dtChanges As DataTable = dtShipments.getchanges() 'DataRowState.Modified
If Not dtChanges Is Nothing Then
Conn.Open()
For Each row As DataRow In dtChanges.Rows
sSO = row("SalesOrder").ToString
sTruck = row("Truck").ToString
iStop = CInt(row("Stop").ToString)
With cmd.Parameters
.Clear()
.AddWithValue("#SO", sSO)
.AddWithValue("#Truck", sTruck)
.AddWithValue("#Stop", iStop)
End With
cmd.ExecuteNonQuery()
Next
End If
Return True
Catch ex As Exception
WriteErrorToLog(Err.Number, strModErr + " - " + Err.Description)
Return False
End Try
End Function
I am not exactly 100% sure why this happens. The problem appears to be specific to when the user “selects” the cells in the first grid, and then calls the SaveChanges code “BEFORE” the user has made another selection in the first grid. In other words, if the user “selects” the rows to “assign” the truck to in grid 1, then, “AFTER” the “selected” cells have been filled with the selected truck, THEN, the user selects some other cell in grid 1, THEN calls the save changes code. In that case the code works as expected.
All my attempts at committing the changes, either failed or introduced other issues. I am confident a lot of this has to do with the grids SelectedRows collection. IMHO, this looks like a risky way to set the cell values, I would think a more user-friendly approach may be to add a check box on each row and assign the truck values to the rows that are checked. But this is an opinion.
Anyway, after multiple attempts, the solution I came up with only further demonstrates why using a BindingSource is useful in many ways. In this case, it appears the DataTable is not getting updated with the last cell change. Again, I am not sure “why” this is, however since it appears to work using a BindingSource, I can only assume it has something to do with the DataTable itself. In other words, before the “Save” code is executed, we could call the tables AcceptChanges method, but then we would lose those changes. So that is not an option.
To help, below is a full (no-fluff) example. In the future, I highly recommend you pull out the parts of the code that do NOT pertain to the question. Example, all the code that saves the changes to the DB is superfluous in relation to the question… so remove it. The more unnecessary code you add to your question only increases the number of SO users that will “ignore” the question. If you post minimal, complete and working code that reproduces the problem so that users can simply copy and paste without having to add addition code or remove unnecessary code will increase you chances of getting a good answer. Just a heads up for the future.
The solution below simply adds a BindingSource. The BindingSource’s DataSource is the DataTable dtShipments then the BindingSource is used a DataSource to DataGridView1. Then in the SaveChanges method, “before” the code calls the dtShipment.GetChanges() method, we will call the BindingSources.ResetBinding() method which should complete the edits to the underlying data source, in this case the DataTable dtShipments.
If you create a new VS-VB winforms solution, drop two (2) grids and two (2) buttons onto the form as shown below, then change the button names to “btnAssign” and “btnApplyChanges.” The assign button will add the selected truck in grid two to the selected rows in grid one. The apply changes button simply resets the binding source and displays a message box with the number of rows that were changed in the dtShipments DataTable. It should be noted, that the code calls the dtShipments.AcceptChanges() method to clear the changes. Otherwise, the code will update changes that have already been made.
Dim dtShipments As DataTable
Dim dtTrucks As DataTable
Dim shipBS As BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dtShipments = GetShipmentsDT()
shipBS = New BindingSource()
shipBS.DataSource = dtShipments
dtTrucks = GetTrucksDT()
dtShipments.AcceptChanges()
DataGridView1.DataSource = shipBS
DataGridView2.DataSource = dtTrucks
End Sub
Private Function GetShipmentsDT() As DataTable
Dim dt = New DataTable()
dt.Columns.Add("ID", GetType(String))
dt.Columns.Add("Truck", GetType(String))
dt.Rows.Add(1)
dt.Rows.Add(2)
dt.Rows.Add(3)
dt.Rows.Add(4)
Return dt
End Function
Private Function GetTrucksDT() As DataTable
Dim dt = New DataTable()
dt.Columns.Add("Truck", GetType(String))
For index = 1 To 10
dt.Rows.Add("Truck" + index.ToString())
Next
Return dt
End Function
Private Sub btnAssign_Click(sender As Object, e As EventArgs) Handles btnAssign.Click
Dim sTruck = DataGridView2.SelectedCells(0).Value.ToString
'Dim drv As DataRowView
For Each row As DataGridViewRow In DataGridView1.SelectedRows
'drv = row.DataBoundItem
'drv("Truck") = sTruck
row.Cells("Truck").Value = sTruck
Next
'DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
'shipBS.ResetBindings(True)
'DataGridView1.CurrentCell = DataGridView1.Rows(0).Cells(0)
End Sub
Private Function SaveChanges() As Boolean
Try
shipBS.ResetBindings(True)
Dim dtChanges As DataTable = dtShipments.GetChanges()
If (dtChanges IsNot Nothing) Then
MessageBox.Show("There are " & dtChanges.Rows.Count & " rows changed in the data table")
' update sql DB
dtShipments.AcceptChanges()
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Sub btnApplyChanges_Click(sender As Object, e As EventArgs) Handles btnApplyChanges.Click
Dim ChangesMade As Boolean
ChangesMade = SaveChanges()
End Sub
Lastly, since you are using VB… I highly recommend that you set the “Strict” option to “ON.” When this option is ON, it will flag possible errors that you may be missing. To do this for all future VB solutions, open VS, close any solutions that may be open, then go to Tools->Options->Projects and Solutions->VB Defaults, then set “Option Strict” to “On.” The default setting is off.
I hope this makes sense and helps.

Commands on Form Load

I have a command in a form which activates on Form Load which is as follows
Private Sub frm_15_IssueWarning_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lbl_incidentid.Text = frm_6_UpdateIncident.lbl_incidentid.Text
'checks to see if a containment already exists for the Incident, if it does, hide the save containment button
Dim SqlString As String = "select [containmentid],[incidentid],[containmentdate],[containment] from [containment] WHERE [incidentid] = #incidentid"
Using conn As New OleDbConnection(ConnString)
Using command As New OleDbCommand(SqlString, conn)
command.Parameters.AddWithValue("#incidentid", lbl_incidentid.Text)
Using adapter As New OleDbDataAdapter(command)
conn.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
Try
If reader.Read() Then
Button1.Visible = False
Else
Button1.Visible = True
End If
Finally
reader.Close()
End Try
End Using
End Using
End Using
And all this works great, however the first command
lbl_incidentid.Text = frm_6_UpdateIncident.lbl_incidentid.Text
If I remove this line and place it in form 6 so that when pressing the button on form 6 to open form 15 it includes the line
lbl_incidentid.Text = frm_15_IssueWarning.lbl_incidentid.Text
The rest of the command on form load no longer works, previously this has'nt been a problem because you could only access form 15 from one location but now this is changing so the form can be opened from various locations. Is there a command I can write here so that the form knows what form was open/pressed in order to make it here and act accordingly or can anyone suggest another way round this issue? I apologise if this doesnt make much sense, its a bit of a ramble.
In the end I changed the forms a little, instead of opening the form and closing the previous form straight away I inserted an if statement to see what previous form was open before closing it so the new form could act accordingly, please see example below
'checks to see which form is open to see where to get incident if from
If frm_6_UpdateIncident.Visible = True Then
Me.lbl_incidentid.Text = frm_6_UpdateIncident.lbl_incidentid.Text
frm_6_UpdateIncident.close()
End If
If frm_17_IncidentView.Visible = True Then
Me.lbl_incidentid.Text = frm_17_IncidentView.lbl_incidentid.Text
frm_17_IncidentView.close()
End If
It was actually really simple, no idea why I didint think of this in the first place

combobox recalling previous datasource

Looking for some insight here...
On my form, I have three ComboBox controls. When the left most receives a selection, it calls a routine to enable and populate the 2nd, then the same happens for the 3rd when a selection is made in the 2nd.
Private Sub cboEquipment_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboEquipment.SelectedIndexChanged
Me.transData.isMobile = If(Me.cboEquipment.Text = MOBIEQIP, 1, 0)
If cboMembership.Enabled = False Then
Me.cboMembership.Enabled = True
End If
Call loadGroups()
End Sub
The method 'loadGroups'
Private Sub loadGroups()
Dim cn As New SqlConnection(My.Settings.MyConnectionString)
Dim cm As New SqlCommand("SELECT ... ORDER BY codeHumanName ASC", cn)
Dim ta As New SqlDataAdapter(cm)
Dim dt As New DataTable
Me.cboMembership.DataSource = Nothing
Me.cboMembership.Items.Clear()
Me.transData.membership = Nothing
cn.Open()
ta.Fill(dt)
With Me.cboMembership
.DataSource = dt
.DisplayMember = "codeHumanName"
.ValueMember = "codeID"
End With
cn.Close()
End Sub
This works fine an dandy, everything loads at should, and provides the appropriate .ValueMember results.
I should mention that when the Form first loads, boxes 2 and 3 are empty and disabled. After their initial load, I have to select an item from the list to get values (as expected).
The odd part is that once the 2nd or 3rd Combobox have been 'initialized', they seem to recall last settings. For example, if I call the Reset method, which sets the .DataSource property to Nothing, clear the list, and set the Text to Nothing; the next time I change the SelectedIndex on cboEquipment, the other boxes come back to life with SelectedItems and SelectedValues.
These values aren't wrong according to the new DataTable, but it is odd that they remember at all (they shouldn't have any reference, and behave as a fresh load).
Other points of interest; the populate methods are called during the SelectedIndexChanged event.
Any thoughts would be great!
UPDATE -
This behavior is tied in with the SelectedIndexChange Event bouncing around (raise! raise! raise!)
Removing and adding event handlers at strategic points has my code working as I want/expect it to.
Here is the article that put me on the right path...
On your second and third combo boxes try clearing the .Text/.SelectedText properties before you set the .DataTable
The combo box is probably just being too helpful and assuming that when you set the .DataTable a second time, you'd want the selected item to match your .Text property.
I ended up using AddHandler and RemoveHandler to prevent chain-firing events as the comboboxes were being populated.

How to change the data source of a ComboBox?

I have two comboboxes. The data sorce of combobox1 is a list of string that is fixed. Combobox2's data sorce will be a list of string that depends on the selection of combobox1. This is very similar to a common case as follows: first enter your contry, then depending on your enter, the second combobox will show you the list of universities in the country.
'Change selection of first combobox
Private Sub cbxClient_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxClient.SelectedIndexChanged
Try
If cbxClient.SelectedIndex <> -1 Then
GetAccount()
End If
Catch
Throw
End Try
End Sub
'Based on selection of first combobox, update the data sorce of second combobox
Private Sub GetAccount()
Try
m_listAccount.Clear()
Dim strClient As String = cbxClient.SelectedItem.ToString
For i As Integer = 0 To m_listDS.Count - 1
If m_listDS(i).Client.Tostring = strClient Then
m_ds = m_listDS(i)
Exit For
End If
Next
If Not m_ds Is Nothing Then
For Each row As DataRow In m_ds.Tables("Account").Rows
m_listAccount.Add(row("account").ToString)
Next
End If
cbxAccount.DataSource = m_listAccount
Catch ex As Exception
End Try
End Sub
My problem is that though m_listAccount updates (has the correct information), the choices shown in cbxAccount are not updated according to m_listAccount (has the wrong information). I do not understand why this happens.
Note: Assume that old string in m_listAccount is {"old1"} (the list only has 1 string) and after updating, the string in m_listAccount is {"new"}. Through break points, I get the following:
cbxAccount.DataSorce={"new"}
cbxAccount.SelectedItem={"old1"}
In the form, cbxAccount shows "old1" string.
Try this,
cbxAccount.DataSource = Nothing
cbxAccount.DataSource = m_listAccount
If you are just updating the same list object, the datasource might not automatically update, but should if the pointer changes to the datasource.
Or you could try,
m_listAccount.AcceptChanges()
If m_listAccount is a DataTable
Doesn't look like you're actually calling the databind method, i.e.
cbxAccount.DataBind()
Try putting that just after you set the data source.

How to wait for a Visual Basic 2010 form's button-click event to end

In my vb program, I have two forms that work together to allow you to define an almost arbitrary amount of designs. In the first form, there is a two-dimensional array, storing an integer (not important) and an ID class that I defined as a public inner class in another form. Clicking on the "Define ID" button in this form will take you to the next form, where you will define a new ID or edit an old one based off of an index in the array you selected or entered in the first form's combo box, using multiple fields in the second form to define its different aspects. What I want to happen is the first form's code to be suspended as the user defines the ID in the second form, then they click on the Continue button on the second form. After a bit of error-checking, the form either is momentarily hidden (which is how I originally implemented it) or returns the edited ID object, but only if the error-checking does not indicate any input is incorrect. Originally, I simply grabbed the ID by using newForm.curID and set that to its spot in the array, waiting for the user to finish by using newForm.showDialog(), but sometimes the program would return a null exception because I don't think I copy the IDs correctly. Also, though the errors show up for a small amount of time, the Continue button saves the ID regardless of its correctness. Is there a way to manipulate the showDialog to get a result response out of it?
Short version: I need to either return an object of an inner class in one form to an array in a calling form or wait for a form to finish and use the calling form to copy its object, then close the callee form.
First Form's "Define" button method
Private Sub DesignButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DesignButton.Click
Me.totalCoilBox.Focus() 'calls validating method, makes textbox yellow if invalid'
If totalCoilBox.BackColor = Color.White Then
Dim newForm As New IVD_DesignData 'creates an object of ID called curID as well'
Try
If DesignIDBox.Items.Contains(DesignIDBox.Text) Then 'design exists
set the curID to the one already in the array'
newForm.curID = CGID(Integer.Parse(DesignIDBox.Text), 1)
'set number of coils to new number, if applicable'
CGID(Integer.Parse(DesignIDBox.Text), 0) = Integer.Parse(DesignIDBox.Text)
'show dialog for editing'
newForm.ShowDialog()
'put the (edited) curID back into the array'
CGID(Integer.Parse(DesignIDBox.Text), 1) = newForm.curID
Else 'design does not exist, make a new one
put number of coils into new array spot'
CGID(Integer.Parse(DesignIDBox.Text), 0) = Integer.Parse(totalCoilBox.Text)
'set the design ID to the number of defined IDs plus one'
newForm.curID.designID = Integer.Parse(DesignIDBox.Text)
'show dialog for editing'
newForm.ShowDialog()
'add curID into the array'
CGID(Integer.Parse(DesignIDBox.Text), 1) = newForm.curID
'add that design number to the dropdown box'
DesignIDBox.Items.Add(newForm.curID.ToString())
'increment number of defined designs'
Current_IDBox.Text = Integer.Parse(Current_IDBox.Text) + 1
End If
Catch ex As ArgumentOutOfRangeException 'dropdown box has no elements in it;
do the same as adding a new ID:
put number of coils into new array spot'
CGID(Integer.Parse(DesignIDBox.Text), 0) = Integer.Parse(totalCoilBox.Text)
'set the design ID to the number of defined IDs plus one'
newForm.curID.designID = Integer.Parse(DesignIDBox.Text)
'show dialog for editing'
newForm.showDialog()
'add curID into the array'
CGID(Integer.Parse(DesignIDBox.Text), 1) = newForm.curID
'add that design number to the dropdown box'
DesignIDBox.Items.Add(newForm.curID.ToString())
'increment number of defined designs'
Current_IDBox.Text = Integer.Parse(Current_IDBox.Text) + 1
End Try
DesignIDBox.Text = newForm.curID.ToString()
newForm.Close()
End If
Second Form's "Continue" button method
Private Sub ContinueButton_Click(ByVal sender As System.Object, ByVal e As ByVal e As System.EventArgs) Handles ContinueButton.Click
Me.NTBox.Focus()
Me.IDBox.Focus()
Me.ODBox.Focus()
Me.TBox.Focus()
Me.WBox.Focus()
If HSCBox.Checked Then
Me.HSC_MultBox.Focus()
Else
curID.HSC = "n"
curID.HSC_Mult = 1
End If
If NTBox.BackColor = Color.White And IDBox.BackColor = Color.White And ODBox.BackColor = Color.White And TBox.BackColor = Color.White And WBox.BackColor = Color.White And ((HSCBox.Checked And HSC_MultBox.BackColor = Color.White) Or Not (HSCBox.Checked)) Then
'success! window falls back?'
End If
End Sub
Thanks for any and all help! I'm much more proficient in Java, so this VB project is a bit frustrating sometimes.
On your second form, make a method like this:
public function ReturnSomething(curId as whateverType) as whateverReturnType
me.curId = curId
me.showDialog
return someValue
end function
then call that from your first form.
EDIT: also, have your second form's Continue method call Me.Close. This will close the second form. The ShowDialog call will block until the form is closed.
The first form should not be calling the second form's close method.