Wrox Programmer Forums
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 17th, 2003, 11:42 AM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to RighteousManUP
Default ToolbarButton toggling

The code will compile, but when I run it, the debugger throws an exception claiming invalid cast exception for the, "int button in," part of the foreach loop. If I do toolBar1.Buttons[1].Pushed = true; as a test, the code has no problem setting the second button to toggled on. The goal of this foreach loop is to turn all the buttons to a non-toggled state, then it will set the button that was clicked to a toggled state. I need to do this in order to keep a mutually exclusive setting for the buttons which represent draw modes. So what is going on?

            foreach (int button in toolBar1.Buttons)
            {
                toolBar1.Buttons[button].Pushed = false;
            }

            toolBar1.Buttons[ButtonClicked].Pushed = true;


Thanks,
Jon



 
Old October 17th, 2003, 12:05 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Jon,

I think this is caused by the fact that the Buttons collection does not return a collection of ints, but a collection of buttons:
Code:
foreach (Button myButton in toolBar1.Buttons)
{
  myButton.Pushed = false;
}
toolBar1.Buttons[ButtonClicked].Pushed = true;
This code will loop through the Buttons collection, and each reference to a button is assigned to MyButton. This means you can treat MyButton as a real button (which it is) and simply use its Pushed property.

HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
ToolbarButton toggling RighteousManUP C# 1 October 17th, 2003 12:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.