3

I want to push toolstripbutton down in my code and I can't seem to be able to do that. I know on Delphi RAD Studio or XE, you can do the following and cause the button to be pressed.

ToolStripButton1.Down := true;

The only ToolStripButton property I see that comes close to "down" is checked true or false. If I do set it to true, it only highlights the toolstripbutton not press it down.

Here is how the button looks when I put my mouse on it and click:

enter image description here

You can clearly see that the Zoom In button is down.

Here is how the button looks when I try to do the samething through my code by setting CheckOnClick true and Checked true.

enter image description here

In this image, the only thing you can see is the blue box around it. I suppose if I had used just the text on the button, you will see that the whole button filled with blue color to show that it was pressed.

enter image description here

I also have toolstrip button in my other program which acts the same way but I had to use imagelist control to switch between pressed or down or checked verses not pressed or down or checked.

So, is there a way to press the ToolStripButton programmatically in Delphi Prism or C#?

Ken White
  • 123,280
  • 14
  • 225
  • 444
ThN
  • 3,235
  • 3
  • 57
  • 115
  • You could refer to this [post](http://stackoverflow.com/questions/84842/how-do-i-programmatically-wire-up-toolstripbutton-events-in-c) – bonCodigo Dec 05 '12 at 14:07
  • @bonCodigo, I already read through that post and it doesn't answer my question. I want to be able to cause the button "go down" or be "pressed" as if an user clicked on it but through my code. – ThN Dec 05 '12 at 14:10
  • @KenWhite :-) LOL Yea, sure if you say so. I am dual booting latest PCLinux OS (LINUX) and Windows 7 not Windows 8. Developing on Delphi 2010 RAD Studio and Delphi Prism (Visual Studio 2010). Thank you. Ugly is not what I am after, Ken, but functionality. Please, don't take it personally. :-) – ThN Dec 07 '12 at 13:48
  • Wasn't insulting your skills; sorry if it appeared that way. I just haven't seen the raised, chunky, square corner tool bar buttons since Delphi 2 (I can't even figure out how to get them like that in `ToolStrip`, and I've spent an hour trying to figure it out. :-) – Ken White Dec 07 '12 at 14:41

2 Answers2

2

Set the ToolStripButton.CheckOnClick property to True. (It's found in the Behavior section of the Items Collection Editor.)

This makes clicking it just like toggling the Down property in a Delphi TSpeedButton (making it flat or depressed), and if ToolStripButton1.Checked is the equivalent of if SpeedButton1.Down in Delphi.

To set up the test, I did the following:

  • Created a new Winforms application
  • Dropped a ToolStrip onto the new MainForm
  • Added four ToolStripButton items and gave them images to make them easier to see.
  • Set the CheckOnClick property to True for each of them
  • Set the Checked property of toolStripButton1 to True;
  • Added the code below to toolStripButton1.Click

    method MainForm.toolStripButton1_Click(sender: System.Object; e: System.EventArgs); begin toolStripButton2.Checked := not toolStripButton2.Checked; toolStripButton4.Checked := toolStripButton2.Checked; end;

Running the app (initial startup, toolStripButton1 checked and the others unchecked):

One button down

The first button is clearly down, and the rest are up.

After clicking toolStripButton1 once:

Two buttons down

The first button is now up (unchecked) and the second and fourth are down (checked). (I should pay more attention to the consistency in sizing if I do successive images in future posts.)

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Ken, unfortunately this didn't work for me. I did set the checkonclick to true and set checked to true. Again, it only highlighted the button not press it down. The weird thing is that it works the other way around. If the button is already pressed down, you can make it flat or bring it back up by setting checked to false. – ThN Dec 05 '12 at 15:30
  • Checked again. Works properly at runtime with the code posted abovel – Ken White Dec 05 '12 at 15:53
  • Ken, I followed your steps and I see how it works, but it doesn't look like the pictures I have added in the description. Is it because I am using image on top of the ToolStripButton instead of just text? – ThN Dec 06 '12 at 13:24
  • No. Mine has images as well (the default empty bitmap image that VS puts on a new ToolStripButton). I can't see the pictures from here, but from your description I think your expectation is wrong. There is an appearance difference between `Down` (a toggle between states) and being `Pressed` (the physical act of having the mouse pointer over it while the mouse button is held down). They're supposed to look different, because they **are** different states. – Ken White Dec 06 '12 at 13:35
  • I just checked again, and what you're seeing is the *focus* rectangle combined with the checked rectangle (which makes it darker when the mouse is over it). If you put two `ToolStripButtons` side by side on the toolbar, set it up like I did above, and make the second one Checked/Unchecked with the mouse, get them both checked at the same time, and move the mouse pointer away from the `ToolStrip` they both appear *identical*. – Ken White Dec 06 '12 at 15:45
  • Yep, that's what I am seeing as well. So, if they are two different states - mouse and non-mouse click, then how do you enable mouse click state. What I don't understand is that ToolStripbutton acts differently based on different inputs but means the same thing. – ThN Dec 06 '12 at 15:58
  • What you're missing is that they **don't** act differently. The act of checking/unchecking is the same, whether it's done with the mouse or in code. Checked is checked, no matter how you get there. You're confusing the visual *focus* rectangle with the *state* of the control, and they have nothing to to with each other. The *focus* indicates where a click of the mouse will change something, and that's it. Nothing more. To make the button react to the mouse, do exactly what I told you above: set `CheckOnClick` to `True`, and the `Checked` value will change automatically when clicked. – Ken White Dec 06 '12 at 16:05
  • For what its worth, I am sure your answer is 100% correct, but I am having hard time accepting it as an answer for now. I am still looking into this. – ThN Dec 07 '12 at 14:00
  • I've posted rewritten instructions, sample code that changes the button state at runtime, and images of different buttons being down (checked) at runtime using the posted code. :-) – Ken White Dec 08 '12 at 20:15
0

If have placed this code in the preceding control in the 'Leave' event.

    Private Sub PurposeComboBox_Leave(sender As Object, e As EventArgs) Handles PurposeComboBox.Leave

    Me.AppliancesForSelectedFunctionToolStripButton.PerformClick()

    End Sub

I do not know if you could place code in Form Load or not. Hope this helps.

Shane
  • 1