J'essaie de déplacer le premier caractère de la chaîne à la fin chaque fois que j'appuie sur le bouton.
Ma logique semble n'afficher que la première sortie encore et encore après avoir appuyé sur le bouton.
string input = "";
string manipulated = "";
int initial;
input = txtInput.Text;
if (txtInput.Text == String.Empty)
{
MessageBox.Show("Textbox is empty, please input a string.");
}
else
{
for (initial = 1; initial < input.Length; initial++)
{
manipulated += input[initial];
}
manipulated += input[0];
lblOutput.Text = manipulated.ToString();
input = manipulated;
manipulated = "";
}
Par exemple si j'entre "1234" dans la zone de texte et que j'appuie sur le bouton, ma sortie doit être "2341", puis après avoir appuyé à nouveau sur le bouton, la sortie doit passer à "3412" .. etc.
p >
3 Réponses :
Vous prenez votre OUTPUT et le placez dans un Label ... mais continuez à prendre votre INPUT de la TextBox qui n'a pas changé ... donc le même résultat à chaque fois.
p> Changez simplement:
txtInput.Text = manipulated;
En:
lblOutput.Text = manipulated.ToString();
Vous pouvez améliorer votre code par une autre solution en utilisant Méthode de sous-chaîne
Créez une nouvelle variable appelée _number et définissez la valeur sur 1
private void BtnMoveText_Click(object sender, EventArgs e)
{
if (txtInput.Text == string.Empty)
{
MessageBox.Show(@"TextBox is empty, please input a string.");
return;
}
if (_number > txtInput.TextLength)
_number = 1;
lblOutput.Text = txtInput.Text.Substring(_number) + txtInput.Text.Substring(0, _number);
_number++;
#region ** Depending on Microsoft **
/*
Substring(Int32)
(Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.)
Parameters
startIndex Int32
The zero-based starting character position of a substring in this instance.
.......................
Substring(Int32, Int32)
(Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length..)
Parameters
startIndex Int32
The zero-based starting character position of a substring in this instance.
length Int32
The number of characters in the substring.
*/
#endregion
}
Puis dans Button événement, vous pouvez remplacer votre code par ce code
public partial class Form1: Form
{
private int _number = 1;
// ....
}
Voici un exemple simple d'opérations de base sur les chaînes:
private void ManipulateBtn_Click(object sender, EventArgs e)
{
string input = InputTxt.Text; // Read the text from you Textbox in Windos form
if (input == string.Empty)
{
return;
}
string temp = input[0].ToString(); // Create a temp for the first char(toString) from you input
input = input.Remove(0,1); // Remove (from you input) At Index 0 (the idex from fist char in string) 1 time)
input += temp; //add the firs item from you input at the end of string
InputTxt.Text = input; // prin the result in the Textbox back.
}
Vous pouvez voir l'exemple SimpleStringOperation
c'est très utile. Cependant, je veux que ma sortie soit à lblOutput, ce qui signifie que le lblOutput.Text doit changer continuellement chaque fois que j'appuie sur le bouton et non le Input.Text, je peux l'utiliser comme référence. Merci beaucoup :)
Merci beaucoup @gtsonkov, cela m'aide vraiment beaucoup: D