2011-08-03 69 views
0

我有一個應用程序,有3個文本框,(用戶名,他們來自哪家公司,他們正在訪問誰)打印一個按鈕,並在屏幕上的鍵盤(顯示器是觸摸屏)。我有一切工作和運作...問題appendText在c#

但是,一個不起作用的是當用戶指向已經鍵入的文本框中的前一個字符,「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); 
      } 

     } 
    } 
+0

'追加'的意思是'在最後'。沒有InsertText嗎? –

回答

2

你說得對,你的問題是使用AppendText通過,它總是追加到的最後(這就是附加手段)。

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

您可能會更好地發佈模擬鍵盤按鍵的消息。

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

+0

我嘗試過「SelectedText」,問題是當我刪除某些東西時,按鈕現在開始輸入到文本框的開頭。 – rcaveda

+0

我已經添加了更多的細節給我的答案,我認爲這將有所幫助。 –

+0

謝謝你的幫助 – rcaveda