Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1550 | Anzahl registrierter User: 1839 | Besucher online: 8
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Autosize-TextBox


Autor: Günther Foidl
Sprache: C#
Bewertung: 3,4
(1 Bewertung)
Anzahl der Aufrufe: 7896
  
Kick it on dotnet-kicks.de  

Beschreibung:

Eine TextBox die ihre größe an den eingegebenen Text anpasst, ähnlich dem Google-Suchfeld in der Toolbar.

Abgelegt unter: TextBox, AutoSize.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
	public class AutoSizeTextBox : TextBox
	{
		#region Properties
		[Description("Minim width of the textbox")]
		[DefaultValue(80)]
		public int MinWidth { get; set; }

		[Description("Maximum width of the textbox")]
		[DefaultValue(400)]
		public int MaxWidth { get; set; }

		[Description("Padding")]
		[DefaultValue(5)]
		public new int Padding { get; set; }
		#endregion
		//---------------------------------------------------------------------
		#region Ctor
		public AutoSizeTextBox() : base()
		{
			this.MinWidth = 80;
			this.MaxWidth = 400;
			this.Padding = 5;
		}
		#endregion
		//---------------------------------------------------------------------
		#region Overrides
		protected override void OnTextChanged(EventArgs e)
		{
			// "Inform" the base:
			base.OnTextChanged(e);

			using (Graphics g = this.CreateGraphics())
			{
				SizeF size = g.MeasureString(this.Text, this.Font);
				int width = (int)size.Width + this.Padding;

				if (width < this.MinWidth) width = this.MinWidth;

				this.Width = width;
			}
		}
		#endregion
	}
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.