Is there a way to trim letters of a string? Or Convert a string to an integer removing its letters from it?
Story:
I have in which I have bunch of options on an application and all of them add up points depending on the option selected by the user. I gave them values like hsq2 hsq4 hsq6 and now I need to be able to get the one that is selected and remove letters from it and add up values. If you know a better way of doing this please let me know also..
The better way would be to use XAML and databinding to a rich ViewModel that has separate properties for each item's label and its score value as an integer. Then a method on your ViewModel would perform the sum of selected values and return it for data binding to the Total Score display. Search for MVVM if you want to know more about that approach.
However, since it looks like you're building a WinForms app, you could use the Tag property of each element to store the integer value. That's how I would have done it 7 years ago.
There are better designs you could use to avoid the issue, but to extract the number from a string, you could use a regex (now you have two problems, as they say!).
Dim match = RegEx.Match("blah2", "\d+")
If match.Success Then
Dim val = CInt(match.Value)
' val = 2
End If
Related
Dim fontsize As Single = CSng(SynopsisTSCmbFontSize.Text)
rtbSynopsis.Font = New Font(SynopsisTSCmbFonts.Text, fontsize)
to change the fontsize to the value selected in a combo box, the value has to be of the Single type.
the combobox is populated with numbers entered at the design mode, ranging from 7-78. I know that these are entered as strings.
the error is :
I have tried a number of things to convert the text (which are numbers, no letters) from the combobox to single to no avail. try parse did not work, trimming did not work, first convert to INT or DBL, then to SNG did not work.
What is the correct syntax here?
I would have thought that it was pretty standard stuff to change the fontsize.
I found a solution : instead of populating the combobox at design time, I populated it at runtime where I had full control over the type.
Dim i As Single
For i = 5 To 70
SynopsisTSCmbFontSize.Items.Add(i)
TreatmentTSCmbFontSize.Items.Add(i)
Next
once the comboboxes are correctly populated, I can run the rest of the code with no errors
thank you all for your time!
I have tried to find an answer to this but have had no luck. I am using VB.NET and VisualStudio 2019
I have a listbox (lboxsectionnames)that has several different section names listed (as shown in the screenshot). I am trying to specify a specific row, and assign that row to a variable as text.
For this instance I am trying to get the first row text, however, I would like to be able to specify row # in future and get text as well.
Dim firstSectionName
lboxSectionNames.GetItemText(0)
firstSectionName = lboxSectionNames.SelectedIndex.ToString
ListBox Text Example
ListBox.Items is an ObjectCollection and can be accessed with your index like this
lboxSectionNames.Items(0).ToString()
or using SelectedIndex like this:
lboxSectionNames.Items(lboxSectionNames.SelectedIndex).ToString()
although you could also do the above like this:
lboxSectionNames.SelectedItem.ToString()
If I affect a full array to a ListBox, using ListBox1.List = [{1;2;3;4}] for example, the Listbox's .List items keep their correct type (here numbers), but if I use ListBox1.AddItem 5 or Listbox1.List(5) = 6 to set an individual item the type is automatically changed to String.
Sample Code:
Private Sub UserForm_Initialize()
ListBox1.List = [{1;2;3;4}]
ListBox1.AddItem 5
ListBox1.AddItem
ListBox1.List(5) = 6
End Sub
Later on, when comparing values, I get wrong results because numbers are not equals to text (5 <>"5").
Is there any easy (1) way to ensure the type of the list items is not converted to String?
(1) I know I can explicitly make the conversion to String, but I rather keep my values as numbers instead of "numbers-strored-as-text" in the listbox
I guess that will be impossible when using AddItem. According to https://learn.microsoft.com/en-us/office/vba/api/access.listbox.additem, the first parameter is a string, so everything you pass will be converted to a string.
Probably your best bet is to collect all items in an array and assign the array using ListBox1.List. Or you have to live with numbers stored as string...
Update
I mixed the pages up - link to the Access page was wrong.
Anyhow, Documentation is rather poor. It says "valid object" but doesn't define what that means. At least it is not possible to add a object - that throws a type mismatch.
Also, the documentation states that a Variant is returned, but it seems thatis not true - when I try to get the result, the compiler throws an error.
As a conclusion, I assume that AddItem converts everything to a string (and throws an error if that fails). So I still assume that you have to build up an array and assign it to List if you want to have real numbers.
I'm fairly new to using Visual Basic. And I have never worked with databases/datatables before now.
On my datatable (I hope that's the correct word) I have two columns that I want to store color values. I changed the data type to "System.Drawing.Color".
However it would appear I cannot simply type in the string (e.g: "Color.Black") to add it to the database. And when using an update SQL query and make it look for color values on the form, it wants to convert them to string.
Is it possible to store color values like this in this way or do I have use another method?
All colors are presented by a string on either "Color.Black" or "#000000", so the only thing you can do is convert it to a string when you put into DataTable, then convert it back to a color when you retrieve it from the Datatable
I have an array Newstr(20) When I fill it, I need to know how many of its indexes has been filled ? and find out the values.
I need to know how many of its indexes has been filled ?
Arrays don't keep that information. They only know how many spots you allocated. You have to track how many you assigned yourself. More than that, if you're working with a collection where you don't know how many items there will be, arrays are really the wrong choice in the first place. You should use a List(Of T) instead.
You could populate the array with a known string, then test for that string to see how many elements in your array are filled.
I would - however - suggest using an array list. You can get the number of elements added to the list from the Count property. This is the MSDN entry for Array Lists.
In order to find which of the elements have been filled, you can use a LINQ construction like this:
Dim input() = New String() {"abc", "def", "ghi", "", Nothing}
Dim output = input.Where(Function(i) Not String.IsNullOrEmpty(i)).ToArray
When you run this code, the output array will contain "abc", "def" and "ghi".
You can modify the selector of the Where to suit your preference if you're coding for a different type of array.
For instance the selector for Integer? will be:
input.Where(Function(i) (Not i Is Nothing) Or (i <> 0)).ToArray
Of course, you'll have to be coding in .NET 3.5+ in order to get access to LINQ.