Results 1 to 20 of 20

Thread: listbox to richtextbox

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    5

    Unhappy listbox to richtextbox

    Hi All. This is my first post here at VBForums.
    VB 2013. I have a listbox populated with .rtf files. When clicking a filename I want that file to show up in my richtextbox.
    I had it working in VB6 long time ago, but now I can not get it to work.
    Please help. It must be very simple.

    Here the listbox is populated with the specific .rtf files, and it work just fine.
    ListBox1.DataSource = IO.Directory.GetFiles("C:\MiniTanks\RTF", "*.rtf").Select(Function(file) IO.Path.GetFileName(file)).ToList

    Then the hard part, not working.
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    rtb3.LoadFile = ListBox1.SelectedItem.ToString()

    Please.
    My Best // Gert Persson, Malmö Sweden

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: listbox to richtextbox

    It's probably because LoadFile isn't a property, but a method.
    https://docs.microsoft.com/en-us/dot...System_String_

    Tad bit surprised VS didn't bark at you.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: listbox to richtextbox

    The problem also is ListBox1.SelectedItem is the filename. You need the full path

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    5

    Re: listbox to richtextbox

    Thx for answering.

    This easy it was. Lol

    rtb3.LoadFile(FilPath + FilNamn)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: listbox to richtextbox

    I would suggest using DirectoryInfo instead of Directory. You can then get a list of FileInfo objects, which contain both name and path, to bind to your ListBox:
    vb.net Code:
    1. Dim folder As New DirectoryInfo("C:\MiniTanks\RTF")
    2. Dim files = folder.GetFiles("*.rtf")
    3.  
    4. With ListBox1
    5.     .DisplayMember = "Name"
    6.     .ValueMember = "FullName"
    7.     .DataSource = files
    8. End With
    then:
    vb.net Code:
    1. rtb3.LoadFile(CStr(ListBox1.SelectedValue))

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    5

    Re: listbox to richtextbox

    Thanks for intresting code.
    I will test that code later.
    Best // Gert

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: listbox to richtextbox

    Quote Originally Posted by jmcilhinney View Post
    vb.net Code:
    1. rtb3.LoadFile(CStr(ListBox1.SelectedValue))
    I think my eye slightly twitched a bit when I saw CStr.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: listbox to richtextbox

    I think my eye slightly twitched a bit when I saw CStr.
    Lol, mine too. But I didn't want to poke the bear.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: listbox to richtextbox

    ListBox1.SelectedValue is an Object containing a string. Hence the cast...

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: listbox to richtextbox

    Quote Originally Posted by dday9 View Post
    I think my eye slightly twitched a bit when I saw CStr.
    Quote Originally Posted by wes4dbt View Post
    Lol, mine too. But I didn't want to poke the bear.
    LoadFile expects a String - requires a String with Option Strict On - and, as .paul. suggests, SelectedValue is type Object, because it can expose any type of object. As such, a cast or conversion is always required. Your choices would be:
    vb.net Code:
    1. rtb3.LoadFile(ListBox1.SelectedValue.ToString())
    2. rtb3.LoadFile(CType(ListBox1.SelectedValue, String))
    3. rtb3.LoadFile(CStr(ListBox1.SelectedValue))
    4. rtb3.LoadFile(DirectCast(ListBox1.SelectedValue, String))
    Personally, I NEVER call ToString on an Object reference that refers to a String. I keep that strictly for actual conversions from one type to another. DirectCast is actually the most correct here, because we are actually casting an Object reference to String as type String. Personally, I never use CType directly for anything. I either use DirectCast for a cast or some other method for a conversion, e.g. ToString or Convert.ToInt32. In cases like this one, I do use CType indirectly because it makes the code more succinct. That means that if I'm casting an Object reference to type String, Integer, Double, etc, then I will use CStr, CInt, CDbl, etc. In this case and those like it, the first two options above will work but I consider them less appropriate that the last two.

    In C#, I would write this:
    csharp Code:
    1. rtb3.LoadFile((string)ListBox1.SelectedValue);
    That is directly equivalent to the use of DirectCast in VB but, as I said, I use CStr and the like for brevity.

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: listbox to richtextbox

    I would’ve called ToString, I didn’t consider DirectCast in this situation. It’s just the Microsoft.VisualBasic reference that caused the (very opinionated) twitch
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: listbox to richtextbox

    Quote Originally Posted by dday9 View Post
    I would’ve called ToString, I didn’t consider DirectCast in this situation. It’s just the Microsoft.VisualBasic reference that caused the (very opinionated) twitch
    CStr has nothing to do with Microsoft.VisualBasic. It is a VB keyword, i.e. part of the VB language, which is why it turns blue in the code editor. Things like MsgBox, InStr, etc, are members of modules that are members of the Microsoft.VisualBasic namespace. There is no such module that CStr or CType or the like are part of. Note that calls to CStr, CType etc are actually inlined by the compiler, so they are more efficient than ToString as well.

    I guess that also begs the question, what would you have done if SelectedValue was exposing an ID of type Integer?

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: listbox to richtextbox

    Quote Originally Posted by jmcilhinney View Post
    I guess that also begs the question, what would you have done if SelectedValue was exposing an ID of type Integer?
    Convert.ToInt32 has an object overload:
    Code:
    If (MyListBox.SelectedValue Is Nothing) Then
        Return ' or something meaningful
    End If
    
    Dim id = Convert.ToInt32(MyListBox.SelectedValue)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: listbox to richtextbox

    Quote Originally Posted by dday9 View Post
    Convert.ToInt32 has an object overload:
    Code:
    If (MyListBox.SelectedValue Is Nothing) Then
        Return ' or something meaningful
    End If
    
    Dim id = Convert.ToInt32(MyListBox.SelectedValue)
    So, again, that's converting something that is already an Int32 to an Int32. You should cast when you're changing the type of the reference only and convert when changing the type of the object.

  15. #15
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: listbox to richtextbox

    On my side, when I tested you code, I used that :
    Code:
    CType(ListBox1.SelectedItem, IO.FileInfo).FullName
    so no need for a CStr or a .tostring. A bit different but work the same.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  16. #16

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    5

    Re: listbox to richtextbox

    Intresting discussion.
    Thx u all for spreding lights on this topics.
    Actually I only use this code.
    And that works just fine for me



    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load, MyBase.CursorChanged
    ListBox1.DataSource = IO.Directory.GetFiles("C:\MiniTanks\RTF", "*.rtf").Select(Function(file) IO.Path.GetFileName(file)).ToList
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged

    Dim FilNamn As String
    Dim FilPath As String

    FilPath = "C:\MiniTanks\RTF"
    FilNamn = (ListBox1.SelectedItem.ToString())

    rtb3.LoadFile(FilPath + FilNamn)

    End Sub
    I'm Swedish so FileName is FilNamn
    and FilePasth is FilPath

    And that works fine.
    In VB6 there was a FileList control as well as DirectoryList and Drive List.

    Thx ALL for contribute to this thread.
    Next broblem I have is about the DataGridView.
    Will start a new tread about it. Welcome to spread light on that topic too. Lol

    My Best // Gert Persson, Malmö Sweden

  17. #17
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: listbox to richtextbox

    You're Swedish? I love y'all's fish.

    (this is a pun, it goes by pastellfiskar in your neck of the woods)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  18. #18

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    5

    Re: listbox to richtextbox

    I dont get that dday9.

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: listbox to richtextbox

    Try this.

    instead of...

    Code:
    ListBox1.SelectedItem.ToString()
    Use...

    Code:
    ListBox1.Text

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: listbox to richtextbox

    Quote Originally Posted by Delaney View Post
    On my side, when I tested you code, I used that :
    Code:
    CType(ListBox1.SelectedItem, IO.FileInfo).FullName
    so no need for a CStr or a .tostring. A bit different but work the same.
    Writing code to use CType so that you don't have to use CStr is achieving nothing because, as I mentioned in post #10, CStr, CInt and the like are simply shortcuts to using CType, to make the code more succinct. You've succeeded only in making the code more complex and reduced the effectiveness of the data-binding.

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