JackTheRipper JackTheRipper - 3 months ago 33
C# Question

C# Check input in dynamically created richtextbox

Here is what I have :

private void button1_Click(object sender, EventArgs e)
{
//Creating the RichTextBox
RichTextBox rtb = new RichTextBox();
rtb.Location = new Point(20, 20);
rtb.Width = 400;
rtb.Height = 300;
rtb.BackColor = Color.White;
rtb.Font = new Font("Mistral", 16, FontStyle.Regular);
int size = rtb.TextLength;
rtb.AcceptsTab = true;
rtb.ScrollBars = RichTextBoxScrollBars.Both;
rtb.ReadOnly = false;
rtb.MaxLength = rtb.TextLength;
rtb.ShortcutsEnabled = true;
rtb.EnableAutoDragDrop = true;
Controls.Add(rtb);
}


I want to capitalize user entry by using

string text = rtb.Text.ToUpper();
rtb.Text = text;
rtb.SelectionStart = rtb.Text.Length;


To make this happen, I need to get the user entry constantly into rtb.Text
How can I achieve that?
Thanks in advance.

Answer

A little messing around:

 private void createDynamicRTB()
    {
        rtb.Location = new Point(20, 20);
        rtb.Width = 400;
        rtb.Height = 300;
        rtb.BackColor = Color.White;
        rtb.Font = new Font("Mistral", 16, FontStyle.Regular);
        int size = rtb.TextLength;
        rtb.AcceptsTab = true;
        rtb.ScrollBars = RichTextBoxScrollBars.Both;
        rtb.ReadOnly = false;
        rtb.MaxLength = rtb.TextLength;
        rtb.ShortcutsEnabled = true;
        rtb.EnableAutoDragDrop = true;
        Controls.Add(rtb);
    }
    RichTextBox rtb = new RichTextBox();
    private void button1_Click(object sender, EventArgs e)
    {
        //Capitalize
        rtb.Text = rtb.Text.ToUpper();


    }

or...

RichTextBox rtb = new RichTextBox();
    private void Form1_Load(object sender, EventArgs e)
    {
        createDynamicRTB();
        rtb.TextChanged += Rtb_TextChanged;//use this to capitalize after leaving
        rtb.KeyPress += Rtb_KeyPress;//use this to capitalize immediately
    }

    private void Rtb_KeyPress(object sender, KeyPressEventArgs e)
    {
        rtb.Text = rtb.Text.ToUpper();
    }

    private void Rtb_TextChanged(object sender, EventArgs e)
    {
        //Capitalize
        rtb.Text = rtb.Text.ToUpper();
    }

    private void createDynamicRTB()
    {
        rtb.Location = new Point(20, 20);
        rtb.Width = 400;
        rtb.Height = 300;
        rtb.BackColor = Color.White;
        rtb.Font = new Font("Mistral", 16, FontStyle.Regular);
        int size = rtb.TextLength;
        rtb.AcceptsTab = true;
        rtb.ScrollBars = RichTextBoxScrollBars.Both;
        rtb.ReadOnly = false;
        rtb.MaxLength = rtb.TextLength;
        rtb.ShortcutsEnabled = true;
        rtb.EnableAutoDragDrop = true;
        Controls.Add(rtb);
    }
Comments