Click here to Skip to main content
15,887,945 members
Home / Discussions / C#
   

C#

 
AnswerRe: align columns headers with cells values in a text file that exported from a datagridview in c# Pin
Gerry Schmitz24-Feb-19 8:46
mveGerry Schmitz24-Feb-19 8:46 
GeneralRe: align columns headers with cells values in a text file that exported from a datagridview in c# Pin
Member 1332584624-Feb-19 13:23
Member 1332584624-Feb-19 13:23 
QuestionTextBox setting caret at end of text Pin
ormonds23-Feb-19 13:21
ormonds23-Feb-19 13:21 
AnswerRe: TextBox setting caret at end of text Pin
Dave Kreskowiak23-Feb-19 13:56
mveDave Kreskowiak23-Feb-19 13:56 
GeneralRe: TextBox setting caret at end of text Pin
ormonds23-Feb-19 14:20
ormonds23-Feb-19 14:20 
GeneralRe: TextBox setting caret at end of text Pin
Mycroft Holmes23-Feb-19 14:59
professionalMycroft Holmes23-Feb-19 14:59 
GeneralRe: TextBox setting caret at end of text Pin
Dave Kreskowiak23-Feb-19 15:16
mveDave Kreskowiak23-Feb-19 15:16 
AnswerRe: TextBox setting caret at end of text Pin
BillWoodruff23-Feb-19 20:31
professionalBillWoodruff23-Feb-19 20:31 
This is a swamp I have waded in, before.

The cheap solution: put code to update the TextBox caret in the Form's 'Shown event handler.

But, if you want to make the TextBox as independent as possible of its container, and, assuming:

1. this is a WinForm app with a TextBox set to show a vertical scrollbar

2. you will set the Text property at design time, or in the 'Load event of its container.

3. when the Form is shown, you want the TextBox scrolled to the end, and the TextBox selected/focused so if you start typing, what you type is added to the TextBox:
using System;
using System.Windows.Forms;

namespace YOURNAMESPACE 
{
    public partial class AutoScrolledTbx : UserControl // contains a TextBox
    {
        public AutoScrolledTbx()
        {
            InitializeComponent();
        }

        // for use creating an instance in code at run-time
        public AutoScrolledTbx(string txt)
        {
            textBox1.Text = txt;

            this.textBox1.Select(this.textBox1.TextLength + 1, 0);
            this.textBox1.ScrollToCaret();
            this.textBox1.Capture = true;
            this.textBox1.Focus();
        }

        private void AutoScrolledTbx_Load(object sender, EventArgs e)
        {
            // Marc Gravell
            this.BeginInvoke((MethodInvoker)this.ForceScroll);
        }

        // adapted from Marc Gravell
        // https://stackoverflow.com/a/218740/133321
        private void ForceScroll()
        {
            // UI thread update
            this.Invoke(
                (MethodInvoker)delegate
                {
                    this.textBox1.Select(this.textBox1.TextLength + 1, 0);
                    this.textBox1.ScrollToCaret();
                    this.textBox1.Focus();
                    ActiveControl = this.textBox1;
                }
            );
        }
        // public method to add text
        public void AppendText(string txt)
        {
            this.textBox1.AppendText(txt);
        }
    }
}
Discussion:

When a Form is being 'Loaded, then displayed, Controls may not appear in an expected state. By pushing a task onto the UI thread using Marc Gravell's technique shown here, you have achieved the equivalent of executing the update code in the Form's 'Shown event.

In case you wonder why a UserControl is used here, rather than a sub-classed TextBox: a TextBox offers no 'Load or other initialization event to manipulate.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot


modified 24-Feb-19 10:28am.

QuestionAPI to call bitcoin price Pin
Member 1416032523-Feb-19 4:25
Member 1416032523-Feb-19 4:25 
AnswerRe: API to call bitcoin price Pin
OriginalGriff23-Feb-19 4:44
mveOriginalGriff23-Feb-19 4:44 
AnswerRe: API to call bitcoin price Pin
Eddy Vluggen23-Feb-19 6:11
professionalEddy Vluggen23-Feb-19 6:11 
QuestionConverting PHP cURL to C# WebClient Pin
Member 1236766823-Feb-19 3:19
Member 1236766823-Feb-19 3:19 
AnswerRe: Converting PHP cURL to C# WebClient Pin
Richard Deeming25-Feb-19 8:31
mveRichard Deeming25-Feb-19 8:31 
QuestionShowing Form at Run Pin
ormonds22-Feb-19 10:47
ormonds22-Feb-19 10:47 
AnswerRe: Showing Form at Run Pin
Dave Kreskowiak22-Feb-19 11:16
mveDave Kreskowiak22-Feb-19 11:16 
GeneralRe: Showing Form at Run Pin
ormonds22-Feb-19 12:27
ormonds22-Feb-19 12:27 
QuestionRDLC Report Data Set Pin
Member 1405713721-Feb-19 13:14
Member 1405713721-Feb-19 13:14 
AnswerRe: RDLC Report Data Set Pin
Gerry Schmitz22-Feb-19 4:37
mveGerry Schmitz22-Feb-19 4:37 
GeneralRe: RDLC Report Data Set Pin
Member 1405713725-Feb-19 7:55
Member 1405713725-Feb-19 7:55 
QuestionChanging textBox from Method Pin
ormonds21-Feb-19 10:11
ormonds21-Feb-19 10:11 
AnswerRe: Changing textBox from Method Pin
Eddy Vluggen21-Feb-19 11:07
professionalEddy Vluggen21-Feb-19 11:07 
AnswerRe: Changing textBox from Method Pin
Mycroft Holmes21-Feb-19 11:11
professionalMycroft Holmes21-Feb-19 11:11 
GeneralRe: Changing textBox from Method Pin
ormonds21-Feb-19 16:48
ormonds21-Feb-19 16:48 
QuestionWHENTO Expect C# 8 to be released with Visual Studio and Default Interface Mathods (aka Multiple Inheritance). Pin
Member 1047477121-Feb-19 4:06
professionalMember 1047477121-Feb-19 4:06 
AnswerRe: WHENTO Expect C# 8 to be released with Visual Studio and Default Interface Mathods (aka Multiple Inheritance). Pin
Eddy Vluggen21-Feb-19 4:16
professionalEddy Vluggen21-Feb-19 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.