Process: A node is added in a treeview control with node text = textbox1.text
I want to prevent addition of duplicate node i.e. say, if a node with text "ABC" is added then next time, a node with text "ABC" should not be added to treeview control.
I tried following methods but could not achieve desired result.
Method A)
Dim list As New ArrayList
list.Add(TextBox1.Text)
if list.Contains(Textbox1.Text) then
MsgBox("Use different name")
else
.....code to add node with text
end if
Method B)
if Treeview1.Nodes.Count > 0 then
For i = 0 to Treeview1.Nodes.Count
if Treeview1.Nodes(i).Text=Textbox1.Text then
MsgBox("Use different name")
end if
next
else
........code to add node with text
end if
I could not understand the solutions suggested for C# on this forum.
Any help will be really appreciated.
Thanks
Method A should work OK. You might have another error in your code (in the else section?). list should be declared static if it is in a function that's being called repeatedly, else it will be reset to new (cleared) each time.
Method B has a couple of errors: (1) the for statement should be For i = 0 to Treeview1.Nodes.Count - 1 (maybe use "for each"), and the else with the code to add the node should be after the msgbox statement. Also, method B only search for the root node(s) of the treeview. You would need to traverse the tree to check all the nodes.
If ListView1.Items.Count > 0 Then
For I = 0 To ListView1.Items.Count - 1
For Each LVL As ListViewItem In ListView1.Items
If ListView1.Items.Item(I).Index = LVL.Index Then
Continue For
Else
If ListView1.Items.Item(I).Text = LVL.Text Then
LVL.Remove()
End If
End If
Next
Next
End If
Related
I am making a To Do program. I have a checked list box there. I want to make every checked item deleted automatically. Here's the code that I am using to check if checked:
For i = 0 To CheckedListBox1.Items.Count - 1
If CheckedListBox1.GetItemChecked(i) Then
Else
End If
Next
How can I do that? Thanks
To remove Items from your ListBox, you can save items to be removed in your loop and then to delete it for example :
Dim itemsToRemove As New List(Of Object)
For i = 0 To CheckedListBox1.Items.Count - 1
If CheckedListBox1.GetItemChecked(i) Then
itemsToRemove.Add(CheckedListBox1.Items(i))
End If
Next
For Each item As Object in itemsToRemove
CheckedListBox1.Items.Remove(item)
Next
If you loop backwards then removing an item doesn't affect the part of the list you have yet to check
For i = CheckedListBox1.Items.Count - 1 to 0 Step -1
If CheckedListBox1.GetItemChecked(i) Then
CheckedListBox1.Items.RemoveAt(i)
End If
Next
This won't prevent the cross thread exception you're getting; for that we really need to know why you're accessing this Control from a thread other than the thread that created it
I am creating a macro where at some point it has to navigate through some folders and sub-folders (tree nodes with children) inside SAP
The main folder is expanded in the following code:
session.findById("wnd[0]/shellcont/shell").expandNode "2**000001"
The sub-folder I´m looking for is the following:
session.findById("wnd[0]/shellcont/shell").selectedNode = "251000001"
However this sub-folder may not exist.
I want to add an IF statement to check if it exists or a way to get the list of sub-folders and look for the one which has the text "Status 51".
I can recognize a GuiTree control, which is of type 2 (column tree).
To determine if a given node exists, if you know its key, you could use the following function that you call this way: If TreeNodeExistsByKey(session.findById("wnd[0]/shellcont/shell"),"251000001") Then...
Function TreeNodeExistsByKey(tree,nodekey)
On Error Resume Next
tree.GetHierarchyLevel(nodekey)
If Err.Number = 0 Then
result = True
Else
result = False
End If
TreeNodeExistsByKey = result
End Function
To get and iterate the children of a given node:
Set tree = session.findById("wnd[0]/shellcont/shell")
Set coll = tree.GetAllNodeKeys()
For i = 0 to coll.Length - 1
nodekey = coll.ElementAt(i)
Next
To get the text of a node by its key, use the method GetNodeTextByKey:
nodeText = tree.GetNodeTextByKey(nodeKey)
I am creating a form in Access which will be used as an order sheet for classroom materials. I have the available resources listed and a text box next to the resource where the user inputs the quantity they desire.
My VBA code checks to see if any entries have been made by using the following. (I am using Nz() to allow for Null results):
QuantCheck = Nz(Box1.Value, 0) + Nz(Box2.Value, 0) + Nz(Box3.Value, 0)
Where "QuantCheck" is the variable I am using in the IF statement which begins the workflow:
If QuantCheck > 0 Then
I would like to clean this up by using some kind of loop statement, however I am not able to extract the .value from a string. I would love something like the following which I could incorporate into a loop:
"Box"&VariableNumber.Value
From what I can tell, I am not able to use a string (concatenated or otherwise) as the base for the .value call.
It is interesting that there is a way to accomplish this when using a SQL statement. I have this elsewhere in the code which works nicely:
SQLStr = "INSERT INTO OrderRequests VALUES (cbSchool, txtName, Title" & x & ".caption, Box" & x & ")"
Here I have a variable "x" which increases with each loop to change the Title line, and the Box line.
Any help is appreciated.
I suggest you use the Tag property of the controls. Put "QuantCheck" in the Tag property of any control you want to include. Then
Function QuantitiesExist(frm As Form) As Boolean
Dim Ctrl As Control
Const sQUANTCHK As String = "QuantCheck"
For Each Ctrl In frm.Controls
If Ctrl.Tag = sQUANTCHK Then
If Nz(Ctrl.Value) > 0 Then
QuantitiesExist = True
Exit For
End If
End If
Next Ctrl
End Function
Now you get self documenting code
If QuantitiesExist(Me) Then
And when you add/delete/change controls, you don't have to edit your code. Just set up new controls with the proper tags.
You could loop through the control on the for checking the names and then if it is the one you wanted take an action on it, is this what you was thinking of?
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If Ctrl.Name = "TxtPath" Then ' "Box" & VariableNumber Then
MsgBox Ctrl.Value
End If
Next
I'm having a really strange issue with the DataGridView control in a VS2008 / .NET 3.5 winforms project. I have a simple form with a grid. In the form constructor I call a function to bind the grind to a DataTable, and then loop through the rows setting the background colour of the last cell to LightGrey and the cell itself to read-only if the column value is true. After the form finishes loading the code didn't work i.e. the cells are not set to LightGrey and are not read-only (even though when I step through the code I can see the properties being set). I then call the function again from a button, but this time the colour is changed to LightGrey and the cell is made read-only i.e. the code works.
CODE:
Dim dgr As DataGridViewRow
For i_DsRow As Integer = 0 To ds.Tables(0).Rows.Count - 1
dr = ds.Tables(0).Rows(i_DsRow)
For i_row As Integer = 0 To DgSearch.Rows.Count - 1
dgr = DgSearch.Rows(i_row)
If dr("DsColoumn1").ToString.ToUpper = dgr.Cells("DgColoumn1").Value.ToString.ToUpper Then
If Val(dr("Coloumn2").ToString) = 3 Then
dgr.Cells("SomeColomname").Value = dr("SomeColoumName2").ToString
If dgr.Cells("SomeColomname3").Value.ToString <> "" Then dgr.Cells("SomeColomname3").Value &= ", "
dgr.Cells("SomeColomname3").Value &= dr("SomeColoumName2").ToString
SetCellColor(dgr.Cells("SomeColomname"), dgr.Cells("SomeColomname3"))
End If
Exit For
End If
Next
Next
Private Sub SetCellColor(ByVal resultCell As DataGridViewCell, ByVal ColorCell As DataGridViewCell)
If resultCell.Value.ToString().ToUpper = "A".ToUpper Or resultCell.Value.ToString().ToUpper = "B".ToUpper Then
ColorCell.Style.BackColor = Color.FromName("Red")
ElseIf resultCell.Value.ToString().ToUpper = "C".ToUpper Or resultCell.Value.ToString().ToUpper = "D".ToUpper Then
ColorCell.Style.BackColor = Color.FromName("MediumSeaGreen")
Else
ColorCell.Style.BackColor = Color.FromName("Yellow")
End If
End Sub
Thanks for adding the code. I would suggest putting all formatting in the CellFormatting event of the DataGridView.
There are a couple of gotchas that you have to watch, which looking at your code shouldn't be a problem but to be aware of:
This runs for each cell in the grid. Therefore Database lookups are a really bad idea.
You do best to get values where possible from the eventargs parameter of the method
If you need a code sample, I believe there are some good c# examples (possibly vb to) this one for example.
Receiving an error on the following line, any suggestions?
If lstbxCharacters.ListCount = 0 Then
Else
End if
Error: Listcount is not a member of Windows.Forms.Listbox
Any method that I can call to check if the contents of a listbox is NOT empty?
Iv'e tried this instead now but it gives me a boolean error when the listbox gets populated with different methods
If lstboxCharacters.Datasource = "" Then
Else
<bunch of methods>
End if
Fixed
If lstbxCharacters.Items.Count > 0 Then
Else
End if
Thanks anyway :)
If lstbxCharacters.Items.Count > 0 Then
'data exists
Else
'NO data exists
End if
the list box has an Items collection that you can check. Google the control and msdn and it will show you all the members.