Notifications
Clear all

[Closed] Custom Resizable TextBox

I’ve been trying to create a textbox whose height increases as its text get changed so that no scrollbars appear in it. I tried creating one with a c# assembly but I still struggle with those a lot, even though there are solutions online of people doing it like here http://stackoverflow.com/questions/2893059/autoresize-textbox-control-vertically
Could any of you pros look through my code and see what I’m doing wrong?
Here is my attempt (one of many):

fn CreateCustomTextBoxAssembly forceRecompile:on = if forceRecompile do
(
source = ""
source += "	using System;
"
source += "	using System.Drawing;
"
source += "	using System.Windows.Forms;
"
source += "	public class SpecialTextbox : RichTextBox
"
source += "	{
"
source += "		public SpecialTextbox() { }
"
source += "		private void SpecialTextbox_TextChanged(object sender, EventArgs e)
"
source += "		{
"
source += "			Size sz = new Size(SpecialTextbox.ClientSize.Width, int.MaxValue);
"
source += "			TextFormatFlags flags = TextFormatFlags.WordBreak;
"
source += "			int padding = 3;
"
source += "			int borders = SpecialTextbox.Height - SpecialTextbox.ClientSize.Height;
"
source += "			sz = TextRenderer.MeasureText(SpecialTextbox.Text, SpecialTextbox.Font, sz, flags);
"
source += "			int h = sz.Height + borders + padding;
"
source += "			if (SpecialTextbox.Top + h > this.ClientSize.Height - 10)
"
source += "			{
"
source += "				h = this.ClientSize.Height - 10 - SpecialTextbox.Top;
"
source += "			}
"
source += "			SpecialTextbox.Height = h;
"
source += "		}
"
source += "	}
"
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.ReferencedAssemblies.AddRange #("System.dll", "System.Drawing.dll", "System.Windows.Forms.dll")
compilerParams.GenerateInMemory = true
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
if (compilerResults.Errors.Count > 0) then
(
	errs = stringstream ""
	for i = 0 to (compilerResults.Errors.Count-1) do
	(
		err = compilerResults.Errors.Item[i]
		format "Error:% Line:% Column:% %
" err.ErrorNumber err.Line err.Column err.ErrorText to:errs 
	)
	MessageBox (errs as string) title: "Errors encountered while compiling C# code"
	format "%
" errs
	undefined
)
else
(
	assembly = compilerResults.CompiledAssembly
	assembly.CreateInstance "SpecialTextbox"
)
)
CreateCustomTextBoxAssembly()
Custom_TextBox = dotNetObject "SpecialTextbox"