[{"data":1,"prerenderedAt":479},["ShallowReactive",2],{"blog:2006:winforms-tricks-and-tips":3,"blogMore-Development":465,"comments-winforms-tricks-and-tips":478},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":16,"body":32,"_type":456,"_id":457,"_source":458,"_file":459,"_stem":460,"_extension":461,"url":462,"wordCount":463,"minutes":120,"commentCount":464},"/blog/2006/winforms-tricks-and-tips","2006",false,"en","WinForms tricks & tips","","2006-11-01T02:41:47+00:00","Development",[13,14,15],".NET","WinForms","C#",{"type":17,"children":18},"root",[19,27],{"type":20,"tag":21,"props":22,"children":23},"element","p",{},[24],{"type":25,"value":26},"text","I’m not sure why it doesn’t do this as standard but a simple event handler should do the trick:",{"type":20,"tag":21,"props":28,"children":29},{},[30],{"type":25,"value":31},"People have asked (in the IRC #CSharp) why adding text to a TextBox is so slow and flickery. Normally they are trying:",{"type":17,"children":33,"toc":449},[34,41,45,283,289,293,323,328,333,357,362,367,391,397,402,408,413,419,434,443],{"type":20,"tag":35,"props":36,"children":38},"h2",{"id":37},"treeview-right-mouse-button-select-the-node",[39],{"type":25,"value":40},"TreeView right-mouse button select the node",{"type":20,"tag":21,"props":42,"children":43},{},[44],{"type":25,"value":26},{"type":20,"tag":46,"props":47,"children":51},"pre",{"className":48,"code":49,"language":50,"meta":9,"style":9},"language-csharp shiki shiki-themes everforest-light dracula","private void treeView_MouseDown(object sender, MouseEventArgs e) {\n    if (e.Button == MouseButtons.Right) {\n    TreeNode node = treeView.GetNodeAt(e.X, e.Y);\n    if (node != null)\n        treeView.SelectedNode = node;\n    }\n}\n","csharp",[52],{"type":20,"tag":53,"props":54,"children":55},"code",{"__ignoreMap":9},[56,118,158,212,241,265,274],{"type":20,"tag":57,"props":58,"children":61},"span",{"class":59,"line":60},"line",1,[62,68,74,80,86,91,97,102,108,113],{"type":20,"tag":57,"props":63,"children":65},{"style":64},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[66],{"type":25,"value":67},"private",{"type":20,"tag":57,"props":69,"children":71},{"style":70},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[72],{"type":25,"value":73}," void",{"type":20,"tag":57,"props":75,"children":77},{"style":76},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[78],{"type":25,"value":79}," treeView_MouseDown",{"type":20,"tag":57,"props":81,"children":83},{"style":82},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[84],{"type":25,"value":85},"(",{"type":20,"tag":57,"props":87,"children":88},{"style":70},[89],{"type":25,"value":90},"object",{"type":20,"tag":57,"props":92,"children":94},{"style":93},"--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[95],{"type":25,"value":96}," sender",{"type":20,"tag":57,"props":98,"children":99},{"style":82},[100],{"type":25,"value":101},", ",{"type":20,"tag":57,"props":103,"children":105},{"style":104},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[106],{"type":25,"value":107},"MouseEventArgs",{"type":20,"tag":57,"props":109,"children":110},{"style":93},[111],{"type":25,"value":112}," e",{"type":20,"tag":57,"props":114,"children":115},{"style":82},[116],{"type":25,"value":117},") {\n",{"type":20,"tag":57,"props":119,"children":121},{"class":59,"line":120},2,[122,128,133,139,144,149,154],{"type":20,"tag":57,"props":123,"children":125},{"style":124},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[126],{"type":25,"value":127},"    if",{"type":20,"tag":57,"props":129,"children":130},{"style":82},[131],{"type":25,"value":132}," (e.",{"type":20,"tag":57,"props":134,"children":136},{"style":135},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[137],{"type":25,"value":138},"Button",{"type":20,"tag":57,"props":140,"children":141},{"style":64},[142],{"type":25,"value":143}," ==",{"type":20,"tag":57,"props":145,"children":146},{"style":82},[147],{"type":25,"value":148}," MouseButtons.",{"type":20,"tag":57,"props":150,"children":151},{"style":135},[152],{"type":25,"value":153},"Right",{"type":20,"tag":57,"props":155,"children":156},{"style":82},[157],{"type":25,"value":117},{"type":20,"tag":57,"props":159,"children":161},{"class":59,"line":160},3,[162,167,172,177,182,187,192,197,202,207],{"type":20,"tag":57,"props":163,"children":164},{"style":104},[165],{"type":25,"value":166},"    TreeNode",{"type":20,"tag":57,"props":168,"children":169},{"style":82},[170],{"type":25,"value":171}," node ",{"type":20,"tag":57,"props":173,"children":174},{"style":64},[175],{"type":25,"value":176},"=",{"type":20,"tag":57,"props":178,"children":179},{"style":82},[180],{"type":25,"value":181}," treeView.",{"type":20,"tag":57,"props":183,"children":184},{"style":76},[185],{"type":25,"value":186},"GetNodeAt",{"type":20,"tag":57,"props":188,"children":189},{"style":82},[190],{"type":25,"value":191},"(e.",{"type":20,"tag":57,"props":193,"children":194},{"style":135},[195],{"type":25,"value":196},"X",{"type":20,"tag":57,"props":198,"children":199},{"style":82},[200],{"type":25,"value":201},", e.",{"type":20,"tag":57,"props":203,"children":204},{"style":135},[205],{"type":25,"value":206},"Y",{"type":20,"tag":57,"props":208,"children":209},{"style":82},[210],{"type":25,"value":211},");\n",{"type":20,"tag":57,"props":213,"children":215},{"class":59,"line":214},4,[216,220,225,230,236],{"type":20,"tag":57,"props":217,"children":218},{"style":124},[219],{"type":25,"value":127},{"type":20,"tag":57,"props":221,"children":222},{"style":82},[223],{"type":25,"value":224}," (node ",{"type":20,"tag":57,"props":226,"children":227},{"style":64},[228],{"type":25,"value":229},"!=",{"type":20,"tag":57,"props":231,"children":233},{"style":232},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[234],{"type":25,"value":235}," null",{"type":20,"tag":57,"props":237,"children":238},{"style":82},[239],{"type":25,"value":240},")\n",{"type":20,"tag":57,"props":242,"children":244},{"class":59,"line":243},5,[245,250,255,260],{"type":20,"tag":57,"props":246,"children":247},{"style":82},[248],{"type":25,"value":249},"        treeView.",{"type":20,"tag":57,"props":251,"children":252},{"style":135},[253],{"type":25,"value":254},"SelectedNode",{"type":20,"tag":57,"props":256,"children":257},{"style":64},[258],{"type":25,"value":259}," =",{"type":20,"tag":57,"props":261,"children":262},{"style":82},[263],{"type":25,"value":264}," node;\n",{"type":20,"tag":57,"props":266,"children":268},{"class":59,"line":267},6,[269],{"type":20,"tag":57,"props":270,"children":271},{"style":82},[272],{"type":25,"value":273},"    }\n",{"type":20,"tag":57,"props":275,"children":277},{"class":59,"line":276},7,[278],{"type":20,"tag":57,"props":279,"children":280},{"style":82},[281],{"type":25,"value":282},"}\n",{"type":20,"tag":35,"props":284,"children":286},{"id":285},"adding-text-to-a-text-box",[287],{"type":25,"value":288},"Adding text to a text box",{"type":20,"tag":21,"props":290,"children":291},{},[292],{"type":25,"value":31},{"type":20,"tag":46,"props":294,"children":296},{"className":48,"code":295,"language":50,"meta":9,"style":9},"textBox1.Text += myNewText;\n",[297],{"type":20,"tag":53,"props":298,"children":299},{"__ignoreMap":9},[300],{"type":20,"tag":57,"props":301,"children":302},{"class":59,"line":60},[303,308,313,318],{"type":20,"tag":57,"props":304,"children":305},{"style":82},[306],{"type":25,"value":307},"textBox1.",{"type":20,"tag":57,"props":309,"children":310},{"style":135},[311],{"type":25,"value":312},"Text",{"type":20,"tag":57,"props":314,"children":315},{"style":64},[316],{"type":25,"value":317}," +=",{"type":20,"tag":57,"props":319,"children":320},{"style":82},[321],{"type":25,"value":322}," myNewText;\n",{"type":20,"tag":21,"props":324,"children":325},{},[326],{"type":25,"value":327},"The problem with this is that it copies all the Text from the text box then adds myNewText to it and copies the whole result back. This is because strings in .NET are immutable, i.e. can’t be changed, and so adding one string to another always results in this overhead (and hence the existence of the StringBuilder class).",{"type":20,"tag":21,"props":329,"children":330},{},[331],{"type":25,"value":332},"The solution is to abandon the slow, inefficient string concatenation and use the method AppendText thusly:",{"type":20,"tag":46,"props":334,"children":336},{"className":48,"code":335,"language":50,"meta":9,"style":9},"textBox1.AppendText(myNewText);\n",[337],{"type":20,"tag":53,"props":338,"children":339},{"__ignoreMap":9},[340],{"type":20,"tag":57,"props":341,"children":342},{"class":59,"line":60},[343,347,352],{"type":20,"tag":57,"props":344,"children":345},{"style":82},[346],{"type":25,"value":307},{"type":20,"tag":57,"props":348,"children":349},{"style":76},[350],{"type":25,"value":351},"AppendText",{"type":20,"tag":57,"props":353,"children":354},{"style":82},[355],{"type":25,"value":356},"(myNewText);\n",{"type":20,"tag":21,"props":358,"children":359},{},[360],{"type":25,"value":361},"Which is fast and efficient while also being available to TextBox, RichTextBox and MaskedTextBox (by virtue of being a method of BaseTextBox).",{"type":20,"tag":21,"props":363,"children":364},{},[365],{"type":25,"value":366},"They usually also ask how to make the text box scroll to the end. Just use the following line:",{"type":20,"tag":46,"props":368,"children":370},{"className":48,"code":369,"language":50,"meta":9,"style":9},"textBox1.ScrollToCaret();\n",[371],{"type":20,"tag":53,"props":372,"children":373},{"__ignoreMap":9},[374],{"type":20,"tag":57,"props":375,"children":376},{"class":59,"line":60},[377,381,386],{"type":20,"tag":57,"props":378,"children":379},{"style":82},[380],{"type":25,"value":307},{"type":20,"tag":57,"props":382,"children":383},{"style":76},[384],{"type":25,"value":385},"ScrollToCaret",{"type":20,"tag":57,"props":387,"children":388},{"style":82},[389],{"type":25,"value":390},"();\n",{"type":20,"tag":35,"props":392,"children":394},{"id":393},"dont-forget-keyboard-input",[395],{"type":25,"value":396},"Don’t forget keyboard input",{"type":20,"tag":21,"props":398,"children":399},{},[400],{"type":25,"value":401},"Check those tab orders and accelerator keys!",{"type":20,"tag":35,"props":403,"children":405},{"id":404},"creating-dynamic-controls",[406],{"type":25,"value":407},"Creating dynamic controls",{"type":20,"tag":21,"props":409,"children":410},{},[411],{"type":25,"value":412},"If you’re ever unsure how to work with a dynamic control just create it in Visual Studio’s Designer and then head into the .designer.cs file and examine the code it generates.",{"type":20,"tag":35,"props":414,"children":416},{"id":415},"windows-forms-faq",[417],{"type":25,"value":418},"Windows Forms FAQ",{"type":20,"tag":21,"props":420,"children":421},{},[422,424,432],{"type":25,"value":423},"There are many other hints, tips and solutions in the ",{"type":20,"tag":425,"props":426,"children":430},"a",{"href":427,"rel":428},"https://www.syncfusion.com/FAQ/WindowsForms/Default.aspx",[429],"nofollow",[431],{"type":25,"value":418},{"type":25,"value":433},".",{"type":20,"tag":21,"props":435,"children":436},{},[437],{"type":20,"tag":438,"props":439,"children":440},"em",{},[441],{"type":25,"value":442},"[)amien",{"type":20,"tag":444,"props":445,"children":446},"style",{},[447],{"type":25,"value":448},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":9,"searchDepth":120,"depth":120,"links":450},[451,452,453,454,455],{"id":37,"depth":120,"text":40},{"id":285,"depth":120,"text":288},{"id":393,"depth":120,"text":396},{"id":404,"depth":120,"text":407},{"id":415,"depth":120,"text":418},"markdown","content:blog:2006:winforms-tricks-and-tips.md","content","blog/2006/winforms-tricks-and-tips.md","blog/2006/winforms-tricks-and-tips","md","/blog/2006/winforms-tricks-and-tips/",317,0,[466,470,474],{"title":467,"date":468,"url":469},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":471,"date":472,"url":473},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":475,"date":476,"url":477},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[],1779264594425]