Select items from a combobox containing a specific word in vb6 - combobox

My problem is that I have the following combobox in my VB6 application:
For Each sectionNodeTCbte In sectionsTCbte
idTC = sectionNodeTCbte.selectSingleNode("Id").Text
cmbTipoCbte.AddItem (sectionNodeTCbte.selectSingleNode("Desc").Text)
cmbTipoCbte.ItemData(cmbTipoCbte.NewIndex) = idTC
Next
This combobox, brings me:
Document 1
Document 2
StyleSheet 1
StyleSheet 2
Document 3
I need to show only those with the word "Document". I should use an if? Or how i could solve, any ideas?
Thank you for readme and sorry for my English!

Try this If using an additional variable:
Dim description As String
For Each sectionNodeTCbte In sectionsTCbte
description = sectionNodeTCbte.selectSingleNode("Desc").Text
If InStr(description, "Document") > 0 Then
idTC = sectionNodeTCbte.selectSingleNode("Id").Text
cmbTipoCbte.AddItem description
cmbTipoCbte.ItemData(cmbTipoCbte.NewIndex) = idTC
End If
Next

Two suggestions, if they matter in your case
If it's possible to have "document 1" in your list, as opposed to "Document 1", you should use vbTextCompare to ignore the case.
If you just want items that start with "Document" you can check that InStr() = 1. Using InStr() > 0 would be true for "This Document", which you may not want.
If InStr(Description, "Document", vbTextCompare) = 1 Then

Related

Covert a String to an Object which has a variable name

I searched for two hours, here on Stackoverflow and on other forums. But i can't find a solution for my problem. I have to be able to change the properties of an object that has a name that I don't know. Now i try to explain better:
The user drag some files in a form, and i get in a array() all the paths of the dragged files. For each path in files() i add to a panel a usercontrol that is the interface of an uploader. (My application it's kind of an uploader). Good, imagine that the user dragged 4 files, i have 4 different usercontrols, named "Uploader1", "Uploader2", "Uploader3" and "Uploader4". I need to change the text of a label in the uploader1, but i can't write:
Uploader1.LabelExample.Text = "Example"
Becouse it doesn't exist! (Not yet!)
So i tryed this method.
Dim UploadCounter as Integer = 1
Dim CurrentUploader = CType(Panel.Controls("Uploader" & UploaderCounter.ToString), UploadBanner)
CurrentUploader.LabelExample.Text = "Example"
I write the same with DirectCast and TryCast, but nothing.
I try also:
For Each Uploader With{.Name = "Uploader1"} as UploaderControl in Panel.Controls
Uploader.LabelExample.Text = "Example"
Next
I searched everywhere for "convert string to an object in vb.net" but i can't find anything that work! They all return "System.NullReferenceException: 'Object reference not set to an instance of an object.'"
Sorry for my bad bad english, thanks for all that will help me! need really!

Check if a string contains all other strings

I am trying to code a part of a software where I try to show the results that match search criteria.
I have a textbox where I can type one or more words I want to search and a listview that contains 4 different columns and a dozen rows. The idea is that each listview row contains lots of words and I want to see only the rows that contain all the words I have typed in the textbox. I have finished the code that searches for one term only. The problem I am having is that I don't fully understand how to do the same, but using multiple terms instead of one term only.
In the textbox, I write the words I want to search separated by a space. I have a variable where I keep the whole content of the listview row separated by : (example => col1row1content:col1row2content:col1row3content,etc). Summarizing, I want to check if a string (the full content of a row) contains all other strings (each word I have typped in the textbox).
This is the code I have implemented:
Dim textboxFullContentArray As String() = textboxSearch.Split(New Char() {" "c})
Dim Content As String
Dim containsAll As Boolean = False
Dim wholeRowContent(listviewMain.Items.Count - 1) As String ' each index of the array keeps the entire row content (one array contains all 4 cells of the row)
' wholeRowContent contains in one index the entire content of a row. That means,
' the index contains the 4 cells that represent an entire row.
' The format is like "rowData1:rowData2:rowData3:rowData4" (omitted for simplicity)
For Q As Integer = 0 To listviewMain.Items.Count - 1
For Each Content In textboxFullContentArray
If wholeRowContent(Q).ToLower.Contains(Content) Then
containsAll = True
' rest of the code...
ElseIf Not wholeRowContent(Q).ToLower.Contains(Content) Then
containsAll = False
Exit For
End If
Next
Next
But of course, this code is showing false positives and I think it's not a good solution. I think it must be much easier and I am overcomplicating the concept.
I am using VB.Net 2013
You can determine whether a String contains all of a list of substrings with a single line of code:
If substrings.All(Function(s) str.IndexOf(s, StringComparison.OrdinalIgnoreCase) >= 0) Then
Notice that I have actually implemented a case-insensitive comparison, rather than using ToLower or ToUpper.
It may not seem as neat to call IndexOf rather than Contains but guess what: Contains actually calls IndexOf internally anyway:
public bool Contains(string value)
{
return this.IndexOf(value, StringComparison.Ordinal) >= 0;
}
You can write your own extension methods if you want a case-insensitive Contains method:
<Extension>
Public Function Contains(source As String,
value As String,
comparisonType As StringComparison) As Boolean
Return source.IndexOf(value, comparisonType) >= 0
End Function
Your If/Else looks like it could be simplified. I would set your containsAll value to true outside the nested loops, and only if you encounter a "Content" in "textboxFullContentArray" that is not contained in wholeRowContent(Q) you set containsAll to false, otherwise do nothing.
Also, one way to see what's going on is to print statements with the values that are being compared throughout your function, which you can read through and see what is happening at runtime when the false positives occur.
After some hours looking for a simple and effective solution (and trying different codes), I have finally found this solution that I adapted from: Bad word filter - stackoverflow
For Q As Integer = 0 To listviewMain.Items.Count - 1
If textboxFullContentArray.All(Function(b) wholeRowContent(q).ToLower().Contains(b.ToLower())) Then
' my code
End If
Next

Allow edits from different users appear in different color inside memo box : Microsoft Access 2010

I have a memo field which contains rich text. I am able to identify a user and change all the text in the box instead of just the text they added.
I am looking to write code which allows the text to be edited and after update , the edited text will appear a different color than the original text in the memo field.
I have tried :
Dim strNew As String
Dim strOld As String
If Me.txt_username_id = "grant" Then
strOld = Me.Form!txtnotesaboutproduct1.OldValue.ForeColor = vbBlack<br/>
strNew = Me.Form!txtnotesaboutproduct1.ForeColor = vbRed
End If
I have also tried
Dim ctlOld As TextBox<br/>
Set ctlOld = Me.Form!txtnotesaboutproduct1
If Me.txt_username_id = "grant" Then
ctlOld = Me.Form!txtnotesaboutproduct1.OldValue.ForeColor = vbRed
End If
Generally, I do this with a continuous subform for Notes, so that I can hold the data, date and user, rather than just one formatted text box. Though I do realize this might be a lot more real estate that you might have, you can use a conditional format within the subform. I do agree that if it is possible, you'll likely need to use HTML and not .Forecolor, which will change the entire box.

Visual Basic Referencing .Net Objects In An Array

I have a big list of objects in the Design view of Visual Basic 2010 that I need to change a bunch of properties for, so of course I tried using an array rather than taking 50-60 lines for a repetitive task. But there seems to be an issue referencing to the object and it seems to be just taking the information from it. I know that was a shitty explanation, but maybe you'll understand it when you see it.
Dim objectsToClear As Array = _
{lblDailyRoundTrip, lblDaysWorked, lblFillBoxes, lblMilesPerGallon, lblMonthlyInsurance, _
lblMonthlyMaintenance, lblMonthlyParking, tbDailyRoundTrip, tbDaysWorked, tbMilesPerGallon, _
tbMonthlyInsurance, tbMonthlyMaintenance, tbMonthlyParking}
For i = LBound(objectsToClear) To UBound(objectsToClear)
objectsToClear(i).Text = ""
objectsToClear(i).Visible = False
Next
Try this instead:
Dim objectsToClear As Array = { lblDailyRoundTrip,
lblDaysWorked,
lblFillBoxes,
lblMilesPerGallon,
lblMonthlyInsurance,
lblMonthlyMaintenance,
lblMonthlyParking,
tbDailyRoundTrip,
tbDaysWorked,
tbMilesPerGallon,
tbMonthlyInsurance,
tbMonthlyMaintenance,
tbMonthlyParking }
For Each item In objectsToClear
item.Text = String.Empty
item.Visible = False
Next item
P.S. - you REALLY should have Option Strict On, and you should have strongly typed your array.
Since you seem to be interested in changing only .Text and .Visible properties, then you can just find the control by name, like this:
Dim returnValue As Control()
returnValue = Me.Controls.Find(objectsToClear(i), True)
Note: The True argument is for whether or not to search all children, which it sounds like you want to do. Read Control.ControlCollection.Find Method documentation for more information.
Now that you have a collection of controls that match the name you specified, loop through the controls in that collection and set the property values, like this:
For Each c As Control In returnValue
c.Text = ""
c.Visible = False
Next

Visual Studio - How i can use a variable into a object name?

i'm learning Visual, and i'm tryind to do this:
For Each folder In Dir.Subfolders
list = list + 1
C1CheckBox(list).Text = folder.Name
Next
I have a lot of checkboxex named C1CheckBox1, C1CheckBox2, C1CheckBox3, etc... then i want to change the text of each checkbox by the folder name (using the list var to reference the object)...
How i can do this?
thankyou for read
You can find controls by name with Controls.Find :
For Each folder In Dir.Subfolders
list = list + 1
Dim cb As CheckBox = Me.Controls.Find("C1CheckBox" & list, True)(0)
cb.Text = folder.Name
Next
This will search the entire form including its child containers. If you know all your checkboxes are in say, panel1, you could be more specific:
Dim cb As CheckBox = Me.Panel1.Controls.Find("C1CheckBox" & list, False)(0)
You can loop through all of the checkboxes setting their text as you go. See this answer for an example for how to enumerate the checkboxes

Resources