I am trying to create an if statement using a checklistbox. How can I make an if statement with my checked value in check list box? I tried using the code below just like in combobox:
If CheckedListBox1.CheckedItems = "AHRM" Then
But I got an error:
value of type cannot be converted to string.
CheckedListBox.CheckedItems returns a collection of all checked items. Not a single string.
You could check it's Count property and then take the first if you expect only one:
Dim checked As String = Nothing
Dim checkedItems = CheckedListBox1.CheckedItems
If checkedItems.Count <> 0 Then checked = checkedItems(0).ToString()
If checked = "AHRM" Then ....
If you expect multiple you could use following little LINQ query and Contains or String.Join(if you want to concat them):
Dim checkedItems = From obj In CheckedListBox1.CheckedItems.Cast(Of Object)()
Select checkedItemAsString = obj.ToString()
Dim containsAHRM As Boolean = checkedItems.Contains("AHRM")
Dim allChecked = String.Join(",", checkedItems)
Dim values As [String] = ""
For i As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
values = CheckBoxList1.Items(i).Value
If values = "AHRM" Then
// your code
end if
End If
Next
Related
I have a list of records and for Employee R1005, I need to check if that Employee has been Enabled for login alert (i.e EnableLoginAlert = Yes), then a button will be displayed.
CompanyID EmployeeNo EnableLoginAlert
10046 R1005 Yes
20041 Ajax12 No
47021 Drek Yes
I have tried the below codes:
If dCompanyDetails.Tables(0).Rows.Count > 0 Then
Dim dataView As DataView = dCompanyDetails.Tables(0).DefaultView
dataView.RowFilter = "EmployeeNo = '" & strEmployeeNumber & "'"
Dim svalue As String = dataView.Table.Rows(0).ItemArray(0).ToString()
If svalue = "No" Then
AlertButton.Visible = False
ElseIf svalue = "Yes" Then
{
//Do something else
}
End If
End If
If you are going to use a DataView then use it. This:
Dim svalue As String = dataView.Table.Rows(0).ItemArray(0).ToString()
is simply going back to the DataTable and using it, ignoring the DataView. The DataView contains DataRowView objects so get the one you need and use it. It is similar to a DataRow and you can use it the same way in this case:
Dim enableLoginAlert = CStr(dataView(0)("EnableLoginAlert")) = "Yes"
Now you have an actual Boolean that represents the state you want.
That's not how you should do it though. Generally speaking, you would use a DataView when you want to bind data. In fact, if you bind a DataTable then the data you see in the UI actually comes from the DefaultView. That's why you can filter and sort it. In this case, there are better options.
If you want to find a row by its primary key then the Rows collection of a DataTable has a Find method, e.g.
Dim row = dCompanyDetails.Tables(0).Rows.Find(strEmployeeNumber)
Dim enableLoginAlert = CStr(row("EnableLoginAlert")) = "Yes"
If you're searching by other than the primary key, the DataTable itself has a Select method. Because multiple rows may match, it returns an array, so you need to get the row out of that, e.g.
Dim row = dCompanyDetails.Tables(0).Select($"EmployeeNo = '{strEmployeeNumber}'").First()
Dim enableLoginAlert = CStr(row("EnableLoginAlert")) = "Yes"
If you want to look up a single row it's perhaps easiest to use LINQ:
Dim row = dCompanyDetails.Tables(0).Rows.Cast(Of DataRow).AsQueryable().FirstOrDefault(Function(r) r("EmployeeNo").ToString() = strEmployeeNumber)
If row IsNot Nothing AndAlso row("EnableLoginAlert").ToString() = "Yes" Then
...
..though I'd be the first to claim that using LINQ on base DataTables is very verbose, because of the Cast/AsQueryable. I'd use strongly typed DataTables (in a dataset); if you were to convert your code to using strongly typed tables it would look like:
Dim r = someDataSet.AProperTableName.FirstOrDefault(Function(r) r.EmployeeNo = strEmployeeNumber)
If r?.EnableLoginALert = "Yes" Then
...
...using strongly typed datatables is much less messy..
nb: You need to Imports System.Linq for these to work
That LINQ is the same thing as:
For Each r as DataRow in dCompanyDetails.Tables(0)
If r("EmployeeNo").ToString() = "R1005" AndAlso r("EnableLoginAlert").ToString() = "Yes" Then
...
You also have the option of using DataTable.Select (not a LINQ thing, though LINQ has a Select too)
Dim matchingRows = dCompanyDetails.Tables(0).Select($"[EmployeeNo] = '{strEmployeeNumber}'")
If matchingrows.Count > 0 AndAlso matchingRows(0)("EnableLoginAlert").ToString() = "Yes"
The below code I tried to sum up the string value with the list values, it happens, but other values are not shown in return. I need to sum the values and other value should be returned to the object using linq in vb.net.
My code:
Dim lstrTaxValue As String = "YQ$40"
Dim lstaValues As New List(Of String)
lstaValues.Add("YQ$10")
lstaValues.Add("TQ$3")
lstaValues.Add("PQ$8")
lstaValues.Add("YQ$10")
lstaValues.Add("TQ$3")
lstaValues.Add("AQ$5")
Dim lobjTValues = (From lstr In lstaValues
From lval In lstrTaxValue.Split(" ")
Where (lstr.Split("$")(0) = CStr(lval).Split("$")(0))
Select (CStr(lval).Split("$")(0) & "$" & (CDbl(CStr(lval).Split("$")(1)) + CDbl(lstr.Split("$")(1))))).ToList()
What am I doing wrong?
To quote Jon Skeet...
Change some value inside the List<T>
In comments...
Why do you want to use lambda expressions? The foreach code works fine and is simple. LINQ is for querying data, not mutating it. – Jon Skeet
Your objective does not seem to lend itself to Linq.
Private Sub OPCode()
Dim lstrTaxValue As String = "YQ$40"
Dim lstaValues As New List(Of String)
lstaValues.Add("YQ$10")
lstaValues.Add("TQ$3")
lstaValues.Add("PQ$8")
lstaValues.Add("YQ$10")
lstaValues.Add("TQ$3")
lstaValues.Add("AQ$5")
Dim TaxValue = lstrTaxValue.Split("$"c)
For i = 0 To lstaValues.Count - 1
If lstaValues(i).Split("$"c)(0) = TaxValue(0) Then
lstaValues(i) = TaxValue(0) & "$" & CStr(CDbl(lstaValues(i).Split("$"c)(1)) + CDbl(TaxValue(1)))
End If
Next
For Each s In lstaValues
Debug.Print(s)
Next
End Sub
Result:
YQ$50
TQ$3
PQ$8
YQ$50
TQ$3
AQ$5
I have got a problem, I have got a filled dataset, but now i need to get a Column value from it and store it into a variable. On Button 2 click i fill it with this:
tbaGridview.Fill(BlGridview1.vwBLcontainerCargo, Bookingnumber)
Now i want to get the data from it that is inside. I have got a for loop:
For i As Integer = 0 To GridView1.DataRowCount - 1
Dim OriginSealNumber As String = BlGridview1.vwBLcontainerCargo.Tables("SEALNUMBER").Rows(i).Item(0)
Next i
But it says Tables is not a member of windowsapplication1.blgridView.vwBLContainerCargoDataTable. How do i get the data for each column??
To reference the GridView's table use this sintax:
((DataRowView)GridView1.Rows[0].DataBoundItem).DataView.Table
But I recommend that you use a BindingSource object and iterate through it.
I fixed it myself by using this:
For i As Integer = 0 To GridView1.DataRowCount - 1
Dim OriginSealNumber As String = BlGridview1.Tables(0).Rows(i)("SEALNUMBER").ToString()
Dim OriginContainerNumber As String = BlGridview1.Tables(0).Rows(i)("CONTAINERNUMBER").ToString()
Dim OriginContainerType As String = BlGridview1.Tables(0).Rows(i)("CONTAINERTYPE").ToString()
Dim OriginQuantity As String = BlGridview1.Tables(0).Rows(i)("QUANTITY").ToString()
Dim OriginPackageType As String = BlGridview1.Tables(0).Rows(i)("PACKAGETYPE").ToString()
Dim OriginDescription As String = BlGridview1.Tables(0).Rows(i)("DESCRIPTION").ToString()
Dim OriginWeight As String = BlGridview1.Tables(0).Rows(i)("WEIGHT").ToString()
Next i
Attempting to split and store strings from a listbox and search then search the contents of the text file I have stored them to, hopefully sorting them in different categories,
firstly I am getting an "Object reference not set to an instance of an object." error with this
Dim variable As String = Nothing
If listArray.SelectedIndex > 0 Then
variable = listArray.Items(listArray.SelectedIndex)
End If
Dim part As String() = variable.Split(New Char() {","c})
Dim line As String
For Each line In part
MessageBox.Show(line)
and secondly, would this be the right code to use for searching those separated strings?
For count As Integer = 0 To Logbook.listArray.Items.Count - 1
Dim searchIndex As String = Logbook.listArray.Items(count).ToString
If searchIndex.Contains(indexSearch.Text) Then
Logbook.listArray.SetSelected(0, True)
End If
Next
I'm pretty new to StackOverflow, my apologies if i'm not up to date with the website etiquette.
am getting an "Object reference not set to an instance of an object."
I guess you don't know that the first item is at index 0 and that SelectedIndex returns -1 if there is no item selected. That's why following code throws that exception:
Dim variable As String = Nothing
If listArray.SelectedIndex > 0 Then
variable = listArray.Items(listArray.SelectedIndex)
End If
Dim part As String() = variable.Split(New Char() {","c}) ' <--- variable Nothing if first item selected
Then you just have to use <> -1 or >= 0:
Dim variable As String = Nothing
If listArray.SelectedIndex >= 0 Then
variable = listArray.Items(listArray.SelectedIndex)
End If
According to the second part of the question(always ask only once), you haven't provided enough informations to understand what you want and what doesn't work with your code.
.. eg: have a stri ng
strResult="controlName1.value * controlName2.value"
.. I need to change it to just controlName1.value * controlName2.value so that i can get the output as double value
Please reply
Thanks
If you're using Windows Forms, there is an indexer property that accepts the name of a sub-control as a string and returns the control if a match is found. See: Control.ControlCollection.Item Property (String).aspx
The straightforward alternative in all UI frameworks is to map Strings to Controls like such:
Function MapStringToControl(ctlName As String) As Control
Select Case ctlName
Case "controlName1"
Return controlName1
Case "controlName2"
Return controlName2
Case Else
Return Nothing
End Function
Of course note that there is no .Value property in Windows Forms--you need to do something like Integer.Parse(ctl.Text).
It depends what type of control it is. For example a textbox has a .Text property. A NumericUpDown control has a .Value property.
All you need to do is to convert the appropriate property to the appropriate type. So for TextBoxes:
Dim result as Double = CDbl(txtFoo.Text) * CDbl(txtBar.Text)
For a NumericUpDown:
Dim result as Double = CDbl(nudFoo.Value) * CDbl(numBar.Value)
Hi guys thanks for your updates.. I wrote my own function by using your concepts and some other code snippets .I am posting the result
Function generate(ByVal alg As String, ByVal intRow As Integer) As String
Dim algSplit As String() = alg.Split(" "c)
For index As Int32 = 0 To algSplit.Length - 1
'algSplit(index) = algSplit(index).Replace("#"c, "Number")
If algSplit(index).Contains("[") Then
Dim i As Integer = algSplit(index).IndexOf("[")
Dim f As String = algSplit(index).Substring(i + 1, algSplit(index).IndexOf("]", i + 1) - i - 1)
Dim grdCell As Infragistics.Win.UltraWinGrid.UltraGridCell = dgExcelEstimate.Rows(intRow).Cells(f)
Dim dblVal As Double = grdCell.Value
algSplit(index) = dblVal
End If
Next
Dim result As String = String.Join("", algSplit)
'Dim dblRes As Double = Convert.ToDouble(result)
Return result
End Function
Thanks again every one.. expecting same in future