Multiple possible additions to a listbox.

Started by KodiakBehr, Sat 08/09/2012 16:52:16

Previous topic - Next topic

KodiakBehr

This should be straightforward but I must have been at this too long.  I've got a listbox that starts mostly unpopulated.

The game is open-ended enough that the player can add entries to that listbox is any order.  How do I ensure that the correct actions for selecting a listbox item are linked to the right item added.

This is probably a clumsy way of explaining.  Let me clarify.

A player gets a phone number, so if I state:

listPhoneNumbers.InsertItemAt(2, "867-5309");

It won't show up in the listbox until there is a 0 and 1 entry.  That's no good to me.

If I instead state:

listPhoneNumbers.AddItem("867-5309");

It could end up in any spot, and I have no idea how to ensure that listPhoneNumbers.SelectedIndex==2 goes to THAT entry.

What do you think I should do?  (Besides get some sleep.)

Khris

#1
Keep the numbers in an array, then iterate through its elements to find out which one was selected.

Code: ags
// header 

enum Contact {
  ePNContact1,
  ePNContact2,
  ePNContact3,
  ...
};

// main script

String phone_number[15];

// in game_start

  phone_number[ePNContact1] = "867-5309";
  phone_number[ePNContact2] = "867-7834";
  ...

int GetContact() {
  int i;
  while (i < 15) {
    if (phone_number[i] == listPhoneNumbers.Items[listPhoneNumbers.SelectedIndex]) return i;
    i++;
  }
  return -1;
}

// call event
  Contact c = GetContact();
  if (c == ePNContact1) {
    ...
  }
  if (c == ePNContact2) {
    ...
  }


Edit: thanks geork, code corrected

KodiakBehr

Okay.  I'm finding this conceptually difficult to follow.  So instead of using if "(listPhoneNumbers.SelectedIndex==0){" I should use "if (c == ePNContact1){" instead?

Also, upon using that code it gives me a parse error at phone_number (line 16)?



geork

About the error: It should be
Code: AGS
phone_number[ePNContact1] = "867-5309";
not
Code: AGS
phone_number[ePNContact1] == "867-5309";
(the single and double equals signs are a programming ****  :P)

Conceptually speaking: Khris is linking the item's name to an "index" he has created, so when the list box is checked, it will be the item's name, not index, which matters. He creates his own "index" here:
Code: AGS
enum Contact {
  ePNContact1,
  ePNContact2,
  ePNContact3,
  ...
};

(which is the same as:)
Code: AGS
enum Contact {
  ePNContact1 = 1, //Wierdly the first one is 1, not 0
  ePNContact2 = 2, 
  ePNContact3 = 3,
  ...
};

Then he gives everyone of his phone_number[] variables a value:
Code: AGS
phone_number[ePNContact1] = "867-5309";

(again, same as:)
Code: AGS
phone_number[1] = "867-5309";]

Hence, when you want to check which phone number has been selected, you call GetContact() which iterates through all the phone_number variables (from phone_number[0] to phone_number[14]) and tries to find whether the string phone_number[number] = the string (i.e. name) of the currently selected list box item: If so it returns the current number
Code: AGS
if (phone_number[i] == listPhoneNumbers.Items[listPhoneNumbers.SelectedIndex]) return i; //return terminates the function too

So when Khris calls next:
Code: AGS
Contact c = GetContact();
he is checking whether the number returned corresponds with any of the items in Contact (e.g. if 1 is returned, c = ePNContact1 as ePNContact1 = 1)

and so, instead of checking 
Code: AGS
if (listPhoneNumbers.SelectedIndex==0])
you check
Code: AGS
if (c == ePNContact1){


Sorry if this is a bit long winded, or if you knew most of that already. But I hope this clears up any conceptual things you found difficult to follow!

Khris

Thanks for explaining it :)

If should be noted, in case that isn't clear, that the enum members can be named anything you want.

Also, all that code is maybe almost too elaborate for the situation.
To address your original question directly: just test for the number, not the index, as in:
Code: ags
  String n = listPhoneNumbers.Items[listPhoneNumbers.SelectedIndex];
  if (n == "867-5309") ...

KodiakBehr

Aw man, that's a much simpler solution.  Thank you both for taking the time to spell it out for me.

SMF spam blocked by CleanTalk