Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inital Tool Bar Button selection stays pressed... 1

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
I have a toolbar setup, which includes a group of 4 buttons that change the "mode" of the program...

Lets say the keys for the for buttons are "A", "B", "C", & "D"

I have the the Form's KeyPreview Set to True, and in the Form_KeyDown() event, I have code similar to this:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  Dim MyButton As Button
  Select Case KeyCode
    Case vbKeyF5: Set MyButton = Toolbar1.Buttons("A")
    Case vbKeyF6: Set MyButton = Toolbar1.Buttons("B")
    Case vbKeyF7: Set MyButton = Toolbar1.Buttons("C")
    Case vbKeyF8: Set MyButton = Toolbar1.Buttons("D")
  End Select
  If Not MyButton Is Nothing Then
    Toolbar1_ButtonClick MyButton
    MyButton.Value = tbrPressed
    TreeView1.SetFocus
  End If
End Sub

Originally, I had the Value property for Button A set to "Pressed" in the Form Designer...

When I turned it off, It worked fine, except there was no default button pressed, which I see as a program flaw.

So I decided to set it at run time...
I tried this code...
Code:
Toolbar1.Buttons("A").Value = tbrPressed
In Each of the following Events:
Form_Load()
Form_Paint()
Form_Activate()

Yet it responded in the same way as if it were set in the designer...

[COLOR=red yellow]Though, after you manually click a button, it works fine, in ANY of the above cases...
Just Not Before You Click A Button...
[/color]

So what is causing this?????

I'm running short on ideas here...

Is there something I could be doing to cause this, or is it just a VB Bug?

*Note: I did try setting the Buttons NOT selected to tbrUnpressed...
Code:
...
With Toolbar1
  If .Buttons("A").Key <> MyButton.Key Then .Buttons("A").Value = tbrUnpressed 
  If .Buttons("B").Key <> MyButton.Key Then .Buttons("B").Value = tbrUnpressed 
  If .Buttons("C").Key <> MyButton.Key Then .Buttons("C").Value = tbrUnpressed 
  If .Buttons("D").Key <> MyButton.Key Then .Buttons("D").Value = tbrUnpressed 
End With
...

...But that does not fix the problem either.

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Seems to be a bug. Moreover, it bugs seems to be with version 5 toolbar only. I tried on verion 6 toolbar as well and it worked fine.
 
>Is there something I could be doing to cause this, or is it just a VB Bug?

No
Yes

The workaround is ... obscure ...

Drop a timer onto your form (call it ToolbarEnableTimer, just for the heck of it), make sure it is enabled and that the interval is set to 1. Place you tbrPressed code in the timer, and immediately disable it:

Private Sub ToolbarEnableTimer_Timer()
Toolbar1.Buttons("A").Value = tbrPressed
Timer1.Enabled = False
End Sub
 
(and I can confirm that the bug does exist in the version 6 toolbar)
 
Cool... (A timer never crossed my mind)

I had to make one modification to get it to work...

I have a splash screen that pops up while the program connects to the database (which takes a few seconds...)

This, effectively, made the timer useless...

So I added a condition that the Splash Screen, Form3 (generic, huh ;-)), be closed before pressing the button...
Code:
Private Sub ToolbarEnable_Timer()
  [b]If Form3.Visible = False Then[/b]
    Toolbar1.Buttons("mxMode").Value = tbrPressed
    ToolbarEnable.Enabled = False
  [b]End If[/b]
End Sub

Now it works like a charm :)

Thanks!

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 

I set the value sometimes in code also, and have not problems - IF I understood this right:

MyButton.Value = tbrPressed 'Put this in the first position
Toolbar1_ButtonClick MyButton

Then, in the click event:
Button.Value = tbrUnpressed

 
I was refering to a Group, where one stays pressed, and the others are unpressed...

Where the Style property of the Button is set to: tbrButtonGroup"

when you set one of the other buttons to tbrPressed, the one that was previously Pressed, automatically becomes Unpressed...
Such As:
button group.png


Similar to Radio/Option Buttons...

The bug consists of where a button is pressed before the loading is completed, it will not be unpressed when the other buttons are pressed programmatically, until one of the buttons is manually clicked with the mouse...

In this case, I was using keyboard shortcuts to programmatically press the buttons...

This shows that the button is pressed, and releases the previous one:
MyButton.Value = tbrPressed

This emulates the button being pressed by executing the code in the click event, without the event being fired the normal way through a Click message from within windows...
Toolbar1_ButtonClick MyButton

I suppose, you could place the .Value = tbrPressed code on the button inside the _Click event to simplify things...

The solution(s) shown in the above posts were to prevent the pressed state from being set before the form loads...

With a Button Group, Under Normal Circumstances, the .Value = tbrUnpressed should not have to be used...
(With the bug, it did not work regardless)

From your post, it sounds like you are referring to a Button with the Style set to tbrCheck which makes the button function as a CheckBox, and you do have to unpress it...

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
>IF I understood this right

I think you need to read really, realy closely to understand what Josh is on about. To be fair to you, he isn't as clear as he might be in his original post to casual VB users (sorry, Josh)
 
No offence taken...

I could have explained in a little more detail...

But I figured this was more of an advance question, and figured that the ones who could help, would understand...
(and it seems you did)

I realized this after I had posted...

That's why I tried to explain in a little more detail of what was going on in the post, for anyone else reading through...

I appologize for any confusion this may have caused.

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Thank to CubeE101 for the extra explaination.

No, I just read too fast...doesn't that sometimes happen to everyone, (or even read wrong, especially for people who do not have englisch as their native language)? [smile]
And no, not casual.

BTW: the exact same solution using the timer is in the MS Kb:

If you change the button types to 0-default, and place the following code in the tb click event, I believe it would work as well and you will not need the timer:
Code:
    Dim btn As mscomctllib.Button
    For Each btn In tlbSelBuchungAuftrag.Buttons
        If btn.Index = Button.Index Then
            Button.Value = tbrPressed
        Else
            btn.Value = tbrUnPressed
        End If
        tlbSelBuchungAuftrag.Refresh 'Important
    Next btn
 

Did you have a chance to test this CubeE101?
I been using it and it works as a button group, as you presented.

btw: tlbSelBuchungAuftrag was the name of the toolbar I was using with this code.
 
Yeah, sorry... it did not work...

The bug requires the timer to set the value AFTER the form loads...

Though, I was messing around with it last night on my home PC (XP Home), and it seemed to work fine, without ANY additional code...

So the bug might be limited to XP Pro...
(I don't know...)

In any case... the solution that strongm suggested, and I modified works fine...

Thanks anyways...

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 

>it did not work
>The bug requires the timer

I posted code to not use a ButtonGroup, but just plain old buttons, set up to work as a button group.

Strange. It is working for me and has been working on w95 to xp-he everytime (compiled).

1. Default buttons, and not group buttons are used.

2. The code I posted in the last post is being used.

3. A button is set to tbrpressed in the load event and when the form shows, is still pressed.

4. In code, prior to anyone clicking a tbr button, a different button is set to pressed, and then the toolbar click event is called.

5. Now the second button is shown pressed and the first not any more.

I guess I need to get xp-pro installed and test this also, even though I can hardly imagine something like this is having a different effect of this type(with the default button acting as a button group, and not using actual buttongroup type of buttons).
(Some of our customers are starting to use xp-pro).

Or could it be something different with the non-english systems we have? (I doubt that though)

I will post again if I ever find it doesn't work for me or our customers also.

Anyways, all the best.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top