2017-04-24 3 views
0

양식에 표시되는 4 개의 버튼 목록이 있으며 단어 목록 (단어 개수 제한 없음)이 있습니다. 양식이 시작되면 단어 목록의 첫 번째 4 단어가 버튼에 나타납니다 (각 버튼마다 한 단어 씩). 또한 왼쪽 또는 오른쪽의 큰 버튼에서 텍스트 값 (단어)을 스크롤해야하는 2 개의 작은 버튼이 있습니다.변수를 사용하여 배열 바꾸기

이하에서는 scrollRightButton_Click 메서드에서 단어가 성공적으로 왼쪽 (스크롤 오른쪽)으로 이동하여 단추 목록의 단추의 텍스트 값이 변경됩니다.

그러나 scrollLeftButton_Click 메서드에서 텍스트 값은 단어 목록의 시작 부분까지 왼쪽으로 스크롤되지 않습니다.

나는 shiftCount 변수를 사용하여 인덱스 이동을 제공합니다. 이것은 문제 일 수도 있고 아닐 수도 있습니다. 배열 인덱스 작업에 관한 규칙에 대해서는 잘 알고 있지 않습니다. 콘솔에 shiftCount의 값을 써서 오른쪽을 클릭하면 값이 올라가고 왼쪽을 클릭하면 값이 올라가는 것을 볼 수 있습니다.하지만 int 값은 왼쪽을 클릭 할 때 시프트를하지 않는 것처럼 보입니다.

나는 문제가 간단하다고 가정하지만 성공적으로 Google 솔루션을 사용할 수는 없습니다. 어떤 도움을 주시면 감사하겠습니다.

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); 
      } 
     } 
    } 
} 

샘플 categories.dat 파일

Drinks 
Breakfast 
Lunch 
Dinner 
Dessert 
Party 
Brunch 

답변

1

봅니다 int shiftCount = 0; 로 시작하고

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); 
    } 
} 

를 설정하고 scrollLeftButton_Click 방법 if (shiftCount >= 1)-if (shiftCount >= 2)을 변경