Enabling and Disabling cbomboBox in VB.net with modules - vb.net

I am trying to enable and disable two combo boxes depending on a selection.
Public Function itemsEnabled(item, item2)
Dim cboCate As String
Dim cboItemM = frmStore.cboItem
Dim cboItemM2 = frmStore.cboItem2
cboCate = frmStore.cboCategory.SelectedItem
cboItemM = frmStore.cboItem
cboItemM2 = frmStore.cboItem2
Try
If cboCate = "Coffee" Then
cboItemM.Enabled = True
cboItemM2.Enabled = False
'if coffee is chosen then colddrink combo box is disabled
ElseIf cboCate = "ColdDrink" Then
cboItemM2.Enabled = True
cboItemM.Enabled = False
'if colddrink is chosen then coffe combo box is disable
End If
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
Return cboItemM
End Function'
And when I select an option in the cboCate (category) nothing happens, I tried adding ToString() at the end of line 5 but I get the error:
object reference not set to an instance of an object.

Related

VSTO MailItem.Save error “The operation cannot be performed because the message has been changed”

I am trying to change categories color of a current selected mail in outlook 2013 using the explorer object to get the current selected item. Everything seems to be well except when I save it gives the error mentioned above. I have been looking for solutions and no luck any ideas? Thanks. here is my code in VB
Private Sub exp_SelectionChange() Handles exp.SelectionChange ' errrrorr
Try
waitapprovemail = Application.Session.GetItemFromID(exp.Selection.Item(1).EntryID)
if (CheckForRedCategory(waitapprovemail)) Then
If (CheckToReleaseMail(waitapprovemail)) Then
waitapprovemail.Categories = "Green Category"
waitapprovemail.Save() ''' this gives the error
End If
End If
Catch Exc As System.Runtime.InteropServices.COMException
MsgBox(Exc.Message & " " & Exc.Source)
Catch exc As System.InvalidCastException
MsgBox("Casting problem")
End Try
End Sub
Private Function CheckToReleaseMail(mail As MailItem) As Boolean ' errrrrr
' check the id with the ids in the locked mail, if found id then check the other flag if it is false or true, if found true then set the category of that waitemail to empty "" else keep it
Dim r As Boolean = True
Dim sarray As String()
' ofile2 = fso2.OpenTextFile("C:\Users\" & userName & "\Documents\Outlook Files\LockedMail.txt", 8, True) '8 for appending in arg2 0 for tristatefalse optional opens as ascii
Try
Using sr As New StreamReader("C:\Users\" & userName & "\Documents\Outlook Files\LockedMail.txt")
Dim line As String
Do
line = sr.ReadLine()
If (line.Equals("") Or line Is Nothing) Then
r = True
Continue Do
Else
sarray = line.Split(",")
If (sarray.Count > 0) Then
If (sarray(0).Equals(mail.EntryID, StringComparison.InvariantCultureIgnoreCase)) Then
r = False
sr.Close()
mail.Close(OlInspectorClose.olDiscard)
Return r
End If
End If
End If
Loop Until line Is Nothing
sr.Close()
End Using
Catch exc As System.Exception
End Try
mail.Close(OlInspectorClose.olDiscard)
Return r
End Function
Private Function CheckForRedCategory(mail As MailItem) As Boolean ' errrrrr
Dim b As Boolean = False
Try
If (mail.Categories.Equals("Red Category")) Then
b = True
mail.Close(OlInspectorClose.olDiscard)
Return b
Else
b = False
End If
Catch exc As System.NullReferenceException
b = False
mail.Close(OlInspectorClose.olDiscard)
End Try
mail.Close(OlInspectorClose.olDiscard)
Return b
End Function

Setting ReadOnly attribute to all Textboxes in Array of Controls

I have the following code looping through a variety of arrayed controls in a form:
For r As Long = LBound(ctrlArray) To UBound(ctrlArray)
If TypeOf ctrlArray(r) Is TextBox Then
ctrlArray(r).Text = ""
If ctrlArray(r).ReadOnly = False Then
ctrlArray(r).ReadOnly = True
End If
Else
If ctrlArray(r).Enabled = True Then
ctrlArray(r).Enabled = False
End If
End If
Next
I receive the error "'ReadOnly' is not a member of System.Windows.Forms.Control" when trying to set textboxes as read only.
Solved this right before I hit the submit button. Thought I would share anyway:
Dim tbx As TextBox
For r As Long = LBound(ctrlArray) To UBound(ctrlArray)
If TypeOf ctrlArray(r) Is TextBox Then
ctrlArray(r).Text = ""
tbx = ctrlArray(r)
If tbx.ReadOnly = False Then
tbx.ReadOnly = True
End If
Else
If ctrlArray(r).Enabled = True Then
ctrlArray(r).Enabled = False
End If
End If
Next

cannot get selectedvalue of combobox, returns empty

I'm sure this is really something stupid in my code, but I cannot get the selected value from my combobox for the life of me. Here is my code.
Dim objScales As List(Of My.Scale) = Nothing
Dim ExistingDimScale As Double = 0
Dim ExistingDimScaleIndex As Double = 0
_ScaleForm = New ScaleForm
Try
Me.LoadProperties()
If Me.ConfigUnits <> 0 Then
'Get the right scales per units
If Me.ConfigUnits = 1 Then 'imperial
objScales = Me.GetImperialScales()
Else
objScales = Me.GetMetricScales()
End If
'Load up the combobox values
If objScales IsNot Nothing Then
_ScaleForm.cmbScale.DisplayMember = "Name"
_ScaleForm.cmbScale.ValueMember = "DimScale"
For Each objScale In objScales
_ScaleForm.cmbScale.Items.Add(objScale)
'MsgBox(objScale.Name.ToString)
Next
'Set the selected Index to the current dim scale
Double.TryParse(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("Dimscale").ToString, ExistingDimScale)
ExistingDimScaleIndex = objScales.FindIndex(Function(Val) Val.DimScale = ExistingDimScale)
If ExistingDimScaleIndex = -1 Then
_ScaleForm.cmbScale.SelectedIndex = 0
Else
Integer.TryParse(ExistingDimScaleIndex.ToString, _ScaleForm.cmbScale.SelectedIndex)
End If
Else
MsgBox("There were no scales set")
End If
Else
Throw New System.Exception("Error Reading Configuration Units")
End If
Catch ex As System.Exception
MsgBox(ex.Message)
'handle it here internally
End Try
_ScaleForm.ShowDialog()
If DialogResult.OK = 1 Then
MsgBox(_ScaleForm.cmbScale.SelectedValue)
End If
The second from the last line MsgBox(_ScaleForm.cmbScale.SelectedValue), this is where I want to use the selected value to do stuff but it keeps popping up empty in the messagebox. I'm tired and unsure of why it's not working.
You are not setting the DataSource property of the ComboBox but inserting every item one by one in the items collection. Try to set the DataSource
_ScaleForm.cmbScale.DataSource = objScales
and you will get the SelectedValue set.
In alternative you could read the SelectedItem property that will return a Scale object if something has been selected and then get the DimScale field from this instance
if DialogResult.OK = _ScaleForm.ShowDialog() Then
if _ScaleForm.cmbScale.SelectedItem IsNot Nothing Then
My.Scale obj = CType(_ScaleForm.cmbScale.SelectedItem, My.Scale)
....
End If
End If

Resetting Datagridview combo box data at runtime

I am creating grid on form load event.
Initially I am setting some values in datagridview combobox as :
Dim dgvc As DataGridViewComboBoxCell
datagrigview1.Rows(0).Cells("Column1").Value = txtColumn1.Text \\setting selected item
datagrigview1.Rows(0).Cells("Column1").Value = txtColumn2.Text
dgvc = datagrigview1.Rows(0).Cells("Column1").Value
dgvc.Items.Add((" ")) \\adding blank
dgvc.Items.Add(txtColumn1.Text) \\then required value
dgvc = datagrigview1.Rows(0).Cells("Column1").Value
dgvc.Items.Add((" "))
dgvc.Items.Add(txtColumn2.Text)
Now when user clicks on particular combobox.I am setting new values in it as :
// Resetting old values
If IsDBNull(dgvc) = False Then
dgvc.DataSource = Nothing
dgvc.Items.Clear()
End If
If DtTable.Rows.Count > 0 Then
Dim k As Integer
Dim dgvc1 As DataGridViewComboBoxCell
dgvc1 = New DataGridViewComboBoxCell()
For k = 0 To DtTable.Rows.Count - 1
If DtItemCd.Rows(k)("ItemCd").ToString <> Current_Code Then
datagrigview1.Rows(e.RowIndex).Cells("Column1").Value = DtTable.Rows(k)("Column1").ToString
dgvc1 = datagrigview1.Rows(e.RowIndex).Cells("Column1")
dgvc1.Items.Add(DtTable.Rows(k)("Column1").ToString)
datagrigview1.Rows(e.RowIndex).Cells("Column2").Value = DtTable.Rows(k)("Column2").ToString
dgvc1 = datagrigview1.Rows(e.RowIndex).Cells("Column2")
dgvc1.Items.Add(DtTable.Rows(k)("Column2").ToString)
End If
Next
End If
This shows both old and new records.Please help.
Probably your code here
If IsDBNull(dgvc) = False Then
is testing if the DataGridViewComboBoxCell is Null not if it's DataSource is null.
Hence it never enters the condional code below.
Could you try to change in this way and see if now you enter in the if condition?
If IsDBNull(dgvc.DataSource) = False Then
I got below solution :
If IsDBNull(dgvc) = False Then
dgvc.Items.Clear()
dgvc.DataSource = Nothing
dgvc = dgvSO.Rows(e.RowIndex).Cells("Column1")
dgvc.Items.Remove(" ")
dgvc.Items.Remove(Current_Code)
End If
This works
cb.SelectedItem = Nothing

Enumeration in vb.net

while executing this below lines i got an error. Error:
Collection was modified; enumeration operation may not execute.
Help me to solve this.
Dim i As IEnumerator
Dim item As DataGridItem
Dim bChk As Boolean = False
i = dgOfferStatus.Items.GetEnumerator
For Each item In dgOfferStatus.Items
i.MoveNext()
item = i.Current
item = CType(i.Current, DataGridItem)
Dim chkItemChecked As New CheckBox
chkItemChecked = CType(item.FindControl("chkItemChecked"), CheckBox)
If chkItemChecked.Checked = True Then
Try
bChk = True
lo_ClsInterviewProcess.JobAppID = item.Cells(1).Text
lo_ClsInterviewProcess.candId = item.Cells(9).Text
Dim str, strSchedule1, strSchedule As String
Dim dspath As DataSet
Dim candidateId As Integer
''Moving the resume to Completed folder
ObjInterviewAssessment = New ClsInterviewAssessment
dspath = ObjInterviewAssessment.GetOffComPath(CInt(lo_ClsInterviewProcess.JobAppID), "GetHoldPath")
If dspath.Tables(0).Rows.Count > 0 Then
If Not IsDBNull(dspath.Tables(0).Rows(0).Item(0)) Then
str = dspath.Tables(0).Rows(0).Item(0)
strSchedule1 = str.Replace("Hold", "Completed")
End If
End If
Dim str1 As String
str1 = Server.MapPath(str).Trim
strSchedule = Server.MapPath(strSchedule1).Trim
Dim file1 As File
If file1.Exists(str1) Then
If file1.Exists(strSchedule) Then
file1.Delete(strSchedule)
End If
file1.Move(str1, strSchedule)
End If
''
intResult = lo_ClsInterviewProcess.UpdateApproveStatus(Session("EmployeeId"), strSchedule1)
BindHoldGrid()
If intResult > 0 Then
Alert.UserMsgBox("btnsearch", "Status Updated")
Else
Alert.UserMsgBox("btnsearch", "Status not Updated")
End If
Catch ex As Exception
ExceptionManager.Publish(ex)
Throw (ex)
End Try
End If
Next
If bChk = False Then
Alert.UserMsgBox("btnsearch", "Please Select any Candidate")
End If
'Catch ex As Exception
' ExceptionManager.Publish(ex)
'End Try
End Sub
Look at this part of your code. I think it's what causes your exception.
Dim i As IEnumerator
...
Dim item As DataGridItem
...
i = dgOfferStatus.Items.GetEnumerator
For Each item In dgOfferStatus.Items
i.MoveNext()
item = i.Current ' <-- here be dragons!? '
...
Next
What you're doing seems a little strange. You iterate through the same collection (dgOfferStatus.Items) twice, once with the For Each loop, and once manually using the i iterator. Then you modify items in your collection with item = i.Current. I believe it's this assignment that causes the exception.
(I also don't understand why you would do this. This assignment seems to be completeley superfluous, since i.Current and item should be identical since both iterators are at the same position in the collection.)
The exception basically tries to tell you that you may not modify a collection while you are iterating through it. But you seem to be doing exactly that.