Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

items in combobox as one long string

vbnewbiejoepvbnewbiejoep Posts: 39Member
Hi,

I have a flexgrid with a combolist that i want to populate with items from a combobox.
Now the problem is that the flexgrid combolist does not have the [code]combolist.add[/code] method, only [code]combolist = "item1|item2|item3"[/code].
(and i thought the flexgrid was more flexible than the microsoft datagrid :( )

Anyway, i need all the items from the combobox in one string, separated with "|", else it wont work.

Example:
I have a combobox with items: item1, item2, item3
now a want this:
[code]
Flexgrid1.combolist = "item1|item2|item3"
[/code]
Of course i dont know the items in the combobox at designtime.


Comments

  • DrMartenDrMarten Posts: 748Member
    [b][red]This message was edited by DrMarten at 2006-1-9 1:58:8[/red][/b][hr]
    : Hi,
    :
    : I have a flexgrid with a combolist that i want to populate with items from a combobox.
    : Now the problem is that the flexgrid combolist does not have the [code]combolist.add[/code] method, only [code]combolist = "item1|item2|item3"[/code].
    : (and i thought the flexgrid was more flexible than the microsoft datagrid :( )
    :
    : Anyway, i need all the items from the combobox in one string, separated with "|", else it wont work.
    :
    : Example:
    : I have a combobox with items: item1, item2, item3
    : now a want this:
    : [code]
    : Flexgrid1.combolist = "item1|item2|item3"
    : [/code]
    : Of course i dont know the items in the combobox at designtime.
    :
    :
    :

    Why not use a label and simply separate the output as follows>>

    label1.Text= item1 & CHR(124) & item2 & CHR(124) & item3

    or

    label1.Text= item1 & " " & CHR(124)& " " & item2 & " " & CHR(124)& " " & " " & item3

    Use the & character to build up a string. :-)

    Regards,

    Dr M.



  • vbnewbiejoepvbnewbiejoep Posts: 39Member
    [b][red]This message was edited by vbnewbiejoep at 2006-1-9 11:10:33[/red][/b][hr]
    : [b][red]This message was edited by DrMarten at 2006-1-9 1:58:8[/red][/b][hr]
    : : Hi,
    : :
    : : I have a flexgrid with a combolist that i want to populate with items from a combobox.
    : : Now the problem is that the flexgrid combolist does not have the [code]combolist.add[/code] method, only [code]combolist = "item1|item2|item3"[/code].
    : : (and i thought the flexgrid was more flexible than the microsoft datagrid :( )
    : :
    : : Anyway, i need all the items from the combobox in one string, separated with "|", else it wont work.
    : :
    : : Example:
    : : I have a combobox with items: item1, item2, item3
    : : now a want this:
    : : [code]
    : : Flexgrid1.combolist = "item1|item2|item3"
    : : [/code]
    : : Of course i dont know the items in the combobox at designtime.
    : :
    : :
    : :
    :
    : Why not use a label and simply separate the output as follows>>
    :
    : label1.Text= item1 & CHR(124) & item2 & CHR(124) & item3
    :
    : or
    :
    : label1.Text= item1 & " " & CHR(124)& " " & item2 & " " & CHR(124)& " " & " " & item3
    :
    : Use the & character to build up a string. :-)
    :
    : Regards,
    :
    : Dr M.
    :
    :
    Thanks for your help Dr M,
    but how must i do it? I don't know how manny items there are in the combobox and i dont know the items itself.
    so i could do something like:
    [code]
    Label1.Text = combobox1.items.item(1) & "|" & combobox1.items.item(2)
    [/code]
    But then again, the above code will only work if i know how manny items there are in the combobox at designtime.


  • DrMartenDrMarten Posts: 748Member
    Hello,

    Using an ARRAY if you have a VARIABLE number of items seems to be the answer. See below please.

    If you have not got the VB.Net help file discs, do you want them?
    If you use an array of type string then convert all other variables using the CSTR function.

    Eg.

    ThisStringsContents=CSTR(ThisVariableNoMatterWhatItsTypeIs)

    Why not use an array then and use the getupperbound method?
    This way you can determine the number of items at run-time. :-)
    You could fill the remainder of the array with empty or NULL strings.


    You can resize an array too using ReDim but use PRESERVE to keep the contents. :-)
    To explain if you do the following>

    Dim MyItems() As String
    ReDim Preserve Myitems(9) ' Size of array is 10 as counting starts at zero.

    'You could then use a variable for the number of items in your output or combobox or textbox.
    'Eg.

    Dim NumberOfOutputElements As Integer

    'Change the ARRAY size. :-)
    NumberOfOutputElements=12
    ReDim Preserve Myitems(NumberOfOutputElements)

    Dim Count As Integer
    Dim LimitOfItems As Integer
    LimitOfItems=MyItems.GetUpperBound(0)

    'Outputs text to TextBox1 of the array elements that are NOT empty
    'strings.
    For Count=0 to LimitOfItems
    If MyItems(Count)<>"" then TextBox1.AppendText=MyItems(Count) & "|"
    Next Count

    'For more on arrays see the sction below the following dashed line.
    ' I hope this helps?

    Regards,

    Dr M.

    ---------------------------------------------------------------------
    Visual Basic Language Concepts

    Declaring Array Variables
    See Also
    Arrays Overview | Array Usage | Advanced Features of Arrays | Arrays of Arrays | Collections as an Alternative to Arrays | Object-Oriented Programming in Visual Basic | Dim Statement | Byte Data Type | New | Short Data Type | Long Data Type | Preserve | Array.GetUpperBound Method | ReDim Statement | New
    Array variables are declared the same way as other variables, using the Dim statement. You follow the variable name with one or more pairs of parentheses to indicate that it is an array rather than a scalar (a variable containing a single value).

    To declare a single-dimensional array variable

    In your declaration, add one pair of parentheses after the variable name, as in the following example:
    Dim MySingleArray() As Integer
    To declare a multidimensional array variable

    In your declaration, add one pair of parentheses after the variable name and place commas inside the parentheses to separate the dimensions, as in the following example:
    Dim My4DArray(,,,) As Short ' Four-dimensional array.
    To declare a jagged array variable

    In your declaration, add as many pairs of parentheses after the variable name as there are levels of nested arrays, as in the following example:
    Dim MyJaggedArray()()() As Byte ' Array of arrays of Byte arrays.
    Initializing Arrays
    You can initialize an array variable as part of its declaration. You can do one of the following in the declaration statement:

    Specify the initial length of one or more of the dimensions in the parentheses following the variable name, without assigning an array object to the variable.
    Assign an array object to the variable, using the New clause. When you use a New clause, you must follow it with braces ({}), even if they are empty.
    Assign an array object to the variable and supply initial lengths in the New clause.
    Assign an array object to the variable and supply initial element values in the New clause. You can supply both lengths and values in the same New clause.
    If you specify dimension lengths in the parentheses following the variable name, you must use a subsequent assignment statement to assign an array object to the variable. The following sample declarations show valid and invalid syntax for a single-dimensional array variable:

    Dim BA(2) As Byte ' Currently contains Nothing (no object).
    Dim BA(2) As Byte = New Byte() ' INVALID (New after length specified).
    Dim BA() As Byte = New Byte() {} ' Empty Byte array object.
    Dim BA() As Byte = New Byte() ' INVALID (missing braces).
    Dim BA() As Byte = New Byte(2) ' INVALID (missing braces).
    Dim BA() As Byte = New Byte() {0,1,2} ' (0) through (2).
    Dim BA() As Byte = New Byte(2) {0,1,2} ' (0) through (2).
    You can initialize a multidimensional array variable in a similar manner. The following sample declarations show a two-dimensional array variable being declared as an Short array with 2 rows and 2 columns.

    Dim S2X2(1, 1) As Short ' (0) through (1), (0) through (1).
    Dim S2X2(1, 1) As Short = New Short(,) ' INVALID (New after lengths).
    Dim S2X2(,) As Short = New Short(,) ' INVALID (missing braces).
    Dim S2X2(,) As Short = New Short(1, 1) ' INVALID (missing braces).
    Dim S2X2(,) As Short = New Short(,) {} ' Empty array object.
    Dim S2X2(,) As Short = New Short(1, 1) {} ' Elements have default value.
    Dim S2X2(,) As Short = New Short(,) {{5, 6}, {7, 8}} ' Four elements.
    The first argument represents the rows; the second argument represents the columns. In the arrays in the preceding declarations, the index values for both the row and the column range from 0 through 1.

    When you initialize a jagged array variable, you can specify the dimension lengths only for the top-level array. The following sample declarations show valid and invalid syntax for an array of arrays of Byte elements:

    Dim JB(1)() As Byte ' Array of two arrays of Byte elements.
    Dim JB(1)(1) As Byte ' INVALID (can only specify top-level size).
    Dim JB(1)() As Byte = New Byte()() ' INVALID (New after lengths).
    Dim JB()() As Byte = {New Byte() {}, New Byte() {}} ' Empty arrays.
    Dim JB()() As Byte = {New Byte(1) {}, New Byte(1) {}} ' Default values.
    Dim JB()() As Byte = {New Byte() {5, 6}, New Byte() {7, 8}}
    In the last of the preceding declarations, JB is initialized to two elements, JB(0) and JB(1), each of which is initialized to a two-element Byte array, the first with element values 5 and 6 and the second with elements 7 and 8.

    Array Size Limits
    The length of every dimension of an array is limited to the maximum value of a Long data type, which is (2 ^ 64) - 1. The total size limit of an array varies, based on your operating system and how much memory is available. Using an array that exceeds the amount of RAM available on your system is slower because the data must be read from and written to disk.

    Note Because arrays are objects in Visual Basic .NET, it is important to distinguish between an array object and an array variable. An array object, once created, does not change its size or rank. An array variable, however, can have a succession of different arrays assigned to it during its lifetime, and these can be of different sizes and ranks.
    Resizing Arrays
    You can resize an array at any time by assigning a different array object to the same array variable, using either ReDim or a standard assignment statement. The new array object can have different dimensions, although it must retain the same rank. This helps you manage memory efficiently. For example, you can use a large array for a short time and then ReDim it to a smaller size. This frees up memory you no longer need.

    When you ReDim an array, its existing values are normally lost. However, you can retain them by including the Preserve keyword in the ReDim statement. For example, the following statement allocates a new array, initializes its elements from the corresponding elements of the existing MyArray, and assigns the new array to MyArray.

    ReDim Preserve MyArray(10, 20)
    In a multidimensional array, you can change only the last dimension when you use Preserve. If you attempt to change any of the other dimensions, a run-time error occurs. If you do not know the current size of a dimension, you can use the GetUpperBound method, which returns the highest subscript value for the dimension you specify.

    In the following example, the first line is valid, but the second line is not, because it attempts to change the first of two dimensions.

    ReDim Preserve Matrix(Matrix.GetUpperBound(0), Matrix.GetUpperBound(1) + 10)
    ReDim Preserve Matrix(Matrix.GetUpperBound(0) + 10, Matrix.GetUpperBound(1))
    See Also
    Arrays Overview | Array Usage | Advanced Features of Arrays | Arrays of Arrays | Collections as an Alternative to Arrays | Object-Oriented Programming in Visual Basic | Dim Statement | Byte Data Type | New | Short Data Type | Long Data Type | Preserve | Array.GetUpperBound Method | ReDim Statement | New



  • YoShIe66YoShIe66 Posts: 6Member
    [b][red]This message was edited by YoShIe66 at 2006-1-10 6:59:8[/red][/b][hr]
    If the info for the flexgid is coming from a populated combobox (I gather that is the case), then you could also just loop though the values on the combobox to build up the string for the flexgrid. Something like this should work fine:

    [code]dim sFlexdata as string = ""
    for x as integer = 0 to combo1.items.count-1
    sFlexdata += "|" & combo1.item(x).text
    next x

    'Then just strip off the first '|'
    sFlexdata = sFlexdata.substring(1)[/code]






  • vbnewbiejoepvbnewbiejoep Posts: 39Member
    : [b][red]This message was edited by YoShIe66 at 2006-1-10 6:59:8[/red][/b][hr]
    : If the info for the flexgid is coming from a populated combobox (I gather that is the case), then you could also just loop though the values on the combobox to build up the string for the flexgrid. Something like this should work fine:
    :
    : [code]dim sFlexdata as string = ""
    : for x as integer = 0 to combo1.items.count-1
    : sFlexdata += "|" & combo1.item(x).text
    : next x
    :
    : 'Then just strip off the first '|'
    : sFlexdata = sFlexdata.substring(1)[/code]
    :
    :
    Thanks for your help!
    Your code really helped me a lot.
    To be honest, i din't know i could use the "+" in sFlexdata += to just add more text to the sting :)

    Becouse i use vb.net i had to change the code just a little:
    [code]
    Dim levdata As String
    Dim n As Integer

    For n = 0 To ComboBox1.Items.Count - 1
    levdata += "|" & ComboBox1.Items.Item(n)
    Next n

    levdata = levdata.Substring(1)
    [/code]

    Well, this works great, although it is not a very proffesional way to populate a combolist. (poor flexgrid)
    Have only one more question:
    What if the populated combobox is very big. I mean, what if i have thousand or more items? Could they all fit in just one simple string?


  • YoShIe66YoShIe66 Posts: 6Member
    : : [b][red]This message was edited by YoShIe66 at 2006-1-10 6:59:8[/red][/b][hr]
    : : If the info for the flexgid is coming from a populated combobox (I gather that is the case), then you could also just loop though the values on the combobox to build up the string for the flexgrid. Something like this should work fine:
    : :
    : : [code]dim sFlexdata as string = ""
    : : for x as integer = 0 to combo1.items.count-1
    : : sFlexdata += "|" & combo1.item(x).text
    : : next x
    : :
    : : 'Then just strip off the first '|'
    : : sFlexdata = sFlexdata.substring(1)[/code]
    : :
    : :
    : Thanks for your help!
    : Your code really helped me a lot.
    : To be honest, i din't know i could use the "+" in sFlexdata += to just add more text to the sting :)
    :
    : Becouse i use vb.net i had to change the code just a little:
    : [code]
    : Dim levdata As String
    : Dim n As Integer
    :
    : For n = 0 To ComboBox1.Items.Count - 1
    : levdata += "|" & ComboBox1.Items.Item(n)
    : Next n
    :
    : levdata = levdata.Substring(1)
    : [/code]
    :
    : Well, this works great, although it is not a very proffesional way to populate a combolist. (poor flexgrid)
    : Have only one more question:
    : What if the populated combobox is very big. I mean, what if i have thousand or more items? Could they all fit in just one simple string?
    :
    :
    :

    Well personally, I'd stay away from flexgrid (unless you're working in VB6 or earlier). The datagrid that ships with VB.Net 2003 works just fine for whatever you need, plus, you can bind a datasource directly to it. That makes populating it very simple. If you built a datatable of entries, you could then use it in both the combobox and the datagrid without changing any of the data itself simply by databinding.

    As for having a lot of combobox entries translate over to a string, I have yet to see a string variable truncate (I once dumped a 2k textfile into one). Only problem I forsee is the overhead you'll use in looping through each entry (if there were say... 4000+) with the FOR...NEXT loop.
  • vbnewbiejoepvbnewbiejoep Posts: 39Member
    [b][red]This message was edited by vbnewbiejoep at 2006-1-10 15:25:51[/red][/b][hr]
    [b][red]This message was edited by vbnewbiejoep at 2006-1-10 15:25:1[/red][/b][hr]
    : Well personally, I'd stay away from flexgrid (unless you're working in VB6 or earlier). The datagrid that ships with VB.Net 2003 works just fine for whatever you need, plus, you can bind a datasource directly to it. That makes populating it very simple. If you built a datatable of entries, you could then use it in both the combobox and the datagrid without changing any of the data itself simply by databinding.
    :
    : As for having a lot of combobox entries translate over to a string, I have yet to see a string variable truncate (I once dumped a 2k textfile into one). Only problem I forsee is the overhead you'll use in looping through each entry (if there were say... 4000+) with the FOR...NEXT loop.
    :

    With the DataGrid you cant have a combobox in it. With FlexGrid you can. With FlexGrid you can bind a datasource also directly.
    [code]FlexGrid1.SetDataBinding(oDataSet, "ProductenTable")[/code]
    But you said it would be possible to bind the Combolist in the FlexGrid with the dataset?
    i use this:
    [code] 'open connection
    oConnection.Open()

    'clear DataSet
    oDataSet.Clear()

    'fill Dataset with data from .mdb
    Try
    oDataAdapter.Fill(oDataSet, "ProductTable")
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try

    'Bind FlexGrid to DataSet
    FlexGrid1.SetDataBinding(oDataSet, "ProductTable")

    'Sluit connection
    oConnection.Close()
    [/code]
    I really like to know how to bind the combolist (or must i bind the column where the combolist is in?)





Sign In or Register to comment.