2010-07-26 87 views
11

我有一个窗体窗体,将文本框中的文本属性设置为字符串变量的文本属性。当表单运行时,它将选中文本框中的所有文本。我需要尝试弄清楚如何防止这种情况发生。我试过取消选择文本框中的文本

DeslectAll() 

在文本框上的方法,但似乎没有工作。我也试过

txtBox.SelectNextControl(txtCostSummary, true, false, true, true); 

但那种我猜什么需要paramters设置为,调整他们似乎不有所作为。要真正理解我在做什么,我会更清楚地了解这一切是如何发生的。

public Form1() 
{ 
    Apple a = new Apple(); 
    a.IwantThisText = "Item 1: " + 50.00 + "\r\n"; 
    txtBox.Text = a.IwantThisText; 
} 

Class Apple 
{ 
    private string iWantThisText; 
    public string IwantThisText 
    { 
    get { return iWantThisText; } 
    set { iWantThisText += value; } // Appends what was there before 
    } 
} 

一切正常,除非它已经印在了文本,但在文本框中的所有文本信息被选中,这是不是我想过会发生的部分,也不是我所希望发生的。

感谢您的任何想法!

回答

37

试试这个:

txtBox.Select(0, 0); 
+0

这样做的伎俩,谢谢! =) – Froz 2010-07-26 03:06:43

+2

然后你应该接受这个答案。 – 2012-04-24 11:05:10

+1

+1给幸运儿13.也许@Froz现在会接受答案;-) – Arrow 2012-10-23 10:49:12

4

试试这个:

//remove focus from control. 
Apple a = new Apple();  
a.IwantThisText = "Item 1: " + 50.00 + "\r\n";  
txtBox.Text = a.IwantThisText; 

// Add this 
txtBox.TabStop = false; 
+1

+1,因为我不知道TabStop属性会解决这个问题。 – 2014-03-21 16:56:51

+0

在我的RO文本框中设置.TabStop属性为false是我所需要的。很好谢谢。 – jinzai 2017-10-05 18:32:12

10

我知道这是一个老问题,但我发现这工作太:

txtBox.SelectionLength = 0; 

这可能是最好到SteveCav的Select(0,0),因为它不会移动选择开始点。

相关问题