C# tutorials
20. Back and forward buttons i

Note: While reading this page, bear in mind that I'm no computer expert and that the text below may be partly inaccurate. If you find errors or have proposals for improvements, please send me a message and help make this a better page for the benefit of future visitors. To the left, there are links to more C# tutorials.


To complete this tutorial, follow these instructions:

1. Open Visual C# 2010 Express.

2. Click on New Project in the File menu.

3. Choose Windows Forms Application if that option isn't already chosen, change the name if you like and click on OK.

4. Point at Toolbox in the left margin and click on the Auto Hide icon at the top of the window (to keep the window to stay open and easier to work with).

5. Drag two Buttons and a RichTextBox to Form 1. button1 is the back button and button2 the forward button. You can rename the buttons if you like. Remember, though, that the names button1 and button2 are used below.

6. Double-click on button1 and write or paste this code where the caret is:

try
{
  if (richTextBox1.Text == string.Empty)
  {
    i = 0;
    MessageBox.Show("Click on button2 to display the first word.");
  }
  else if (i == 0)
  {
    MessageBox.Show("You have reached the first word.");
  }
  else
  {
    i = i - 1;
    richTextBox1.Text = str[i];
  }
}
catch (Exception excMsg)
{
  MessageBox.Show(excMsg.Message.ToString(), "Error");
}

7. Double-click on button2 and write or paste this code where the caret is:

try
{
  if (richTextBox1.Text == string.Empty)
  {
    i = 0;
    richTextBox1.Text = str[0];
  }
  else if (i == str.Length - 1)
  {
    MessageBox.Show("You have reached the last word.");
  }
  else
  {
    i = i + 1;
    richTextBox1.Text = str[i];
  }
}
catch (Exception excMsg)
{
  MessageBox.Show(excMsg.Message.ToString(), "Error");
}

8. Write or paste this code right above private void button1_Click(object sender, EventArgs e):

int i = 0;
string[] str = { "from", "the", "beginning", "to", "the", "end" };

9. Press F5 to start debugging the program.

10. Click on button1. A MessageBox with the message Click on button2 to display the first word should appear.

11. Close the MessageBox and click several times on button2. All the words in the array str should appear one at a time. After the last word is reached, a MessageBox with the message You have reached the last word should appear.

12. Close the MessageBox and click several times on button1. All the words in the array str should appear one at a time in reversed order. After the first word is reached, a MessageBox with the message You have reached the first word should appear.

13. Delete the word in richTextBox1 and repeat step 10 and 11. You should get the same response as you got the first time you did these steps.

Comments on the first code snippet

Line 3-7: If the RichTextBox is empty, set i to 0 (in case the text in the RichTextBox has been deleted and i has some other value at the moment) and display a message.

Line 8-11: If i is 0 and the first word in the array (with index 0) is displayed, display a message.

Line 12-16: If the RichTextBox isn't empty and i isn't 0, subtract 1 from i and display the word with that index in the array.

Comments on the second code snippet

Line 3-7: If the RichTextBox is empty, set i to 0 (in case the text in the RichTextBox has been deleted and i has some other value at the moment) and display the first word in the array (with index 0).

Line 8-11: If i is the length of the array minus 1 and the last word in the array (with index 5) is displayed, display a message. - 1 in line 6 is needed because array index starts with 0 and Length with 1.

Line 12-16: If the RichTextBox isn't empty and i isn't the length of the array minus 1, add 1 to i and display the word with that index in the array.

Comments on the third code snippet

Line 1: Set the initial value of the int i to 0.

Line 2: The array str should be replaced with something more useful.


Would you like to comment on this page or some other page? Use the contact form. Write the title of the page you want to comment on and your comment in the field Message.