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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

toolbar button images too small

Status
Not open for further replies.

IT4EVR

Programmer
Feb 15, 2006
462
US
I am implementing a toolbar in my application. I have it hooked up with an image list. The problem I am having is that the images on the button don't stretch to fit the size of the button. Even if I make the button larger, the image stays the same size.

How can I modify this so that I can enlarge the button image?

Thanks...

The wisest people are those who are smart enough to realize they don't know it all.
 
Does the Toolbar Button have a SizeMode property? or ImageSize property?
 
This is a custom button that I wrote. You can add it to a panel or any control container and it will behave much like a button.

Code:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CustomTools.GUI
{
	public class MyToolbarButton : System.Windows.Forms.PictureBox
	{
		private bool Over = false;
		private bool selected = false;

		public MyToolbarButton()
		{
			//
			// TODO: Add constructor logic here
			//
			SetStyle(ControlStyles.AllPaintingInWmPaint | 
				ControlStyles.UserPaint | 
				ControlStyles.DoubleBuffer,
				true); 

			this.Size = new Size(22,22);
		}


		/// <summary>Specify if this button is currently pressed/toggled</summary>
		public bool Selected
		{
			set
			{
				selected = value;
				this.Invalidate();
			}
			get
			{
				return selected;
			}
		}

		protected override void OnMouseHover(EventArgs e)
		{
			this.Over = true;
			this.Invalidate();
		}

		protected override void OnMouseLeave(EventArgs e)
		{
			this.Over = false;
			this.Invalidate();
		}


		private void InitializeComponent()
		{
			this.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;

		}


		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
			base.OnPaint (e);

			if (Over)
			{
				e.Graphics.DrawRectangle(Pens.DarkGray, 0,0,this.Width - 1, this.Height - 1);
			}
			if (selected)
			{
				e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(85, Color.SteelBlue)), 
					(this.Width - this.Image.Width) / 2, (this.Height - this.Image.Height) / 2,	
					this.Image.Width,this.Image.Height);
			}
		}
	}
}
 
The toolbar has a button size property, however that doesn't size the image with the button. It just makes the button larger. That's a great code piece there but not sure if I want to go through all that for a button image. The toolbar has a size mode however again it doesn't change sizes of the images in the buttons.

The wisest people are those who are smart enough to realize they don't know it all.
 
I figured out the solution. I had to change the ImageSize Height and Width properties within the ImageList.

The wisest people are those who are smart enough to realize they don't know it all.
 
I wouldn't recommend that unless you are using just that ImageList for just that button.

If you had say, two buttons.. two different sizes...but the same image... you'd have some major problems.


ToolBar has been replaced by the ToolStrip Control in .NET 2.0 anyways.

I recommend updating, it has the ToolStripRenderer class that is really neat:
 
Note: broken link above due to parenthesis.. copy/paste it instead
 
Thanks for your reply but I don't feel the need to upgrade to 2.0 at this time. All of these images were about the same size to begin with so this will work for me.

Ever since 2.0 has come out, it's like every developer is expected to drop what he is doing and automatically use the new version. I perused a book on 2.0 for the web and it's presented some nice features.

When I go to the bookstore, I can no longer see any books on the 1.1 technology. It's as if it has dropped off the earth. What, I guess we can't create nice apps with 1.1 anymore :(

Frankly I want to master the 1.1 technology before moving to the 2.0. While 2.0 has some great features, from what I have been exposed to, it basically automatically generates code for some aspects in 1.1 that you had to write a lot of code for. The problem is, you may never know really what goes on behind the scenes.

Maybe my view is misguided but I'm going with it :)

I'll tackle 2.0 next year.

The wisest people are those who are smart enough to realize they don't know it all.
 
I would disagree with your opinion that 2.0 is only some new features.

It's not so much that 2.0 just autgenerates code you needed to hand-write before but just like this particular case with the toolbar control:

The 1.1 toolbar was never designed with the idea to do what you are asking it to do. So you are coding a work-around. The new toolStrip in 2.0 IS designed to do what you want to do. You can choose to stay w/ 1.1... I am just suggesting that this work-around isn't necessary in 2.0.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top