ListView_1_Event_Gadget isn't working

Just starting out? Need help? Post your questions and find answers here.
Beamernsw
New User
New User
Posts: 7
Joined: Sun May 05, 2024 6:42 am

ListView_1_Event_Gadget isn't working

Post by Beamernsw »

Hi all, my first time asking for help in a forum. I usually like to work things out myself or find answers in a forum, but this one has got me beat.
I am new to purebasic and finding it quite a bit different to the previous languages I've used (not many by the way, only a novice programmer).

I am writing a program to change the faction that players in my game are representing. Currently, I have a listview gadget that lists the multiplayer character names and a randomize button that randomly picks a faction with which the character has enough rep and applies it to each one. That has been working well.
Now I'm adding the ability to double click on a character name and manually choose the faction to represent. So my process is that once you double click on a character, that listview gadget (0) is hidden and another listview gadget (1), which is over top of the first one, is un-hidden and lists all the factions that the character has enough rep with to choose.
Oh btw, while the first listview is shown, as you click on each character an image gadget displays that character's faction uniform and a text gadget displays the faction name. What should happen, but doesn't, is when you click on the factions in the second listview, the image gadget and text gadget should update as it does with the first listview gadget.

The relevant code that I believe I have issue with is:

Code: Select all

Repeat
  myevent = WaitWindowEvent()
    Select myevent
      Case #PB_Event_CloseWindow
        End
       Case #PB_Event_Gadget
         Select EventGadget()
           Case Button_0
             If GetGadgetState(Option_0) = 1
               RandomizeAllS(EventType())
               Gosub Populate
             Else
               RandomizeAllM(EventType())  
               Gosub Populate
             EndIf
             SetGadgetState(ListView_0,0)
             For a = 0 To ArraySize(myListItemsSorted$())
               If GetGadgetItemText(ListView_0,(GetGadgetState(ListView_0))) = myListItems$(a)
                 myItem=a
                 FactionNames$ = myListName$(myItem)
                 FactionGroup$ = myListGroup$(myItem)
               EndIf
             Next
             Gosub mySelection
           Case Button_1
             End
           Case ListView_0_Event_Gadget
             For a = 0 To ArraySize(myListItemsSorted$())
               If GetGadgetItemText(ListView_0,(GetGadgetState(ListView_0))) = myListItems$(a)
                 myItem=a
                 FactionNames$ = myListName$(myItem)
                 FactionGroup$ = myListGroup$(myItem)
               EndIf
             Next
             Gosub mySelection
           Case ListView_1_Event_Gadget
             Restore FactionData
             For factionFinder = 1 To 48
               Read.s FactionGroup$
               Read.s FactionChest$
               Read.s FactionAcron$
               Read.s FactionNames$
               If FactionNames$ = GetGadgetItemText(ListView_1,(GetGadgetState(ListView_1)))
                 Break
               EndIf
             Next
             Gosub mySelection
           Case Option_0
             Gosub OptionChange
           Case Option_1
             Gosub OptionChange
         EndSelect
         If EventType() = #PB_EventType_LeftDoubleClick 
           Gosub SuitSelector
         EndIf 
   EndSelect
Until myevent = #PB_Event_CloseWindow
Or maybe the problem is in the form. The listview gadgets have different formats in here for some reason.
I've manually added listview_1 to the enumeration but it wipes it when run.

Code: Select all

Global Window_0

Global Button_0, Button_1, Text_1, Image_0, Container_0, Option_0, Option_1, Container_1, Text_2, Text_3, ListView_1

Enumeration FormGadget
  #ListView_0
EndEnumeration

Enumeration FormFont
  #Font_Window_0_0
  #Font_Window_0_1
  #Font_Window_0_2
  #Font_Window_0_3
EndEnumeration

LoadFont(#Font_Window_0_0,"Calibri Light", 12)
LoadFont(#Font_Window_0_1,"Agency FB", 37, #PB_Font_Bold)
LoadFont(#Font_Window_0_2,"Calibri", 12)
LoadFont(#Font_Window_0_3,"Calibri", 16, #PB_Font_Bold | #PB_Font_Italic)


Procedure OpenWindow_0(x = 0, y = 0, width = 700, height = 850)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Freelancer Body Swap v4.0", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  Button_0 = ButtonGadget(#PB_Any, 90, 480, 260, 30, "Randomize All")
  SetGadgetFont(Button_0, FontID(#Font_Window_0_0))
  Button_1 = ButtonGadget(#PB_Any, 450, 480, 220, 30, "Exit")
  SetGadgetFont(Button_1, FontID(#Font_Window_0_0))
  Text_1 = TextGadget(#PB_Any, 40, 10, 620, 70, "Freelancer Body Swap", #PB_Text_Center)
  SetGadgetFont(Text_1, FontID(#Font_Window_0_1))
  ListViewGadget(#ListView_0, 40, 160, 370, 270)
  SetGadgetFont(#ListView_0, FontID(#Font_Window_0_2))
  Image_0 = ImageGadget(#PB_Any, 510, 160, 150, 270, 0)
  Container_0 = ContainerGadget(#PB_Any, 40, 90, 370, 60, #PB_Container_Raised)
  Option_0 = OptionGadget(#PB_Any, 10, 10, 220, 20, "Load Single Player Save Games")
  Option_1 = OptionGadget(#PB_Any, 10, 30, 220, 20, "Load Multiplayer Characters")
  SetGadgetState(Option_1, 1)
  CloseGadgetList()
  Container_1 = ContainerGadget(#PB_Any, 440, 90, 220, 60, #PB_Container_Raised)
  Text_2 = TextGadget(#PB_Any, 0, 0, 220, 60, "", #PB_Text_Center)
  SetGadgetFont(Text_2, FontID(#Font_Window_0_3))
  CloseGadgetList()
  Text_3 = TextGadget(#PB_Any, 40, 440, 370, 30, "Double-Click to Choose Manually", #PB_Text_Center)
  ListView_1 = ListViewGadget(#PB_Any, 40, 160, 370, 270)
  SetGadgetFont(ListView_1, FontID(#Font_Window_0_2))
EndProcedure
I'm sure this is probably something simple and silly, but for the life of me, I can't get it to work even though I've experimented the crap out of it :)

Thank you in advance,
Beamer

Edit: I thought I'd expand slightly on the issue. I've added a debug line into the wait event for listviews 0 and 1, but nothing ever shows for listview 1 even though I can see and select each faction name in that listview.
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: ListView_1_Event_Gadget isn't working

Post by jacdelad »

Both codes are not runnable, so we can't know for sure where the problem is. Try reducing it to a minimum which shows the problem and post it.
Also: Use EnableExplicit (which you probably already do) and I would absolutely not use GoSub (use procedures instead).
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Beamernsw
New User
New User
Posts: 7
Joined: Sun May 05, 2024 6:42 am

Re: ListView_1_Event_Gadget isn't working

Post by Beamernsw »

I've cut down the program and removed what I could easily, but it does require a heap of external files to work. I've added them into a zip, Hopefully it won't be too large.
https://1drv.ms/u/s!Ai_6vJB-m7EGgoUID-3 ... A?e=Je8GWa

Also, I know it's a different issue, and I'm not too concerned at the moment, but the double click event isn't what I wanted. I've tried to make it work for just listview gadget 0 but this is the only way I could get it too work at all. Not too big an issue though, mainly just need the events to detect my clicking on listview gadget 1.

Thanks again, much appreciated.

Edit: Sorry, I fixed that second problem. Had a thought, searched the forum, problem fixed :)
I used: IsWindowVisible_(GadgetID(ListView_0))=1 in the double click bit

BTW, I'm just curious as to what's not good about gosubs? Still got soo much to learn.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ListView_1_Event_Gadget isn't working

Post by infratec »

You have to use EventType() inside of the gadget select and not in general.

Code: Select all

Case ListView_0_Event_Gadget
             
             If EventType() = #PB_EventType_LeftDoubleClick 
              Gosub SuitSelector
             EndIf
Last edited by infratec on Sun May 05, 2024 11:01 am, edited 1 time in total.
Beamernsw
New User
New User
Posts: 7
Joined: Sun May 05, 2024 6:42 am

Re: ListView_1_Event_Gadget isn't working

Post by Beamernsw »

OMG that's so obvious, it's embarrassing .
Thanks infratec.
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: ListView_1_Event_Gadget isn't working

Post by jacdelad »

Regarding the GoSubs: Procedures are much more structured and when using GoSubs you can easily get lost somewhere. Just my opinion (I never use any GoSub in PureBasic), others may give you other arguments.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ListView_1_Event_Gadget isn't working

Post by infratec »

The 'problem' with GoSub are the variables.

They are the same variables then in the calling part.
So you need to be very carefull not to mess something.
Beamernsw
New User
New User
Posts: 7
Joined: Sun May 05, 2024 6:42 am

Re: ListView_1_Event_Gadget isn't working

Post by Beamernsw »

Ahhh, thanks guys. I'm from the old school days of basic when you used to type your own line numbers and only had goto and gosub to rely on. Gosub saved so much typing since you could use it to repeat calls. No such thing as proceedures back then :)

Edit: Curious, can you structure a proceedure to accept variables and then call the proceedure with them.. Such as myProceedureX (var.1,var.2).
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ListView_1_Event_Gadget isn't working

Post by infratec »

Beamernsw wrote: Sun May 05, 2024 11:28 am Edit: Curious, can you structure a proceedure to accept variables and then call the proceedure with them.. Such as myProceedureX (var.1,var.2).
Of course.
You can also use a structure (pointer) as parameter.
Beamernsw
New User
New User
Posts: 7
Joined: Sun May 05, 2024 6:42 am

Re: ListView_1_Event_Gadget isn't working

Post by Beamernsw »

Hi, sorry for the "bump", but I was wondering if anyone had an idea regarding my initial problem. Why "Case ListView_1_Event_Gadget" section won't detect any change in gadget state.
I've made a couple of the suggested changes and re-zipped the files here --> https://1drv.ms/u/s!Ai_6vJB-m7EGgoUc2CA ... A?e=eQ93N4

Thanks again, Beamer.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: ListView_1_Event_Gadget isn't working

Post by spikey »

Beamernsw wrote: Fri May 10, 2024 10:37 am Why "Case ListView_1_Event_Gadget" section won't detect any change in gadget state.
You currently have two list view gadgets occupying the same space, neither is hidden in the design. One of them will end up invisible and inaccessible from the UI initially.

There are only two lines in the source which reference ListView_1_Event_Gadget, the declaration on line 13 and the second is the response on line 58. You never update its value, so the Case will never test true and the branch is never entered.

Oh and can I suggest that this is a really bad gadget naming convention. I know you've followed the form designer's default one but it does it this way solely because it can't really do anything else. Gadgets must have unique names and the designer can't know what your intended purpose for the form is.

Use window and gadget names which are meaningful to the purpose of the window/gadget not its sequence on the form. So, for example, List_Factions, Button_Randomize and Button_Exit are way more comprehensible when you are re-reading code, and especially, when someone else is trying to get up to speed with yours. By the time you get to List_16 and Button_78 on Window_20 you'll have no idea what anything does any more and you'll spend more time referring back to the form design than writing any code!
Last edited by spikey on Fri May 10, 2024 11:07 am, edited 1 time in total.
Beamernsw
New User
New User
Posts: 7
Joined: Sun May 05, 2024 6:42 am

Re: ListView_1_Event_Gadget isn't working

Post by Beamernsw »

Thanks for the response spikey. The naming convention thing makes perfect sense of course and I will use that from now on. As for not updating the ListView_1_Event_Gadget, I'm not quite sure how. I only have the same two lines for ListView_0_Event_Gadget but it seems to work. I just figured that clicking on an item in the list updated it...creating an event so to speak. I have previously seperated the two listview windows into their own area and kept both visible but it made no difference.
Also, I don't understand why the designer wrote them differently. I tried to manually make the second the same as the first, but it overwrote my changes.

Code: Select all

Enumeration FormGadget
  #ListView_0
EndEnumeration

ListViewGadget(#ListView_0, 40, 160, 370, 270)
SetGadgetFont(#ListView_0, FontID(#Font_Window_0_2))
compared to

Code: Select all

Global ListView_1

ListView_1 = ListViewGadget(#PB_Any, 40, 160, 370, 270)
SetGadgetFont(ListView_1, FontID(#Font_Window_0_2))
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: ListView_1_Event_Gadget isn't working

Post by spikey »

Beamernsw wrote: Sat May 11, 2024 8:35 am I only have the same two lines for ListView_0_Event_Gadget but it seems to work.
Just as you have a "Case Button_0" and "Case Button_1" I would expect you to have a "Case ListView_0" and a "Case ListView_1" in the "Select myevent" block (from line 25) but you do not.

I have to say I'm struggling to follow your logic here. I don't understand why you think there is a need for the ListView_0_Event_Gadget or ListView_1_Event_Gadget variables and what you are expecting them to do. (This will hamper my ability to assist).

I can say that there is no magic involved in the process, nothing "just happens". There must be code somewhere to make things happen. If the Form Designer doesn't provide it, then you must. In a similar way in which you set the values of FactionName$ and FactionGroup$ you would need to set the value of ListView_0_Event_Gadget and ListView_1_Event_Gadget if you expect them to have meaningful values later.

If ListView_0_Event_Gadget is currently working I'd guess that's because it works by default at present. I haven't step debugged to try and work out why this is the case because it would be too time consuming but I'm think its reasonable to say you can't expect it to stay that way.
Beamernsw wrote: Sat May 11, 2024 8:35 am Also, I don't understand why the designer wrote them differently. I tried to manually make the second the same as the first, but it overwrote my changes.
The form designer supports generating code using enumerations and also using variables and #PB_Any. You can specify which method you prefer via Menu → File → Preferences → Form. If this setting is in conflict with the method used in an open form it gets ... complicated. You should ensure that
the preference value is set the same to avoid this.

The properties panel is also not perfect and can glitch occasionally too.

In this case the '#PB_Any' property on 'ListView_0' has been cleared for some reason, reset it and save. This should cause the form designer to fix that particular problem.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: ListView_1_Event_Gadget isn't working

Post by spikey »

Beamernsw wrote: Sun May 05, 2024 8:13 am BTW, I'm just curious as to what's not good about gosubs? Still got soo much to learn.
Procedures encapsulate code providing a local variable scope and can subdivide code into reusable, manageable, functional components. This increases code readability and helps to constrain the "bug scope" in the event of a problem. A coherent and modular structure makes construction, debugging and maintenance easier. It's also easier to "onboard" new colleagues or when revisiting established code for changes after an absence of attention.

The use of Goto and Gosub does the exact opposite. It enlarges scope increasing the issues of name collision or duplication, inadvertent and erroneous changes, complicating code paths and reducing readability. It's the programming equivalent of using every archaic term you can think of and 8pt type in a contract. It's not technically incorrect but you can usually find something unpleasant concealed by the practice.

Unpleasantness in software is bugs. Unlike a two-sided contract, where one side can gain a competitive advantage over another by creating a complex contract to their own advantage, in software everyone loses by reducing manageability. New features take longer to develop and debug, bugs take longer to fix, learning curves are steeper and more likely to be problematic... The bigger the project, the worse the situation gets.

Debugging is always the most time consuming part of a big software project (as you're probably starting to grasp with your current problem). Anything which makes this easier = good. Anything which makes it harder = bad.
Beamernsw
New User
New User
Posts: 7
Joined: Sun May 05, 2024 6:42 am

Re: ListView_1_Event_Gadget isn't working

Post by Beamernsw »

Thanks again spikey, that cleared up a few things for me. I mostly copied and pasted example code from the help file and from this forum and then changed the code until I could make it work. I can't remember where I copied in the ListView_0_Event_Gadget bit from, but you were perfectly correct. I removed the _Event_Gadget from the end of those two Case lines and that fixed my problem. I then went and renamed all my gadgets to make sense and tidied up some other code. I decided to rearrange the proceedure in the designer so that all text gadgets were together and the listview gadgets as well, etc. But half of them dissapeared... LOL. I guess they like to stay in the order they were made. I put them back and all is good once more.

Again.....thank you very much,
I am now back on track to finish writing this little app.

Beamer.
Post Reply