开发者

Problem with appendText in c#

I have an app that has 3 text boxes, (users name, what company they are from, and who they are visiting) A button to print, and a keyboard on the screen (monitor is touch screen). I have everything working and functioning...

BUT, the one thing that does not work is when the user points to previous character in the text box that has already been typed, the buttons that "AppendText" (keyboard) do not start typing where the user pointed but it continues typing at the end of what has been typed.

Is this because of "AppendText" or some other issue that I have in my code?

I also am trying to get the first text box (Name_Box) to be sent to form one, which then will be split into two labels (1, first name| 2, last name) right now I have it being sent to one label But I want to split it so the first name is stacked above the second name in the next form (which is printed out).

Thank you so much.

Here is my code: First Form

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Printing;
    using System.IO;

    namespace SMART
    {
  开发者_如何转开发  public partial class Form1 : Form
    {

    private TextBox tbSelected; // Last focused TextBox
    private int posCaret;       // Caret position
    private int selLength;      // Selection length

    public Form1()
    {
        InitializeComponent();


        // We will use leave event for textboxes

        Name_Box.Leave += new System.EventHandler(textBox_Leave);
        Company_Box.Leave += new System.EventHandler(textBox_Leave);
        Visiting_Box.Leave += new System.EventHandler(textBox_Leave);
        // Set initial selection to the first textbox
        Name_Box.Select();
        tbSelected = Name_Box;
        posCaret = 0;
        selLength = 0;


    }

    // Leave event handler
    private void textBox_Leave(object sender, EventArgs e)
    {
        // Remember the last focused thextbox,
        // the caret position in it and the selection length
        tbSelected = (TextBox)sender;

        posCaret = tbSelected.SelectionStart;
        selLength = tbSelected.SelectionLength;

    }

    // Helper method to restore selection
    private void RestoreLastSelection()
    {
        tbSelected.Select();
        posCaret = tbSelected.SelectionStart;
        selLength = tbSelected.SelectionLength;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        label5.Text = DateTime.Now.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        label5.Text = DateTime.Now.ToString();

        Form2 frm = new Form2(Name_Box.Text);
        frm.Show();
        frm.Close();

        StreamWriter sw;
        sw = File.AppendText ("C:\\SignIn.txt");
        sw.WriteLine ("Date and Time: " + label5.Text + " | Name: " + Name_Box.Text + "  | Company: " + Company_Box.Text + " | Visiting: " + Visiting_Box.Text + " |");
        sw.Close ();

        Name_Box.Clear();
        Company_Box.Clear();
        Visiting_Box.Clear();
    }

    private void button42_Click(object sender, EventArgs e)
    {
        //SPACE BAR

        tbSelected.AppendText(" ");

    }

    private void button24_Click(object sender, EventArgs e)
    {
        //DELETE
        string t = tbSelected.Text;
        if (t.Length > 0)
        {
            tbSelected.Text = t.Remove(t.Length - 1);
        } 
    }

    private void button12_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("-");
    }

    private void button13_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("Q");
    }

    private void button14_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("W");
    }

    private void button15_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("E");
    }

    private void button16_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("R");
    }

    private void button17_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("T");
    }

    private void button18_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("Y");
    }

    private void button19_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("U");
    }

    private void button20_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("I");
    }

    private void button21_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("O");
    }

    private void button22_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("P");
    }

    private void button25_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("A");
    }

    private void button26_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("S");
    }

    private void button27_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("D");
    }

    private void button28_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("F");
    }

    private void button29_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("G");
    }

    private void button30_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("H");
    }

    private void button31_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("J");
    }

    private void button32_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("K");
    }

    private void button33_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("L");
    }

    private void button35_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("Z");
    }

    private void button36_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("X");
    }

    private void button37_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("C");
    }

    private void button38_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("V");
    }

    private void button39_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("B");
    }

    private void button40_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("N");
    }

    private void button41_Click(object sender, EventArgs e)
    {
        tbSelected.AppendText("M");
    }

    private void button2_Click_1(object sender, EventArgs e)
    {
        tbSelected.AppendText("'");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        tbSelected.Clear();
    }
    }
     }

Heres is my code: Second Form

     using System;
     using System.Collections.Generic;
     using System.ComponentModel;
     using System.Data;
     using System.Drawing;
     using System.Linq;
     using System.Text;
     using System.Windows.Forms;
     using System.Drawing.Printing;




    namespace SMART
    {
        public partial class Form2 : Form
        {
            public Form2(string strTextBox)
            {
                InitializeComponent();
                label3.Text = strTextBox;   
            }

            private void Form2_Load(object sender, EventArgs e)
            {
                label1.Text = DateTime.Now.ToString();

                PrintDocument pd = new PrintDocument();
                Margins margins = new Margins(0, 0, 0, 0);
                pd.DefaultPageSettings.Margins = margins;
                pd.PrintPage += new PrintPageEventHandler(PrintImage);
                pd.Print();

                /*
                //My sad attempt at splitting the Name


                var fullname = strTextBox;
                var names = fullname.Split (" ");
                label3.Text = names[0];
                label5.Text = names[1];
        */
            }

            void PrintImage(object o, PrintPageEventArgs e)
            {
                int x = SystemInformation.WorkingArea.X;
                int y = SystemInformation.WorkingArea.Y;
                int width = this.Width;
                int height = this.Height;

                Rectangle bounds = new Rectangle(x, y, width, height);
                Bitmap img = new Bitmap(width, height);
                this.DrawToBitmap(img, bounds);
                Point p = new Point(0, 0);
                e.Graphics.DrawImage(img, p);
            }

        }
    }


You're right in that your problem is the use of AppendText, which always appends to the end (that's what append means).

You need to Insert the character at the current carat position.

You might do better to post a message that simulates a keypress from a physical keyboard.


If you want to insert text at the user's current position, you can use SelectedText. This will replace the current selection (if the user has selected characters):

tbSelected.SelectedText = "V";

Edit: The problem is in here:

private void button24_Click(object sender, EventArgs e)
{
    //DELETE
    string t = tbSelected.Text;
    if (t.Length > 0)
    {
        tbSelected.Text = t.Remove(t.Length - 1);
    } 
}

You set the text, which returns the cursor to the beginning of the textbox. You should set tbSelected.SelectionStart after you clear the text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜