0

我有一个有 3 个文本框的应用程序,(用户名、他们来自什么公司以及他们正在访问谁)一个打印按钮和一个屏幕上的键盘(显示器是触摸屏)。我有一切工作和运作...

但是,不起作用的一件事是当用户指向已经输入的文本框中的前一个字符时,“AppendText”(键盘)的按钮不会在用户指向的位置开始输入,但它会继续在键入的内容结束。

这是因为“AppendText”还是我的代码中存在的其他问题?

我也正在尝试将第一个文本框(Name_Box)发送到一个表单中,然后将其拆分为两个标签(1,名字| 2,姓氏)现在我将它发送到一个标签但是我想拆分它,以便在下一个表单中将第一个名称堆叠在第二个名称之上(打印出来)。

太感谢了。

这是我的代码:第一种形式

    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();
    }
    }
     }

这是我的代码:第二种形式

     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);
            }

        }
    }
4

2 回答 2

2

你是对的,你的问题是 AppendText 的使用,它总是追加到末尾(这就是追加的意思)。

您需要在当前克拉位置插入字符。

您最好发布一条模拟物理键盘按键的消息。

于 2011-08-03T17:46:44.553 回答
0

如果要在用户当前位置插入文本,可以使用SelectedText。这将替换当前选择(如果用户选择了字符):

tbSelected.SelectedText = "V";

编辑:问题出在这里:

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

您设置文本,这会将光标返回到文本框的开头。您应该tbSelected.SelectionStart在清除文本后设置。

于 2011-08-03T17:50:15.807 回答