codeRed
codeRed

Reputation: 53

Shifting an array using a variable

I have a list of 4 buttons that I display in my form, and I have a list of words (no set limit on the number of words). When the form starts, the fist 4 words in the word list appear on the buttons (one word for each button). I also have 2 more smaller buttons that are supposed to scroll the text values(words) on the larger buttons, either left or right.

Below, in the scrollRightButton_Click method, the words successfully shift to the left (scrolling right) changing the text values of the buttons in the button list.

However, in the scrollLeftButton_Click method, the text values are not scrolling left back to the beginning of the word list.

I use the variable named shiftCount to provide the shift of the index. This may or may not be the problem. I am not very familiar with the rules regarding working with array indexes. I write to the Console, the value of shiftCount, so you can see the value goes up when clicking right, and down when clicking left, but that int value does not seem to make the shift when clicking left.

I assume the problem is simple, but I cannot seem to successfully google a working solution. Any help will be appreciated.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace ButtonScroll
{
    public partial class MainUI : Form
    {
        const string CATSFILE = "categories.dat";

        List<Button> buttonList = new List<Button>();

        string listEntry;

        List<string> cats = File.ReadAllLines(CATSFILE).ToList();

        int shiftCount = 1;



        public MainUI()
        {
            InitializeComponent();

            // Add each button to the list 
            buttonList.Add(catButton1);
            buttonList.Add(catButton2);
            buttonList.Add(catButton3);
            buttonList.Add(catButton4);

            // Add category names to buttons
            for (int i = 0; i < buttonList.Count; i++)
            {
                listEntry = cats[i];
                buttonList[i].Text = listEntry;
            }
        }

        private void scrollRightButton_Click(object sender, EventArgs e)
        {
            int threshhold = cats.Count - 3;

            if (shiftCount < threshhold)
            {
                for (int i = 0; i < buttonList.Count; i++)
                {
                    listEntry = cats[i + shiftCount];
                    buttonList[i].Text = listEntry;
                }
                shiftCount++;
                Console.Write(shiftCount);
            }
        }

        private void scrollLeftButton_Click(object sender, EventArgs e)
        {
            if (shiftCount >= 2)
            {
                shiftCount--;
                for (int i = 0; i < buttonList.Count; i++)
                {
                    listEntry = cats[i + shiftCount];
                    buttonList[i].Text = listEntry;
                }

                Console.Write(shiftCount);
            }
        }
    }
}   

Sample categories.dat file

Drinks
Breakfast
Lunch
Dinner
Dessert
Party
Brunch

Upvotes: 0

Views: 49

Answers (1)

Hannes
Hannes

Reputation: 482

Try to start with int shiftCount = 0; and then set

private void scrollRightButton_Click(object sender, EventArgs e)  
{
    int threshhold = cats.Count - 3;

    if (shiftCount < threshhold)
    {
        shiftCount++;
        for (int i = 0; i < buttonList.Count; i++)
        {
            listEntry = cats[i + shiftCount];
            buttonList[i].Text = listEntry;
        }
        Console.Write(shiftCount);
    }
}

and change if (shiftCount >= 2) to if (shiftCount >= 1) in your scrollLeftButton_Click method

Upvotes: 1

Related Questions