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.
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,25 @@
 		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;
+			}
+		}
+#endif
 		#endregion	// Internal Methods
 
 		#region	Events
@@ -558,6 +584,18 @@
 				use_compatible_text_rendering = value;
 			}
 		}
+
+		public override bool AutoSize {
+			get {
+				return this.autoSize;
+			}
+			set {
+				this.autoSize = value;
+				if(value) {
+					this.AutoCalculateNewSize();
+				}
+			}
+		}
 #endif
 		#endregion
 	}
_______________________________________________
Mono-winforms-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-winforms-list

Reply via email to