• Welcome to PlanetSquires Forums.
 

i'm in need to learn of a listbox function....

Started by veltesian, July 07, 2022, 04:36:11 AM

Previous topic - Next topic

veltesian

I recently started a new PRJ where I have a listbox that's populated with items
& I was wondering how to, with a mouseclick selecting 1 item,... then a textbox string
(that is below the listbox) be used to (when a main buttonclick on the form) alter/
change/replace/overwrite the selected item in the listbox. I tried various different ways
to attempt in solving this myself,.... but much to disappointment I could not figure it out.

example code that I tried:

   sub lb2
   Dim nIndex as wfxListBox.SelectedIndex
   nIndex = Form1.L1.SelectedIndex
     If Form1.L1.SelectedIndex > -1 Then
        nIndex = Form1.L1.Items.Add(Form1.tx.text)
   End If
   end sub

I look in realization that the above code is not right due to the error it showed when trying to
just compile it to check for errors.

so if anyone could be interested in helping me solve this .... thanks ahead of time


Bumblebee

#1
This works for me:

Function Form1_List1_Click( ByRef sender As wfxListBox, ByRef e As EventArgs ) As LRESULT
  Form1.Text1.Text = Form1.List1.SelectedItem.Text
  Function = 0
End Function


The reverse would be:


n = Form1.List1.SelectedIndex
Form1.List1.Item(n).Text = Form1.Text1.Text


This raises a question I forgot to ask:
Item and SelectedItem properties use the wfxListBoxItem object, so why can't SelectedItem.Text be assigned a new string?
The Help entry for Item and SelectedItem says "gets or sets".

veltesian

#2
special thanks to bumblebee for the help

this part works fine....

Function Form1_List1_Click( ByRef sender As wfxListBox, ByRef e As EventArgs ) As LRESULT
  Form1.Text1.Text = Form1.List1.SelectedItem.Text
  Function = 0
End Function

the reverse part does not work... which i was hoping would work. even if i put "Dim n as string" before...

n = Form1.List1.SelectedIndex
Form1.List1.Item(n).Text = Form1.Text1.Text

i just need to figure out how to overwrite a listbox item where a button click will take a string
from a textbox & update the listbox with the new item in the place where you selected in the listbox.

Bumblebee

#3
My bad, variable n is an integer.
Alternatively, you could do this:
Form1.List1.Item(Form1.List1.SelectedIndex).Text = Form1.Text1.Text

Here is a code snippet that might help. It "moves" a listbox item up or down, requiring a swap of information.
Function Form1_UpDown1_Click( ByRef sender As wfxUpDown, ByRef e As EventArgs ) As LRESULT
Form1.UpDown1.SelectNextControl(false)
If Form1.List2.SelectedIndex = -1 Then Exit function
dim a as string
dim as integer n,t,d
n = Form1.List2.SelectedIndex
if Form1.UpDown1.Delta = 1 then t = n - 1 else t = n + 1
If t = -1 Or t = Form1.List2.Items.Count Then Exit function
'move item up or down by swapping information
a = Form1.List2.SelectedItem.Text
d = Form1.List2.SelectedItem.Data32
Form1.List2.Item(n).Text = Form1.List2.Item(t).Text
Form1.List2.Item(n).Data32 = Form1.List2.Item(t).Data32
Form1.List2.Item(t).Text = a
Form1.List2.Item(t).Data32 = d
Form1.List2.SelectedIndex = t
Function = 0
End Function

Bumblebee

Wouldn't it be more elegant if we could just do:

Form1.List1.SelectedItem.Text = Form1.Text1.Text

But that property is read-only  :'(

veltesian

#5
I'd like to thank Bumblebee for their helping me with this ListBox
project. I have made use of a few lines of their code help they have provided.
There are still a few things with manipulation of the ListBox component
I have yet to understand.

1. In selecting an item in the listbox, a textbox would register the number of
the listbox index

2. In changing the number value of a text box could cause the listbox to
highlight the chosen index number

3. In typing a word of some kind in a separate other textbox & then selecting
an item in the listbox,.... then pressing a button could then change the item
selected to reflect the word that was entered in that certain textbox

I do apologize if what I'm asking for is being.... pushy/arrogant/hyper insistive/ETC

Bumblebee

This should perform the function for 1.
Note that the first item in a listbox has an index of zero.
Function Form1_List1_Click( ByRef sender As wfxListBox, ByRef e As EventArgs ) As LRESULT
  Form1.Text1.Text = Str(Form1.List1.SelectedIndex)
  Function = 0
End Function


This should perform the function for 2.
Function Form1_Text1_TextChanged( ByRef sender As wfxTextBox, ByRef e As EventArgs ) As LRESULT
  dim n as integer
  n = val(Form1.Text1.Text)
  if n < 0 or n > Form1.List1.Items.Count -1 then exit function
  Form1.List1.SelectedIndex = n
  Function = 0
End Function


This should perform the function for 3.
Function Form1_Button1_Click( ByRef sender As wfxButton, ByRef e As EventArgs ) As LRESULT
  Form1.List1.Item(Form1.List1.SelectedIndex).Text = Form1.Text2.Text
  Function = 0
End Function


veltesian

a very humble thanks to BumbleBee for lending the assist on this code help.
it means alot & i can stand a chance to learn much more & hopefully fully understand
how to code with winFBE. :D

Bumblebee

On second thought, it may be better to use the KeyPress event instead of TextChanged.
Glad I could help :)

Paul Squires

...just a quick note... there is a lot of work that can/will/should be done on the WinFormsX framework that underpins the WinFBE visual designer code output. WinFBE 3 update was always meant to be a two step approach. Step 1 was to rewrite the code editor portion which is now complete and released. Step 2 is to enhance and refine the visual designer. That step is my next major endeavour to tackle.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Bumblebee

How should WinFormsX be conceptualized?

For a novice programmer like me, it appears to be an extension to the FreeBasic language.
And the AFX framework presents itself as a human readable version of the Windows API.

Behind the scenes, there is plenty of FB programming that make this possible, correct?

Paul Squires

WinFormsX is my attempt to make a kind of object oriented overlay to the WinAPI. My old FireFly designer taught me an important lesson that people tend to shy away from pure WinAPI programming no matter how much you try to simplify it. Visual Basic was hugely successful because it abstracted away the WinAPI but still allowed you access to it if you desired it. In hindsight, I should have used a pointer approach rather than a "dot" syntax because using references in FB is not as versatile as using pure pointers. Oh well, that ship has sailed so I will have to double down on refining the dot syntax.

Jose's AFX (WinFBX framework) is probably the main reason that I stuck with FB over the long run. There is so much there that makes FB programming much easier. WinFormsX is basically a layer built on top of WinFBX (AFX). If you look at the code you will see me using Jose's CWindow class (a masterpiece!!) all of the time. WinFBX also makes dealing with High DPI displays incredibly easy. Jose does not get enough kudos and I wish that his tools and libraries were more widely adopted.

In the long run, WinFormsX needs to be updated, and expanded, to include more properties and methods as well as maybe custom controls (especially a fully functional easy to use grid control). Also, and MOST IMPORTANTLY, I need to create MUCH better documentation and tutorials. I have shyed away from documentation for two reasons (1) I find it boring and tedious ;) and (2) WinFBE has changed so much that I (probably wrongfully) decided to wait until it was stable before I tried to document everything.

Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Johan Klassen

Paul, it would be wonderful if someone took the documentation responsibility like fxm is for the FB manual