1

私の課題は、listBox に 100 個の乱数を表示することから始まりました。これはうまくいきます。次に、StreamWriter と StreamReader を使用して、結果を 2 番目の listBox に書き込んで読み戻します。ここで、いくつかのことが起こっています。100 行のそれぞれには ... System.Windows.Forms.ListBox.Items.Countというプレフィックスが付けられます。

そのようなフレーズを Google で検索しようとすると、表示されるのは MSDN ライブラリの labrynth だけであり、これに該当するものは何も見つかりませんでした。http://www.dotnetperls.com/listboxを紹介され ましたが、解決策も見つかりませんでした。また、読み戻された 100 エントリのそれぞれは最初のランダムにすぎず、100 すべてではありません。それは別の問題であることはわかっています。それは the_System.Windows.Forms.. の部分です。最初に修正する必要があります。

今日はこれに集中するために仕事を休みました。時間は飛んでいて、私はどこにも行きません。私は自分の教科書を分析し、狂ったように Google で検索しましたが、FaceBook で何の役にも立たない .NET ユーザー グループを見つけました。..他にどこに行けばいいのかわからない。

これまでの私のコードは次のとおりです...

/*  Matthew A. May  June 17th. & 22nd POS/409
 * This application simulates the roll of dice 100 times. The output is displayed in 
 * the listBox.  The Results can be saved (written) to a text file.  This txtFile can 
 * then be read back and redisplayed.  **  Some code is derived from...
 * Gaddis, T. (2012). Starting out with Visual C#® 2010 (2nd ed.). Boston, MA: Addison-Wesley.....
 * Page #s' are referenced accordingly throughout where applicable.  * 
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;    //required for the StreamWrite & StreamReader classes for txtFile functions. 

namespace WindowsFormsApplication1
{
    public partial class Main : Form
    {
        private int diceSide1;   // private accessor
        private int diceSide2;   //  Field Variable Declarations.
        private int SUM;         //  SUM = diceSide1 + diceSide2  
        const int Roll_MAX = 100;
        public Main()
        {
            InitializeComponent();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {
                //GroupBox...
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            /* When the user clicks a Write button, the program will write the sum of the dice for each roll into a sequential data file. 
            the SUM is an int= dye1 + dye 2. SUM cannot be a constant since its' value wil change with each roll. 
             */
        }

        private void btnRollEm(object sender, EventArgs e)
        {
        Random rand = new Random();  //creates new object from Random Class. Gaddis, T. (2012)Chptr5Pg321.

                for (int lineNum = 1; lineNum <= Roll_MAX; lineNum++)
                {
                    diceSide1 = rand.Next(6) + 1;   
                    diceSide2 = rand.Next(6) + 1;
                    SUM = diceSide1 + diceSide2;
                    lstBoxOut.Items.Add ("On roll , " + lineNum + " ,You rolled a " + diceSide1 + " and a "  + diceSide2 + " for a sum of "  + SUM);               
                } //end For-Loop. At this point, output is exactly as expected. 

        } //End btnRollEm

        private void btnWriteToFile(object sender, EventArgs e)
        {          
            StreamWriter rollLog;  //create StreamWriter object reference Variable. 
            rollLog = File.CreateText("Roll Results.txt");  //creating the File. 

            for (int count = 1; count <= 100; count++)
                {
                 rollLog.WriteLine(lstBoxOut);  
                //changing (lstBoxOut) to (count) shows 1-100 vertically.
                //Example Pg.298 & 301, showing where to write to.
                }   //End Write ForLoop

                 rollLog.Close();  //close file after creation.

            MessageBox.Show ("Your results have been successfully Saved to File.");
        }   //reviewing the txtFile, only the 1st line is written 100 times. 

        private void btnReadFile(object sender, EventArgs e)
        {
              //btnRead From File. Hides Main Form, Shows 2nd Form for txtFile OutPut.

             Form2 form2 = new Form2();
             form2.Show(); //Show // StreamReader. Read txt File.
             //// Declare a StreamReader variable.
             //StreamReader rollResults;

             //// Open the file and get a StreamReader object.
             //rollResults = File.OpenText("Roll Results.txt");
        }

        private void lstBoxOut_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        private void btnClear(object sender, EventArgs e)
        {
            lstBoxOut.Items.Clear(); //doesn't work.
        }
        private void btnExit(object sender, EventArgs e)
        {
            this.Close(); // Close the form.
        }
    }    //End Class Declaration
}       //End NameSpace Declaration
4

5 に答える 5

0

次のようにコードを変更してください

 private void btnWriteToFile(object sender, EventArgs e)
    {          
        StreamWriter rollLog;  //create StreamWriter object reference Variable. 
        rollLog = File.CreateText("Roll Results.txt");  //creating the File. 

        for (int count = 0; count <lstBoxOut.Items.Count;  count++)
            {
             rollLog.WriteLine(Convert.ToString(lstBoxOut.Items[count]));  
            //changing (lstBoxOut) to (count) shows 1-100 vertically.
            //Example Pg.298 & 301, showing where to write to.
            }   //End Write ForLoop

             rollLog.Close();  //close file after creation.

        MessageBox.Show ("Your results have been successfully Saved to File.");
    }   //reviewing the txtFile, only the 1st line is written 100 times. 
于 2013-06-24T20:30:02.777 に答える
0
for (int count = 1; count <= 100; count++)
{
    rollLog.WriteLine(lstBoxOut); 
}

これがおそらく問題の原因です。ループの反復ごとに、同じことをファイルに書き込んでいます (したがって、100 の同一の行があります)。ところで、これを見るための大きな手がかりは、ほとんどの場合 (常にではありません)、ループ変数 (この場合は「カウント」) がある場合、ループの本体のどこかでその変数を使用する必要があるということです。

次は、奇妙に見える "System.Windows.Forms.ListBox.Items.Count" データです。これは、そのファイルに書き込んでいるために発生してます。コンテンツではなく、リストボックス自体である「lstBoxOut」を書いています。デフォルトでは、これを行うと、ほとんどの WPF 型が型の名前に変換されます。おそらく書きたいことは、次のようなものです。

for (int count = 1; count <= 100; count++)
{
    rollLog.WriteLine(lstBoxOut.Items[count]); 
}
于 2013-06-24T20:30:33.753 に答える
0

「ファイルへの書き込み」メソッドでは、次のように指定します。

rollLog.WriteLine(lstBoxOut);

lstBoxOut が ListBox 項目であると想定するのは、以前のコードの一部、特に次のとおりです。

lstBoxOut.Items.Add ("On roll , " + lineNum + " ,You rolled a " + diceSide1 + " and a "  + diceSide2 + " for a sum of "  + SUM);

そうは言っても、それはリストボックスからアイテムを取得する方法ではありません。使用を検討してください

lstBoxOut.Items[count].ToString()

WriteLine コマンド内で使用するか、より適切な方法として、for ループの代わりに foreach ループを使用します。

于 2013-06-24T20:31:21.323 に答える