-
May 27th, 2021, 12:03 PM
#1
Thread Starter
New Member
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
-
May 27th, 2021, 02:02 PM
#2
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
-
May 27th, 2021, 06:03 PM
#3
Re: listbox to richtextbox
The problem also is ListBox1.SelectedItem is the filename. You need the full path
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 28th, 2021, 10:23 AM
#4
Thread Starter
New Member
Re: listbox to richtextbox
Thx for answering.
This easy it was. Lol
rtb3.LoadFile(FilPath + FilNamn)
-
May 28th, 2021, 10:32 AM
#5
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:
Dim folder As New DirectoryInfo("C:\MiniTanks\RTF")
Dim files = folder.GetFiles("*.rtf")
With ListBox1
.DisplayMember = "Name"
.ValueMember = "FullName"
.DataSource = files
End With
then:
vb.net Code:
rtb3.LoadFile(CStr(ListBox1.SelectedValue))
-
May 28th, 2021, 02:16 PM
#6
Thread Starter
New Member
Re: listbox to richtextbox
Thanks for intresting code.
I will test that code later.
Best // Gert
-
May 28th, 2021, 03:56 PM
#7
Re: listbox to richtextbox
Originally Posted by jmcilhinney
vb.net Code:
rtb3.LoadFile(CStr(ListBox1.SelectedValue))
I think my eye slightly twitched a bit when I saw CStr.
-
May 28th, 2021, 07:34 PM
#8
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.
-
May 28th, 2021, 07:51 PM
#9
Re: listbox to richtextbox
ListBox1.SelectedValue is an Object containing a string. Hence the cast...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 28th, 2021, 09:00 PM
#10
Re: listbox to richtextbox
Originally Posted by dday9
I think my eye slightly twitched a bit when I saw CStr.
Originally Posted by wes4dbt
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:
rtb3.LoadFile(ListBox1.SelectedValue.ToString())
rtb3.LoadFile(CType(ListBox1.SelectedValue, String))
rtb3.LoadFile(CStr(ListBox1.SelectedValue))
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:
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.
-
May 28th, 2021, 09:13 PM
#11
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
-
May 28th, 2021, 09:32 PM
#12
Re: listbox to richtextbox
Originally Posted by dday9
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?
-
May 29th, 2021, 09:26 AM
#13
Re: listbox to richtextbox
Originally Posted by jmcilhinney
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)
-
May 29th, 2021, 09:46 AM
#14
Re: listbox to richtextbox
Originally Posted by dday9
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.
-
May 29th, 2021, 11:33 AM
#15
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)
-
May 29th, 2021, 12:01 PM
#16
Thread Starter
New Member
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
-
May 29th, 2021, 12:07 PM
#17
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)
-
May 29th, 2021, 12:15 PM
#18
Thread Starter
New Member
Re: listbox to richtextbox
-
May 29th, 2021, 01:07 PM
#19
Re: listbox to richtextbox
Try this.
instead of...
Code:
ListBox1.SelectedItem.ToString()
Use...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 29th, 2021, 09:07 PM
#20
Re: listbox to richtextbox
Originally Posted by Delaney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|