ListBox Control Function

Started by Pax Animo, Tue 17/10/2023 23:04:06

« previous - next »

Pax Animo

Hey there, AGS folks

I'm trying to figure out how to control multiple list boxes by turning them on and off via their ID. I've been experimenting with them for a while and looked through the manual, but I must admit I'm still a bit at a loss when it comes to GUI controls in AGS. I'm simply trying to do something as provided below:

Code: ags
function list_box_control(int lstb_id) {
  if (ListBox[lstb_id].visible) ListBox[lstb_id].visible = false;
  else ListBox[lstb_id].visible = true;
}

i've looked at GUIControl.Visible
bool GUIControl.VisibleGets/sets whether the GUI control is visible. This is true by default, but you can set it to false in order to temporarily remove the GUI control from the GUI.

While the control is invisible, it will not be drawn on the screen, and will not register clicks or otherwise respond to any user input.

Applies To

Inherited by the Button, InvWindow, Label, ListBox, Slider and TextBox.

Example:

btnSaveGame.Visible = false;will make the btnSaveGame button invisible.

See also: GUIControl.Enabled

But this only controls it by it's name not it's ID, arrhh it's one of those days.

Cheers in advance.
Misunderstood

Crimson Wizard

#1
Controls do not exist on their own, but are placed on a parent GUI. Therefore there's no global array of ListBoxes (or Buttons, etc), and if you are using indexes, then you would need to also supply a parent GUI's ID.

Code: ags
function list_box_control(int parent_id, int lstb_id) {
    GUIControl *control = gui[parent_id].Controls[lstb_id];
    ListBox *listbox = control.AsListBox;
    listbox.Visible = !listbox.Visible; // this will toggle its current value
}

But, could you explain more, why do you want to use IDs, and not names, what is the use case here?

Pax Animo

#2
You had me at what is the use case here?, i'm often thinking i know what i want to do but oh my do i get confused at times,

So I'm thinking of a way to control each list box via a single function as in my crazy mind i thought it might make it easier to control.

Something like

Code: ags
function list_box_control(int lstb_id, String bio_text) {
  if (ListBox[lstb_id].visible) ListBox[lstb_id].visible = false;
  else { 
    ListBox[lstb_id].visible = true;
    if (ListBox[lstb_id].SelectedIndex == 0) lCitizensBio.Text = bio_text;  //lCitizenaBio is obviosuly just a lable to print to from the string parameter
    if (ListBox[lstb_id].SelectedIndex == 1) lCitizensBio.Text = bio_text;  //
  }
}

I'm really sorry if it makes no sense, but thank you for your help

Oh this function will be called from a GUI button which has multiple buttons for various citizens and their bio's
Misunderstood

Khris

Here's how to toggle the visibility of a control:

Code: ags
void Toggle(this GUIControl*) {
  this.Visible = !this.Visible;
}

You can now do
Code: ags
  listBoxWhatever.Toggle();

I'm not sure how useful this is though, when dealing with multiple listboxes you'd usually want to explicitly set the visibility of each one.


As for accessing listboxes by their ID: each GUI has its own Controls[] array you can use for that:
Code: ags
  gBunchOLists.Controls[2].Visible = false;
  gBunchOLists.Controls[3].Visible = true;

Looking at your bio_text code, I think the best way to solve this is if you describe what your end goal is rather than your current process. Try explaining it in game design terms rather than AGS technical ones.

Pax Animo

#4
Quote from: Khris on Wed 18/10/2023 09:00:52Here's how to toggle the visibility of a control:

Code: ags
void Toggle(this GUIControl*) {
  this.Visible = !this.Visible;
}

You can now do
Code: ags
  listBoxWhatever.Toggle();

This is perfect for now thank you.

As for the bio_text code I've removed that part as I don't think it will be necessary for now.

On a side note your help introduced me to function overloading, I'm now able to control my GUI is a much neater way, thanks ever so much!
Misunderstood

SMF spam blocked by CleanTalk