String issue

DR00ME

Well-known member
Joined
Feb 6, 2004
Messages
169
Location
Finland
lstRGB.Items.Add(CStr(pixelColor.R) + " " + CStr(pixelColor.G) + " " + CStr(pixelColor.B))

As you can see I have added 3 separate values in the listbox.

Dim MyString As String

MyString = lstRGB.SelectedItem


lets say MyString is like "60 255 132" how do I split it in three parts ? one of them would be 60 second 255 and third 132... can you do it with split or substring ? it is 4am I cant think HELP! :)
 
Yes, use the Split method. Then convert each color into an
integer and create a Color structure.

Code:
Dim MyColors() As String
MyColors = MyString.Split(" "c)  Split MyString by space characters

Dim red, green, blue As Integer
red = Convert.ToInt32(MyColors(0))
green = Convert.ToInt32(MyColors(1))
blue = Convert.ToInt32(MyColors(1))

Dim MyColor As Color = Color.FromArgb(red, green, blue)
 
dim the string as an array , eg:
Code:
Dim MyString As String() = lstRGB.SelectedItem.Split(" ")
/// your values will be MyString(0) , MyString(1) and MyString(2)

Edit: grr beaten to the post ;) :p
 
now it works like this:

Dim MyArray() As String
Dim MyString As String

MyString = lstRGB.SelectedItem
MyArray = MyString.Split(" "c)

lblIndexSelect.BackColor = _
Color.FromArgb(CByte(MyArray(0)), CByte(MyArray(1)), CByte(MyArray(2)))


is there any difference between CByte and Convert.ToByte ?
 
Back
Top