विंडोज फॉर्म में टेक्स्टबॉक्स का डिफ़ॉल्ट व्यवहार सभी टेक्स्ट को हाइलाइट करना है यदि इसे पहली बार टैबबिंग करके केंद्रित किया जाता है, लेकिन अगर इसे क्लिक किया जाता है। हम परावर्तक में देख सकते हैं TextBox
के OnGotFocus()
ओवरराइड को देखकर:
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
if (!this.selectionSet)
{
this.selectionSet = true;
if ((this.SelectionLength == 0) && (Control.MouseButtons == MouseButtons.None))
{
base.SelectAll();
}
}
}
ऐसा नहीं है कि अगर बयान है कि व्यवहार है कि हम पसंद नहीं है कारण है। इसके अलावा, चोट के अपमान जोड़ने के लिए, Text
संपत्ति के सेटर आँख बंद करके रीसेट करता है कि selectionSet
चर जब भी पाठ किया जाता है पुनः सौंपा:
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
this.selectionSet = false;
}
}
तो अगर आप इसे में किसी पाठ बॉक्स और टैब है, सभी पाठ का चयन किया जाएगा। यदि आप इसमें क्लिक करते हैं, तो हाइलाइट हटा दिया जाता है, और यदि आप इसमें पुनः टैब करते हैं, तो आपकी देखभाल स्थिति (और शून्य की चयन लंबाई) संरक्षित होती है। लेकिन अगर हम प्रोग्रामिंग रूप से नए Text
सेट करते हैं, और फिर टेक्स्टबॉक्स में टैब को फिर से सेट करते हैं, तो सभी टेक्स्ट फिर से चुने जाएंगे।
यदि आप मेरे जैसे हैं और इस व्यवहार को परेशान और असंगत पाते हैं, तो इस समस्या के आसपास दो तरीके हैं।
पहले, और शायद सबसे आसान, बस प्रपत्र Load()
पर DeselectAll()
बुला और जब भी Text
परिवर्तन से selectionSet
की स्थापना को गति प्रदान करने के लिए है:।
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox2.SelectionStart = this.textBox2.Text.Length;
this.textBox2.DeselectAll();
}
(DeselectAll()
सिर्फ शून्य करने के लिए SelectionLength
सेट यह वास्तव में SelectionStart
है जो TextBox
के selectionSet
वैरिएबल को फ़्लिप करता है। उपरोक्त मामले में, DeselectAll()
पर कॉल आवश्यक नहीं है क्योंकि हम टेक्स्ट के अंत तक शुरुआत सेट कर रहे हैं। लेकिन अगर हम इसे किसी अन्य स्थिति में सेट करते हैं, जैसे पाठ की शुरुआत , फिर बुलाओ यह एक अच्छा विचार है)
अधिक स्थायी रास्ता विरासत के माध्यम से वांछित व्यवहार के साथ अपने स्वयं के पाठ बॉक्स बनाने के लिए है:।
public class NonSelectingTextBox : TextBox
{
// Base class has a selectionSet property, but its private.
// We need to shadow with our own variable. If true, this means
// "don't mess with the selection, the user did it."
private bool selectionSet;
protected override void OnGotFocus(EventArgs e)
{
bool needToDeselect = false;
// We don't want to avoid calling the base implementation
// completely. We mirror the logic that we are trying to avoid;
// if the base implementation will select all of the text, we
// set a boolean.
if (!this.selectionSet)
{
this.selectionSet = true;
if ((this.SelectionLength == 0) &&
(Control.MouseButtons == MouseButtons.None))
{
needToDeselect = true;
}
}
// Call the base implementation
base.OnGotFocus(e);
// Did we notice that the text was selected automatically? Let's
// de-select it and put the caret at the end.
if (needToDeselect)
{
this.SelectionStart = this.Text.Length;
this.DeselectAll();
}
}
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
// Update our copy of the variable since the
// base implementation will have flipped its back.
this.selectionSet = false;
}
}
}
आप शायद करने के लिए परीक्षा सिर्फ base.OnGotFocus()
फोन नहीं है, लेकिन फिर हम उपयोगी खो देगा बेस Control
कक्षा में कार्यक्षमता। और हो सकता है कि आप selectionSet
बकवास के साथ गड़बड़ न करें और OnGotFocus() में हर बार पाठ को अचयनित करें, लेकिन फिर हम उपयोगकर्ता के हाइलाइट को खो देंगे यदि वे फ़ील्ड से बाहर और पीछे टैब्ड करते हैं।
बदसूरत? आप बेट्चा हो। पर अब जो है वो है।
आपका प्रश्न http://stackoverflow.com/ से संबंधित हो सकती प्रश्न/1140250/कैसे-से-निकालें-फोकस-से-एक-टेक्स्टबॉक्स-इन-सी-विनफॉर्म – DarenW
क्या आपने इसे हल करने का प्रबंधन किया था? आपने इसे कैसे ठीक किया? – fletcher
@ फ़्लेचर: मुझे अभी तक इसे देखने के लिए गोल नहीं मिला है। मैं कुछ दिनों में जवाब दूंगा। – CJ7