UpDownBase.Select(Int32, Int32) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Sélectionne une plage de texte dans la zone de rotation (également appelée contrôle up-down) spécifiant la position de départ et le nombre de caractères à sélectionner.
public:
void Select(int start, int length);
public void Select(int start, int length);
override this.Select : int * int -> unit
Public Sub Select (start As Integer, length As Integer)
Paramètres
- start
- Int32
Position du premier caractère à sélectionner.
- length
- Int32
Nombre total de caractères à sélectionner.
Exemples
L’exemple de code suivant utilise la classe NumericUpDowndérivée . Ce code nécessite qu’un NumericUpDown contrôle et un Button contrôle aient été créés sur un formulaire et que l’espace System.Drawing de noms a été ajouté en tant que référence. Sur l’événement Click du bouton, la taille du point de texte dans le NumericUpDown contrôle augmente. Cela invite le contrôle à ajuster sa PreferredHeight propriété afin que tout le texte soit visible dans le contrôle. Une fois que l’utilisateur entre une nouvelle valeur et quitte le NumericUpDown contrôle, le texte est converti en valeur numérique à partir d’une valeur de chaîne et validé entre les valeurs et Maximum les Minimum valeurs. Si la valeur n’est pas valide, une MessageBox valeur s’affiche avec l’erreur et la Select méthode sélectionne le texte afin que l’utilisateur puisse entrer une nouvelle valeur.
void numericUpDown1_Leave( Object^ /*sender*/, EventArgs^ /*e*/ )
{
/* If the entered value is greater than Minimum or Maximum,
select the text and open a message box. */
if ( (System::Convert::ToInt32( numericUpDown1->Text ) > numericUpDown1->Maximum) || (System::Convert::ToInt32( numericUpDown1->Text ) < numericUpDown1->Minimum) )
{
MessageBox::Show( "The value entered was not between the Minimum andMaximum allowable values.\nPlease re-enter." );
numericUpDown1->Focus();
numericUpDown1->Select(0,numericUpDown1->Text->Length);
}
}
void button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
int varPrefHeight1;
/* Capture the PreferredHeight before and after the Font
is changed, and display the results in a message box. */
varPrefHeight1 = numericUpDown1->PreferredHeight;
numericUpDown1->Font = gcnew System::Drawing::Font( "Microsoft Sans Serif",12.0,System::Drawing::FontStyle::Bold );
MessageBox::Show( String::Format( "Before Font Change: {0}\nAfter Font Change: {1}", varPrefHeight1, numericUpDown1->PreferredHeight ) );
}
private void numericUpDown1_Leave(Object sender,
EventArgs e)
{
/* If the entered value is greater than Minimum or Maximum,
select the text and open a message box. */
if((System.Convert.ToInt32(numericUpDown1.Text) > numericUpDown1.Maximum) ||
(System.Convert.ToInt32(numericUpDown1.Text) < numericUpDown1.Minimum))
{
MessageBox.Show("The value entered was not between the Minimum and" +
"Maximum allowable values." + "\n" + "Please re-enter.");
numericUpDown1.Focus();
numericUpDown1.Select(0, numericUpDown1.Text.Length);
}
}
private void button1_Click(Object sender,
EventArgs e)
{
int varPrefHeight1;
/* Capture the PreferredHeight before and after the Font
is changed, and display the results in a message box. */
varPrefHeight1 = numericUpDown1.PreferredHeight;
numericUpDown1.Font = new System.Drawing.Font("Microsoft Sans Serif",
12F, System.Drawing.FontStyle.Bold);
MessageBox.Show("Before Font Change: " + varPrefHeight1.ToString() +
"\n" + "After Font Change: " + numericUpDown1.PreferredHeight.ToString());
}
Private Sub numericUpDown1_Leave(sender As Object, e As EventArgs)
' If the entered value is greater than Minimum or Maximum,
' select the text and open a message box.
If (System.Convert.ToInt32(numericUpDown1.Text) > numericUpDown1.Maximum) Or _
(System.Convert.ToInt32(numericUpDown1.Text) < numericUpDown1.Minimum) Then
MessageBox.Show("The value entered was not between the Minimum and " & _
"Maximum allowable values." & Microsoft.VisualBasic.ControlChars.Cr & _
"Please re-enter.")
numericUpDown1.Focus()
numericUpDown1.Select(0, numericUpDown1.Text.Length)
End If
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs)
Dim varPrefHeight1 As Integer
' Capture the PreferredHeight before and after the Font
' is changed, and display the results in a message box.
varPrefHeight1 = numericUpDown1.PreferredHeight
numericUpDown1.Font = New System.Drawing.Font("Microsoft Sans Serif", _
12F, System.Drawing.FontStyle.Bold)
MessageBox.Show("Before Font Change: " & varPrefHeight1.ToString() & _
Microsoft.VisualBasic.ControlChars.Cr & "After Font Change: " & _
numericUpDown1.PreferredHeight.ToString())
End Sub
Remarques
La Select méthode peut être utilisée lorsque la zone de rotation obtient le focus ou lorsque la Text propriété échoue à la validation des données. Lors de l’ajout du code de validation pour la ValidateEditText méthode dans une classe dérivée, appelez la Select méthode en cas d’échec de la validation.