[RESOLVED] If ListBox change -> Do Something
Hello,
is it in some way possible to launch a sub if data in a listbox is updated?
I thought something as simple as:
Code:
Private Sub ListBox1_Change()
MsgBox ("ListBox data changed!")
End Sub
But that did not work.
My listbox is filled with data, and I need for every new line that is added to automatically do some data processing (not to be handled by buttons).
:)
Re: If ListBox change -> Do Something
It would work if you put:
After each place where you put "ListBox1.Add [stuff]". :bigyello:
Re: If ListBox change -> Do Something
Hmm, that doesn't work, as the place where the "ListBox1.Add[stuff]" is in a callback routine and not in the form where the listbox is....
But isn't there any way to do the same with a listbox as you can do with the textbox:
Code:
Private Sub TextBox1_Change()
'Do stuff
End Sub
As this just works right out of the box...?
Re: If ListBox change -> Do Something
All controls come with certain event trapping based on what it is. Adding additional event trapping doesn't work like you think it does.
The easiest way would be to have your own subroutine like this:
vb Code:
'you can throw this is a module so it can be accessed from anywhere
'Replace every "ListBox1.Add [stuff]" with "Call AddToList([stuff])"
Public Sub AddToList(newItem As String)
Form1.ListBox1.Add newItem
msgbox "new thing added"
End Sub
You can also create your own usercontrol. This will allow you to determine changes and create a "_Change()" subroutine, but it involves extra work to essentially do the equivalent of the first solution.
Re: If ListBox change -> Do Something
You can also create a whole new Class for it. It's usually just extra work, but it might be closer to what you are thinking of.
Here is a good tutorial on Classes.
Good luck! :bigyello:
Re: If ListBox change -> Do Something
ListBox1.Add :confused:
If using VB.NET it would be ListBox1.Items.Add
If using VB6 it would be List1.Additem
If using some form of VBA (which I suspect is what is being used here) it would be ListBox1.Additem
Quote:
Originally Posted by Cato.M
But isn't there any way to do the same with a listbox as you can do with the textbox
Yet another reason why I suspect you are using VBA rather than VB6....Listboxs in VB6 and VB.NET do not have a Change event....ListBoxs used in the VBA world, however, do have a Change event.
Re: If ListBox change -> Do Something
Yes, you're right Hack, it is VBA. I'm all new to this, so I got a bit confused by the fact that the IDE is named VB6. Anyways, I'm using an API written in C++, and uses some callback functions which I'm having a hard time figuring out. That's why the data is stored in this listbox, which I wanted to use. It turns out it doesn't matter wether I use a listbox or a textbox, so I decided to go for the textbox for now, just to get it up and working. It's not so important how this application works, as long as the result gives me what I need :)
Anyways, thanks for feedbacks guys ;)
Re: If ListBox change -> Do Something
FYI: Since this particular issue is resolved, I'll leave this where it is. However, for future reference, VBA questions should be posted in the Office Development section.
There are times when a VB6 coding solution will also work in VBA. There are times, however, when a VB6 coding solution will NOT work in VBA. If you post the question on Classic VB, our members will assume you are using VB6 and what they give you as potential solutions may, or may not work.