2017-04-24 2 views
0

私は自分のフォームに表示する4つのボタンのリストを持っており、単語のリスト(単語数には制限がありません)があります。フォームが始まると、単語リストの最初の4単語がボタンに表示されます(各ボタンに1単語)。私はまた、左または右の大きなボタンのテキスト値(単語)をスクロールするはずの2つの小さなボタンを持っています。変数を使用して配列をシフト

以下、scrollRightButton_Clickメソッドでは、ボタンリスト内のボタンのテキスト値を変更して、正常に左にスクロールします(右にスクロールする)。

ただし、scrollLeftButton_Clickメソッドでは、テキスト値は単語リストの先頭までスクロールしません。

私は変数shiftCountを使ってインデックスのシフトを提供します。これは問題である場合とそうでない場合があります。配列インデックスを扱う際の規則にはあまり慣れていません。私はコンソールに、shiftCountの値を書きます。右をクリックすると値が上がり、左をクリックしたときに値が上がっているのがわかりますが、左をクリックしてもその整数値はシフトしていないようです。

私は問題は単純だと仮定しますが、私は正常に動作するソリューションであるとは思えません。どんな助けもありがとう。

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)を変更する

関連する問題