Results 1 to 6 of 6

Thread: This might help someone...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    90

    Lightbulb This might help someone...

    I am new to VB.Net. And for some reason this took me awhile to figure out how to do. I basically have a Listbox being populated by a dataset. Well using the dataview. After it was populated for the selecteditem property of the listbox was something like system.data.datarow
    Something Like that.

    Here is alittle code to help get the text that is selected in the listbox after populating it with a dataset.

    <vbscript>
    Dim i As Integer
    dim sSelectedText as String
    i = lstData.SelectedIndex
    sSelectedText = lstData.GetItemText(lstData.SelectedValue)
    Call MessageBox.Show(sSelectedText)
    </vbscript>

    Hope this helps someone. It gave me a headache...but it seems that comes with trying to migrate from vb6 to .net

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    did this not work then ...
    VB Code:
    1. MessageBox.Show(ListBox1.SelectedItem)
    cuz that should show you the selected item also.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    90
    no ....
    instead of what was selected it returned.
    System.data.datarow

    or something like that.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you set the valuemember property of the listbox to the fieldname of the field you want to show then you can use SelecteValue. Otherwise you can use SelectedItem and cast it to a dataviewrow then pick whatever field you want to show.

    PS: The Valuemember and Displaymember properties are case sensitive.
    VB Code:
    1. 'using valuemember
    2. 'as long as the valuemember property is set like this:
    3. ListBox1.ValueMember = "record_Text"
    4. 'then this will work
    5. MsgBox(ListBox1.SelectedValue)
    6.  
    7.  
    8. 'using selecteditem
    9. MsgBox(DirectCast(ListBox1.SelectedItem, Data.DataRowView)("record_Text").ToString)
    10. 'this way doesn't matter if valuemember is set you just have to cast
    11. 'to a dataviewrow and pick your field
    Last edited by Edneeis; Aug 15th, 2003 at 01:32 PM.

  5. #5
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Originally posted by Edneeis
    PS: The Valuemember and Displaymember properties are case sensitive.
    A real good point.
    Also I expect Edneeis (as a guru) to use MessageBox.Show so that newbies like me learn better syntax of .NET.
    Last edited by Lunatic3; Aug 15th, 2003 at 02:13 PM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    [Insert Blushing Smiley Here]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width