Hello Andreia,

the first implementation of Button.AutoSize did not resize in cases where the 
Font has been changed. I've corrected it but forgot to send the new Version 
to the list.

Therefore please review this patch instead of the initial one.

Kind Regards,
Valentin S.

On Wednesday 06 December 2006 15:02, you wrote:
> Hi there!
>
> Sorry for the delay in getting back to you on this. Thank you for the
> patch, I'm looking at it now, will get back to you later on this :)
>
> andreia gaita
> Mono Team
>
> On 11/23/06, latency <[EMAIL PROTECTED]> wrote:
> > Hi,
> > I was working on a Winform App and noticed that AutoSize was not working
> > with buttons so I've hacked a little and made a basic implementation of
> > it.
> >
> > The bits are attached to the Mail.
> >
> > Kind Regards,
> > Valentin S.
> >
> >
> > _______________________________________________
> > Mono-winforms-list maillist  -  [email protected]
> > http://lists.ximian.com/mailman/listinfo/mono-winforms-list
Index: ButtonBase.cs
===================================================================
--- ButtonBase.cs	(revision 68204)
+++ ButtonBase.cs	(working copy)
@@ -47,6 +47,9 @@
 		internal bool 			paint_as_acceptbutton;
 		private	bool			use_compatible_text_rendering;
 		private bool			use_visual_style_back_color;
+#if NET_2_0
+		private bool autoSize;
+#endif
 		#endregion	// Local Variables
 
 		#region ButtonBaseAccessibleObject sub-class
@@ -321,7 +324,6 @@
 				}
 			}
 		}
-
 		#endregion	// Public Instance Properties
 
 		#region Protected Instance Properties
@@ -486,6 +488,11 @@
 		}
 
 		protected override void OnTextChanged(EventArgs e) {
+#if NET_2_0
+			if(this.AutoSize) {
+				this.AutoCalculateNewSize();
+			}
+#endif
 			Redraw();
 			base.OnTextChanged(e);
 		}
@@ -530,6 +537,32 @@
 		private void PerformClick() {
 			OnClick(EventArgs.Empty);
 		}
+
+#if NET_2_0
+		private void AutoCalculateNewSize()	{
+			Graphics g = this.CreateGraphics();
+			SizeF neededSpace = g.MeasureString(this.text,
+				this.Font,
+				new PointF(0f, 0f),
+				this.text_format);
+
+			if(neededSpace.Width > this.DefaultSize.Width) {
+				this.Width = (int)Math.Ceiling(neededSpace.Width);
+			}
+
+			// +15 is needed to have a proper padding between the border and the text.
+			if(neededSpace.Height + 15 > this.DefaultSize.Height) {
+				this.Height = (int)Math.Ceiling(neededSpace.Height) + 15;
+			}
+		}
+
+		protected override void OnFontChanged(EventArgs e) {
+			if(this.AutoSize) {
+				this.AutoCalculateNewSize();
+			}
+			base.OnFontChanged(e);
+		}
+#endif
 		#endregion	// Internal Methods
 
 		#region	Events
@@ -544,9 +577,16 @@
 
 		#region .NET 2.0 Public Instance Properties
 #if NET_2_0
-		public bool UseVisualStyleBackColor {
-			get { return use_visual_style_back_color; }
-			set { use_visual_style_back_color = value; }
+		public override bool AutoSize {
+			get {
+				return this.autoSize;
+			}
+			set {
+				this.autoSize = value;
+				if(value) {
+					this.AutoCalculateNewSize();
+				}
+			}
 		}
 
 		public bool UseCompatibleTextRendering {
@@ -558,6 +598,11 @@
 				use_compatible_text_rendering = value;
 			}
 		}
+
+		public bool UseVisualStyleBackColor {
+			get { return use_visual_style_back_color; }
+			set { use_visual_style_back_color = value; }
+		}		
 #endif
 		#endregion
 	}
_______________________________________________
Mono-winforms-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-winforms-list

Reply via email to