I am trying to apply if condition on data bound combobox but my code is not working. my combobox are getting data ComboBox5.DataSource = table2,ComboBox6.DataSource = table2,ComboBox6.DisplayMember = "name" from sql, My combobox has multiple names like "bob","sam","john" etc. I want to hide another combobox when i select "bob" else that comboboxes should visible. how should i do it?
Dim rv As Object = ComboBox3.Items.Cast(Of Object)().Where(Function(r) ComboBox3.GetItemText(r) = "bob").FirstOrDefault()
If ComboBox3.SelectedText = rv Then
ComboBox5.Visible = False
ComboBox6.Visible = False
ElseIf ComboBox3.SelectedText <> rv Then
ComboBox5.Visible = True
ComboBox6.Visible = True
End If
Here is some code that checks if a TextBox.Text value is in a Combobox.
Dim fn As String
For Each fn In cbType.Items
If fn = tbType.Text Then
Do the = Code from your project
Return
End If
Next
Dim fn2 As String
For Each fn2 In cbType.Items
If fn2 <> tbType.Text Then
Do the <> Code from your project
Return
End If
Next
Related
I'm trying to provide a way to filter data within a ListView using CheckBox controls. Checking a box will add or remove the selected attribute from the ListView.
The current code:
For i = 0 To RecCount
Filter = True
'RecordDat array populated here
'filter the record by checkbox selection
If ckbComplete.Checked = True And Len(RecordDat(7)) = 0 Then
Filter = False
ElseIf ckbOpen.Checked = True And Len(RecordDat(7)) > 0 Then
Filter = false
ElseIf ckbApps.Checked = True And strings.Left(RecordDat(0).ToString, 4) <> "APPS" Then
Filter = False
ElseIf ckbContract.Checked = True And Strings.Left(RecordDat(0), 3) <> "SOP" Then
Filter = false
End If
If Filter = False Then GoTo SkipItem
' More code adding the item to the list view and formatting
SkipItem:
Next
At the moment I can filter only once per array index, i.e. I can filter ckbComplete and ckbApps but not ckbcomplete, ckbApps and ckbContract.
I managed to figure out a solution by trial and error and wish to share what I found.
In order to filter the data, each variable (in this case, the column value) required its own filter statements, each which must be encased in an If ELSE statement.
The resulting code was as follows:
Dim Filter As Boolean = True
For i = 0 To RecCount
Filter = False
'RecordDat initialisation
Filter = FilterPass(RecordDat)
If Filter = False Then Continue For
Dim itm As ListViewItem
itm = New ListViewItem(RecordDat)
Healthchart.ListView1.Items.Add(itm)
'Continue formatting and input of data into listview
Next
The FilterPass functions then follow:
Private Function FilterPass(ByVal RecordDat() As String)
Dim filter As Boolean = False
If Healthchart.ckbComplete.Checked = True Then
If Len(RecordDat(7)) > 0 Then
filter = FilterPass2(RecordDat)
If filter = True Then
filter = FilterPass3(RecordDat)
End If
End If
End If
If Healthchart.ckbOpen.Checked = True Then
If Len(RecordDat(7)) = 0 Then
filter = FilterPass2(RecordDat)
If filter = True Then
filter = FilterPass3(RecordDat)
End If
End If
End If
Return filter
End Function
Private Function FilterPass2(ByVal RecordDat() As String)
Dim filter As Boolean = False
If Healthchart.ckbApps.Checked = True Then
If Strings.Left(RecordDat(0).ToString, 4) = "APPS" Then
filter = True
End If
End If
If Healthchart.ckbContract.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "SOP" Then
filter = True
End If
End If
If Healthchart.ckbTech.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "ATS" Then
filter = True
End If
End If
If Healthchart.ckbDes.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "DES" Then
filter = True
End If
End If
Return filter
End Function
Private Function FilterPass3(ByVal RecordDat() As String)
Dim filter As Boolean = True
Dim SubDate As Date = Date.ParseExact(RecordDat(1).ToString, "dd/MM/yy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
If Healthchart.ckbLast30.Checked = True Then
If DateAdd(DateInterval.Day, -30, Now) > SubDate Then
filter = False
End If
End If
If Healthchart.ckb7Days.Checked = True Then
If DateAdd(DateInterval.Day, -7, Now) > SubDate Then
filter = False
End If
End If
Return filter
End Function
One can then add as many 'filters' as required by adding an additional If Filter = true then into the FilterPass function.
I have in my Excel userform several checkboxes (in total 10) which all should be have the same behavior. It means, when I reload the form the value of the checkbox should change, depending on values from the spreadsheet with the actual selected row.
The following code is working properly for one checkbox:
If Sheet1.Cells(iRow, Sheets("aux").Range("B21")) = "X" Then
cbERW.Value = True
ElseIf Sheet1.Cells(iRow, Sheets("aux").Range("B21")) = "?" Then
cbERW.Value = Null
Else
cbERW.Value = False
End If
How can I do this in the easiest way for multiple checkboxes? But for every checkbox the referring row and column could change. For example, the next one look like this (cbHSAW instead of cbERW, Column B22 instead of B21):
If Sheet1.Cells(iRow, Sheets("aux").Range("B22")) = "X" Then
cbHSAW.Value = True
ElseIf Sheet1.Cells(iRow, Sheets("aux").Range("B22")) = "?" Then
cbHSAW.Value = Null
Else
cbHSAW.Value = False
End If
Anybody know how to do this easily?
You can wrap your test statement in a function and pass the range for the specific checkbox. Then set the return result can be as the value for the checkbox. The below needs to be modified for your form and ranges but will give the desired result.
'Function to test the value of the range
Private Function SetCheckBox(ByVal rng As Range)
If rng.Value = "X" Then
SetCheckBox = True
ElseIf rng.Value = "?" Then
SetCheckBox = Null
Else
SetCheckBox = False
End If
End Function
Private Sub UserForm_Initialize()
'Set value for each checkbox by passing its range to the function
CheckBox1.Value = SetCheckBox(Sheets("aux").Range("A1"))
CheckBox2.Value = SetCheckBox(Sheets("aux").Range("A2"))
CheckBox3.Value = SetCheckBox(Sheets("aux").Range("A3"))
CheckBox4.Value = SetCheckBox(Sheets("aux").Range("A4"))
End Sub
How can i format some cell value into something else using DevExpress.XtraGrid.StyleFormatCondition . For example I need to bold some value in one or more cell with same value and how to change only one column not entire row
Here it is my example:
For i As Integer = 0 To GridView1.DataRowCount
Dim a As String = (GridView1.Columns(0)).ToString
If a = "Vrsta računa" Then 'THE NAME OF COLUMN
Dim b = GridView1.GetRowCellValue(i, "accountsType") ' THE NAME OF CELL
If b = "KLASA" Then
Dim condition1 As StyleFormatCondition = New DevExpress.XtraGrid.StyleFormatCondition()
condition1.Appearance.BorderColor = Color.Blue
condition1.Appearance.BackColor = Color.Red
condition1.Appearance.Options.UseBackColor = True
condition1.Appearance.Options.UseBorderColor = True
'***** HOW TO BOLD CELL VALUE *******
condition1.Condition = FormatConditionEnum.Expression
condition1.ApplyToRow = False
condition1.Expression = "[accountsType] = 'KLASA'"
GridView1.FormatConditions.Add(condition1)
ElseIf b = "SINTETIKA" Then
Dim condition1 As StyleFormatCondition = New DevExpress.XtraGrid.StyleFormatCondition()
condition1.Appearance.BorderColor = Color.Blue
condition1.Appearance.BackColor = Color.HotPink
condition1.Appearance.Options.UseBackColor = True
condition1.Appearance.Options.UseBorderColor = True
condition1.Condition = FormatConditionEnum.Expression
condition1.ApplyToRow = False
condition1.Expression = "[accountsType] = 'SINTETIKA'"
' I WANT TO Have an effect on a cell , AND not on entire row
GridView1.FormatConditions.Add(condition1)
ElseIf b = "ANALITIKA" Then
Dim condition1 As StyleFormatCondition = New DevExpress.XtraGrid.StyleFormatCondition()
condition1.Appearance.BorderColor = Color.Blue
condition1.Appearance.BackColor = Color.Green
condition1.Appearance.Options.UseBackColor = True
condition1.Appearance.Options.UseBorderColor = True
condition1.Condition = FormatConditionEnum.Expression
condition1.ApplyToRow = False
condition1.Expression = "[accountsType] = 'ANALITIKA'"
GridView1.FormatConditions.Add(condition1)
End If
End If
Next
It looks like you are using Format Conditions not in a correct way.
There is no need to traverse through all rows and columns manually. Before painting each row, GridView automatically checks if it meets any condition in its FormatConditions collection. If so, GridView applies a corresponding style.
Please refer to the following help topic to learn how to use Format Conditions correctly:
https://documentation.devexpress.com/#WindowsForms/CustomDocument759
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
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