Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default listbox click event help

I have a userform on which I have TextBox1, TextBox2, TextBox3,
ListBox1, and CommandButton1. The user enters info into the three
textboxes then clicks CommandButton1 to update ListBox1 with the data
as shown in the following code:

'If no selection is made in the listbox then create a new
listbox item...
If ListBox1.ListIndex = -1 Then
ListBox1.AddItem TextBox1.Value
ListBox1.List(ListBox1.ListCount - 1, 1) =
TextBox2.Value
ListBox1.List(ListBox1.ListCount - 1, 2) =
TextBox3.Value
'Else if a selection is made in the listbox, update the
values
'to those in the textboxes.
Else
ListBox1.List(ListBox1.ListIndex, 1) =
TextBox1.Value
ListBox1.List(ListBox1.ListIndex, 2) =
TextBox2.Value
ListBox1.List(ListBox1.ListIndex, 3) =
TextBox3.Value
End If

My problem is that I have a ListBox1_Click event that, when clicked,
populates TextBox1, TextBox2, and TextBox3 with the information from
the row selected in ListBox1 as follows:

Private Sub ListBox1_Click()

If ListBox1.ListIndex < -1 Then
TextBox1.Value = ListBox1.List(ListBox1.ListIndex, 1)
TextBox2.Value = ListBox1.List(ListBox1.ListIndex, 2)
TextBox3.Value = ListBox1.List(ListBox1.ListIndex, 3)
End If

End Sub

So when I select a row from ListBox1, TextBox1, TextBox2, and TextBox3
are populated. The idea is to be able to populate the textboxes,
modify them, then click CommandButton1 to update the information on
that line. It appears that the ListBox1_Click event is being fired
again when the data is modified, reverting to the original amounts as
opposed to the updated amounts. I can update ListBox1 without a
problem if I do not have the ListBox1_Click event, but this is
something I would like to have.

Thanks,
Mike.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default listbox click event help

How about make a (private) variable, something like
bCancelClick as Boolean and set this to True when the
command button code runs and make it False again after
the command button code has finished.

Then in your click event have:
If bCancelClick Then
Exit Sub
End If

RBS


"Michael Malinsky" wrote in message
oups.com...
I have a userform on which I have TextBox1, TextBox2, TextBox3,
ListBox1, and CommandButton1. The user enters info into the three
textboxes then clicks CommandButton1 to update ListBox1 with the data
as shown in the following code:

'If no selection is made in the listbox then create a new
listbox item...
If ListBox1.ListIndex = -1 Then
ListBox1.AddItem TextBox1.Value
ListBox1.List(ListBox1.ListCount - 1, 1) =
TextBox2.Value
ListBox1.List(ListBox1.ListCount - 1, 2) =
TextBox3.Value
'Else if a selection is made in the listbox, update the
values
'to those in the textboxes.
Else
ListBox1.List(ListBox1.ListIndex, 1) =
TextBox1.Value
ListBox1.List(ListBox1.ListIndex, 2) =
TextBox2.Value
ListBox1.List(ListBox1.ListIndex, 3) =
TextBox3.Value
End If

My problem is that I have a ListBox1_Click event that, when clicked,
populates TextBox1, TextBox2, and TextBox3 with the information from
the row selected in ListBox1 as follows:

Private Sub ListBox1_Click()

If ListBox1.ListIndex < -1 Then
TextBox1.Value = ListBox1.List(ListBox1.ListIndex, 1)
TextBox2.Value = ListBox1.List(ListBox1.ListIndex, 2)
TextBox3.Value = ListBox1.List(ListBox1.ListIndex, 3)
End If

End Sub

So when I select a row from ListBox1, TextBox1, TextBox2, and TextBox3
are populated. The idea is to be able to populate the textboxes,
modify them, then click CommandButton1 to update the information on
that line. It appears that the ListBox1_Click event is being fired
again when the data is modified, reverting to the original amounts as
opposed to the updated amounts. I can update ListBox1 without a
problem if I do not have the ListBox1_Click event, but this is
something I would like to have.

Thanks,
Mike.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default listbox click event help

I'm not sure I follow...can you expand on your response?

Thanks,
Mike.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default listbox click event help

Yes, your code will be like this:

At the top (in the declarations section) of your form code have this:

private bCancelClick as Boolean

Then your altered code will be:

In the commandbutton:
----------------------------------

bCancelClick = True

'If no selection is made in the listbox then create a new
listbox item...
If ListBox1.ListIndex = -1 Then
ListBox1.AddItem TextBox1.Value
ListBox1.List(ListBox1.ListCount - 1, 1) =
TextBox2.Value
ListBox1.List(ListBox1.ListCount - 1, 2) =
TextBox3.Value
'Else if a selection is made in the listbox, update the
values
'to those in the textboxes.
Else
ListBox1.List(ListBox1.ListIndex, 1) =
TextBox1.Value
ListBox1.List(ListBox1.ListIndex, 2) =
TextBox2.Value
ListBox1.List(ListBox1.ListIndex, 3) =
TextBox3.Value
End If

bCancelClick = False


In the Click event:
------------------------------

Private Sub ListBox1_Click()

If bCancelClick Then
Exit Sub
End If

If ListBox1.ListIndex < -1 Then
TextBox1.Value = ListBox1.List(ListBox1.ListIndex, 1)
TextBox2.Value = ListBox1.List(ListBox1.ListIndex, 2)
TextBox3.Value = ListBox1.List(ListBox1.ListIndex, 3)
End If

End Sub


RBS


"Michael Malinsky" wrote in message
ups.com...
I'm not sure I follow...can you expand on your response?

Thanks,
Mike.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default listbox click event help

Thanks...after I sent my last post, I was messing around with it and
came up with the same solution. My initial confusion was regarding the
placement of the

Private bCancelClick as Boolean

Once I figured that out I followed the rest.

Thanks again.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ListBox Click Event BHatMJ Excel Discussion (Misc queries) 6 June 21st 07 09:34 PM
What event to detect click on UserForm ListBox? Don Wiss Excel Programming 1 August 26th 05 10:35 PM
Click event on listbox IanC Excel Programming 2 April 5th 05 07:05 AM
click event on listbox CStephenson Excel Programming 1 March 22nd 05 06:21 PM
Is refreshing listbox rowsource in listbox click event possible? Jeremy Gollehon[_2_] Excel Programming 4 September 25th 03 06:45 PM


All times are GMT +1. The time now is 11:26 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"