Real Software Forums
http://forums.realsoftware.com/

Is it even possible to do this?
http://forums.realsoftware.com/viewtopic.php?f=10&t=47847
Page 1 of 1

Author:  timehacker11 [ Sat May 11, 2013 12:43 am ]
Post subject:  Is it even possible to do this?

If ListBox1.SelectedItem.ToString.Contains("szs") Then
MsgBox("You've selected a SZS File!")
ElseIf ListBox1.SelectedItem.ToString.Contains("brres") Then
MsgBox("You've selected a BRRES file!")
Else
MsgBox("Boo! No Nintendo files selected!")
End If


I am assuming it isn't possible like a lot of things aren't but are in single platform languages

Author:  taylor-design [ Sat May 11, 2013 1:59 am ]
Post subject:  Re: Is it even possible to do this?

timehacker11 wrote:
I am assuming it isn't possible like a lot of things aren't but are in single platform languages


I'm not sure what you mean by this last comment. What you're asking about has nothing to do with single vs. cross platform.

That said, there is no ListBox.SelectedItem method. So you would need to do something like:

Dim i As Integer = Listbox1.ListIndex
If i < 0 Then
MsgBox("Boo! No files selected!")
ElseIf ListBox1.Cell(i,0).InStr("szs") > 0 Then
MsgBox("You've selected a SZS File!")
ElseIf ListBox1.Cell(i,0).InStr("brres") > 0 Then
MsgBox("You've selected a BRRES file!")
Else
MsgBox("Boo! No Nintendo files selected!")
End If


You could use Extends to add functions like "SelectedItem" to ListBox, and "Contains" to string.

Author:  kermit [ Sat May 11, 2013 4:50 am ]
Post subject:  Re: Is it even possible to do this?

if the listbox is not multi select, then something like this will do it:


dim selection as string
dim answer as string
answer = "Boo! No Nintendo files selected!"
selection = listbox1.list(Listbox1.ListIndex)
if InStr(selection,"szs") > 0 then answer ="You've selected a SZS File!"
if InStr(selection,"brres")> 0 then answer ="You've selected a BRRES File!"
msgbox answer

Author:  Karen [ Sat May 11, 2013 9:12 am ]
Post subject:  Re: Is it even possible to do this?

Of course it is possible...

May I suggest you read the documentation on the listbox in language reference for the listbox API?

What you are asking shows you did not look there first.

You won't learn the language and the framework well enough to do anything significant without doing some homework.


- Karen

Author:  taylor-design [ Sat May 11, 2013 2:14 pm ]
Post subject:  Re: Is it even possible to do this?

kermit wrote:
if the listbox is not multi select, then something like this will do it:
...
selection = listbox1.list(Listbox1.ListIndex)
...


Always check for no selection, which is -1. Most ListBox methods that take an index will throw an OutOfBounds. ListBox.List will return the entire list as a string with -1, which is odd, and will create a subtle bug instead of an out right exception.

Actually, I tend to code even more defensively just in case a bug elsewhere ever returns a crazy result:
If ListBox1.Index < 0 Or ListBox1.Index >= ListBox1.ListCount Then
TakeSomeDefensiveAction...

Author:  timehacker11 [ Sun May 12, 2013 3:42 am ]
Post subject:  Re: Is it even possible to do this?

taylor-design wrote:
timehacker11 wrote:
I am assuming it isn't possible like a lot of things aren't but are in single platform languages


I'm not sure what you mean by this last comment. What you're asking about has nothing to do with single vs. cross platform.

That said, there is no ListBox.SelectedItem method. So you would need to do something like:

Dim i As Integer = Listbox1.ListIndex
If i < 0 Then
MsgBox("Boo! No files selected!")
ElseIf ListBox1.Cell(i,0).InStr("szs") > 0 Then
MsgBox("You've selected a SZS File!")
ElseIf ListBox1.Cell(i,0).InStr("brres") > 0 Then
MsgBox("You've selected a BRRES file!")
Else
MsgBox("Boo! No Nintendo files selected!")
End If


You could use Extends to add functions like "SelectedItem" to ListBox, and "Contains" to string.


Thanks for the code.

@Karen: I infact did look in the LR, but I'm so used to VB.NET where every piece of code is "Englishified". For example, VB.NET has the SelectedItem method, while RealBasic does not, atleast not by default.

@taylor design: I've just looked into the Extends method in the LR, and it looks amazing and super powerful. Will do some experimentation with it. Although where would I put it? In a class method?

Author:  charonn0 [ Sun May 12, 2013 3:52 am ]
Post subject:  Re: Is it even possible to do this?

Extension methods must be put into a module.

Author:  taylor-design [ Sun May 12, 2013 3:58 am ]
Post subject:  Re: Is it even possible to do this?

timehacker11 wrote:
@taylor design: I've just looked into the Extends method in the LR, and it looks amazing and super powerful. Will do some experimentation with it. Although where would I put it? In a class method?


Put Extends methods in a module. I usually name the module after the class being extended, i.e. "ListBoxExt".

Keep in mind that you can't override an Extends method in a subclass.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/