Making the code better looking

Hi, I have the code below that based on the selection of the PopupMenu changes pages in the PagePanel in the other window.

Is there a way to make it better looking? I am writing all the code explicitly. Is this the right way to do it?

if PopupMenu1.SelectedRowIndex=0 then
  Window2.PagePanel1.SelectedPanelIndex=0
elseif PopupMenu1.SelectedRowIndex=1 then
  Window2.PagePanel1.SelectedPanelIndex=1
elseif PopupMenu1.SelectedRowIndex=2 then
  Window2.PagePanel1.SelectedPanelIndex=2
elseif PopupMenu1.SelectedRowIndex=3 then
  Window2.PagePanel1.SelectedPanelIndex=3
elseif PopupMenu1.SelectedRowIndex=4 then
  Window2.PagePanel1.SelectedPanelIndex=4
end

Thank you in advance.

Val

Please review documentation about case statements.

Alternative may be to put the page panels in an array on window open, so you can refer to them by index later.

Hi, all,

I just have re-written the code above with the following one.


Var n As Integer
n=PopupMenu1.SelectedRowIndex
Window2.PagePanel1.SelectedPanelIndex=n

It works and looks much better with 60 PopupMenu indices. Just for general education, I need to play and find other way to do the same.

Have a nice day, all,

Val

Select Case PopupMenu1.SelectedRowIndex Case 0 Window2.PagePanel1.SelectedPanelIndex=0 Case 1 Window2.PagePanel1.SelectedPanelIndex=1 Case 2 Window2.PagePanel1.SelectedPanelIndex=2 Case 3 Window2.PagePanel1.SelectedPanelIndex=3 Case 4 Window2.PagePanel1.SelectedPanelIndex=4 End Select

Hi, guys, again,

I have 59 PagePanels. On each panel I have a segmented button that switches between a text area and an image.

I am trying to read the page panel index and parse it into the button’s name.

For example, if the PagePanel1.SelectedPanelIndex=0, then it should send commands to the TextArea0 and Canvas0.

To achieve this, I’ve written the following code

[code]Var n As Integer
n=PagePanel1.SelectedPanelIndex

If segmentIndex = 0 Then
TextArea(n).Visible=True
Canvas(n).Visible=False
// code for when segment 0 is selected
Else
TextArea(n).Visible=False
Canvas(n).Visible=True
// code for when segment 1 is selected
End If

[/code]

Apparently, I am doing something essential wrong because all the code area is red from bugs.

Can someone point me what exactly I am doing wrong?

Thank you,

Val

Use the name of the actual control set.

HI, Tim,

Thank you for the advice. This is actually what I am ended doing. With 59 panels going through actual names is a pain.

I was hoping to automate this process some way.

Again, I appreciate your feedback.

Val

Did you make the controls into a contol set? That will allow you use an index instead of typing out the control names in a big select case statement.

Hi, Tim,

I have 59 page panels. Each page panel has a segment button with two segments (“text” and “image”), one text area and one canvas.

Since all these text areas and canvases are located in different page panels, I am not sure that I can set them as a control set.

Thank you again for your help.

Val

Sure you can. This is a classic example of when to use a control set. Make the control index the same as the panel index they are on, so you can use the panel index directly to index the control.

Window2.PagePanel1.SelectedPanelIndex = PopupMenu1.SelectedRowIndex
  • Don’t use the awful var because I don’t like it.
  • Give your variables good names.
  • You don’t need the intermediate variable at all.

Hi, Tim,

Thank you give me an example how to do it? I’ve never done sets before.

Is there a YouTube tutorial for that? Or maybe a written tutorial?

Thank you,

Val

Hi, Tim,

I have found this one in the documentation.

https://documentation.xojo.com/topics/user_interface/desktop/desktop_controls/control_sets.html

I will try to learn from it as much as I can.

Thank you again for suggesting this idea.

Val

Have I missed something?
In the original post,
Window2.PagePanel1.SelectedPanelIndex is always being set to the same value as the PopupMenu1.SelectedRowIndex

If that is the case, the whole code becomes one line:

Window2.PagePanel1.SelectedPanelIndex=PopupMenu1.SelectedRowIndex

Doesnt solve the 59 images thing obviously: control set is the right answer for that.

Hi, Jeff,

The following code is not a problem

 Window2.PagePanel1.SelectedPanelIndex=PopupMenu1.SelectedRowIndex

It reads the index of the selected menu in popupmenu1 on Window 1 and sets the PagePanel1 to the corresponding index.

I’ve got stuck how to pass that index to the segment button, text area and canvas.

I want by using the popupmenu to address the textfield and canvas.

Right now I have the following code in the segmented button



If segmentIndex = 0 Then
  TextArea5.Visible=True
  Canvas5.Visible=False
  // code for when segment 0 is selected
Else
  TextArea5.Visible=False
  Canvas5.Visible=True
  // code for when segment 1 is selected
End If

TextArea5.Top=64
TextArea5.Left=20
TextArea5.Width=560
TextArea5.Height=392
Canvas5.Top=100
Canvas5.Left=20
Canvas5.Width=540
Canvas5.Height=297

With 59 indices in the popup menu and 59 page panels, I have 59 segmented buttons, 59 textareas, and 59 canvases.

Going manually through each segmented button “press” event and changing which texture and canvas it will talk to, is a pain.

I am hoping that using a control set approach would simplify this process.

[img]

https://imgur.com/a/Lt03qUf

[/img]