Как можно перемещать n символов от начала строки до конца непрерывно каждый раз, когда я нажимаю кнопку? - PullRequest
2 голосов
/ 25 мая 2019

Каждый раз, когда я нажимаю кнопку, я пытаюсь переместить первый символ строки в конец.

Кажется, моя логика снова и снова отображает только первый вывод после нажатия кнопки.

        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 = "";
        }
    }

например. если я введу «1234» в текстовое поле и нажму кнопку, мой вывод должен быть «2341», затем после того, как я снова нажму кнопку, выход должен переместиться на «3412» .. и т. д.

Ответы [ 3 ]

0 голосов
/ 25 мая 2019

Вы можете улучшить свой код с помощью другого решения, используя Метод подстроки

Создайте новую переменную с именем _number и установите значение 1

public partial class Form1: Form
{
    private int _number = 1; 
    // ....
}

Затемв событии Button вы можете заменить свой код этим кодом

    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

    }

enter image description here

0 голосов
/ 25 мая 2019

Это простой пример базовых операций с строками:

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.
    }

Вы можете увидеть пример SimpleStringOperation

0 голосов
/ 25 мая 2019

Вы берете свой ВЫХОД и помещаете его в Ярлык ... но продолжаете извлекать свой ВХОД из TextBox, который не изменился ... таким образом, каждый раз один и тот же результат.

Просто измените:

lblOutput.Text = manipulated.ToString();

Кому:

txtInput.Text = manipulated;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...