Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Jul 23, 2018 6:58 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Opening a window using a string
PostPosted: Thu Feb 02, 2012 10:11 pm 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
Does anyone know how to open a window with a string?

If I have the window's name as a string called "roomsavedlist", how do I open this window using the variable?

roomsavedlist.open doesn't work.....

window.open(roomsavedlist) doesn't work....

I've tried other combinations...no luck.....


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Thu Feb 02, 2012 11:59 pm 
Offline

Joined: Mon Oct 10, 2005 11:37 am
Posts: 42
I don't think you'll be able to do this unless the window is open. If it's open, you can use introspection just like in the previous post:

Dim count as Integer
Dim t as Introspection.TypeInfo

for count = 0 to (windowCount - 1)
t = Introspection.GetType(window(count))
if t.name = "roomsavedlist" then
Window(count).show
exit
end if
next


If it's not open, maybe you just call a method that goes through all the possibilities and opens the appropriate window? Like:

select case theWindowName
case "roomsavedlist"
roomsavedlist.show
case "someotherwindow"
someotherwindow.show
...
end select

_________________
Eric Andrews
infiniteline studios


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Fri Feb 03, 2012 12:32 am 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
The first one won't do me any good, because I'm trying to open that window with this code.

The 2nd code didn't work at all.


Hard to believe Real couldn't implement something as simple as roomsavedlist.show.

It should just resolve the string as text, and then form the complete command.


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Fri Feb 03, 2012 12:52 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Real Basic is not an interpreted language. It is a compiled language. There are trade-offs. One of them is the ability to magically transform a string into a variable or object. It just doesn't work that way in a compiled language. You have to do a little extra work. I would recommend a select case statement.
dim w as Window
select case roomsavedlist
case "entryWindow"
w = new entryWindow
case "chamberWindow"
w = new chamberWindow
case ...
...
end
w.Show


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Fri Feb 03, 2012 1:12 am 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
I get:

There is no class with this name. w = new entryWindow.

Same goes for w = new chamberWindow if I comment out the above.


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Fri Feb 03, 2012 1:28 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
You should replace those with your own windows (and the strings you save), naturally.


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Fri Feb 03, 2012 1:32 am 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
I have around 100 windows, so that doesn't seem practical at all.....


Just on a test project with only two windows where I replaced them with my names, it still crashes on the w.show.


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Fri Feb 03, 2012 1:41 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
nilesmitchell wrote:
I have around 100 windows, so that doesn't seem practical at all.....

Practical or not, it's about the only way.

Quote:
Just on a test project with only two windows where I replaced them with my names, it still crashes on the w.show.

Did you set roomsavedlist to one of the values?


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Fri Feb 03, 2012 4:40 am 
Offline
User avatar

Joined: Thu Sep 10, 2009 2:50 am
Posts: 418
Location: Santa Cruz, CA, USA
nilesmitchell wrote:
I have around 100 windows, so that doesn't seem practical at all.....

In such situations you can take advantage of the xml format to extract the info and generate the source code for you. Here's a routine that opens an XML project and creates Tims code for all the windows.

dim f As FolderItem = GetOpenFolderItem("")  //open a RB XML project
if f = nil then return

dim doc As new XmlDocument(f) //parse the XML

//collect all the window name nodes
dim winList As XmlNodeList = doc.Xql("//block[@type='Window']/ObjName")

//build an easier to handle String array from the nodes
dim winNames() As String
for i As integer = 0 to winList.Length - 1
winNames.Append winList.Item(i).Child(0).Value
next

//build up the output source code
dim codeLines() As String
codeLines.Append "Sub showWindowWithName(roomsavedlist As String)"
codeLines.Append " dim w As Window"
codeLines.Append " select case roomsavedlist"
for i As integer = 0 to winNames.Ubound
codeLines.Append " case """ + winNames(i) + """"
codeLines.Append " w = new " + winNames(i)
next
codeLines.Append " end"
codeLines.Append " w.show"
codeLines.Append "End Sub"

//join the codeLines into a single finished string
dim methodText As String = Join(codeLines, EndOfLine)

dim c As New Clipboard //store result on clipboard
c.SetText(methodText)
c.Close
MsgBox "done" //give some feedback

To use...
'save as' your project to XML format.

put the above code into a pushbutton, run, click the button and choose the XML project.

when the Msgbox comes up the method source has been put on the clipboard.

go to an appropriate place in your original project and paste the method.

use the method, ie showWindowWithName("someWindowName")


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Fri Feb 03, 2012 9:38 am 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
Doofus, that is some mighty fine code!

Thank you!


Timhare, thank you too. I know why w.show was crashing. I got the same thing on Doofus's code.

I was using "readall" for my textinputstream. It was adding some blank characters after the room name, which was making the variable roomsavedlist wrong.

I switched it to "readline" and everything thing works great now.

Thanks to everyone for helping me with this!


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Fri Feb 03, 2012 11:44 pm 
Offline

Joined: Mon Jan 17, 2011 4:51 pm
Posts: 131
Interesting.

I found that although this method worked great for loading the window based on a string, the window itself did not behave normally. It would not respond to code that closes the window. So, if in the program, you moved to another window, the code I had to close the outgoing window would be ignored. It's almost like it was launched in a different way. I'm not familiar with the case command and how it works....might have something to do with that.

Real suggested I use something as simple as

if roomsavedlist = "Lobby" then
lobby.show

I would just have to do that for every window I had.....so.....

I slightly modified Doofis code to do this for me.

I replaced the middle part with this

//build up the output source code
dim codeLines() As String
for i As integer = 0 to winNames.Ubound
codeLines.Append "if roomsavedlist = """ + winNames(i) + """" + " then"
codeLines.Append winNames(i) + ".show"
codeLines.Append "end"
next


And that now works perfectly, seems cleaner, and most importantly, the window responds to the code closing it!

Your code is very flexible, Doofus. I like it a lot!


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Sat Feb 04, 2012 12:28 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
You're using implicit instances of the windows. It is an approach that simplifies certain coding tasks, but be aware that it is somewhat limiting in the long run.


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a window using a string
PostPosted: Sat Feb 04, 2012 8:32 am 
Offline

Joined: Fri Sep 30, 2005 11:19 am
Posts: 275
timhare wrote:
Real Basic is not an interpreted language. It is a compiled language. There are trade-offs. One of them is the ability to magically transform a string into a variable or object. It just doesn't work that way in a compiled language. You have to do a little extra work.


You can use introspection to more or less magically transform a string into a variable containing an object.
I doubt compiled vs interpreted really has that much to do with the ability to get variables and/or objects from strings, even though it might be easier to implement in interpreted languages.

_________________
"If you look carefully at my lips, you'll realize that I'm actually saying something else."
Brainswitch Machina - REALStudio/REALbasic
RBScriptExtender - give RBScript power to work with any class


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group