O.K., so basically, one of my program's goals is to read strings from a specfied textfile randomly, so one random word will appear in a text box during each run. Ultimately, I'm making a hangman style game that will show dashes in the textbox until the user enters the correct letters to make the word appear. The only real change I made to the code was I made the array global.
Code:
public partial class frm1 : Form
{
public static string[] wordArray;
Code:
public void frm1_Load(object sender, EventArgs e)
{
StreamReader wordsIn;
try
{
wordsIn = new System.IO.StreamReader("myData.txt");
string strTemp = wordsIn.ReadToEnd();
wordsIn.Close();
wordArray = Regex.Split(strTemp, @"[\s]");
Randomize(wordArray); // here you get the array with random items.
}
catch (Exception ex)
{
MessageBox.Show("Error processing input file\n" + ex.Message);
}
int tester;
string teststring;
for (int i = 0; i > 0; i++)
{
teststring = wordArray[i];
txtWord.Text = teststring;
}
for (int i = 0; i > 0; i++)
{
tester = wordArray[i].Length;
txtLength.Text = tester.ToString();
}
}
I assume that if this were working properly, the first value of the randomized array would be displayed in the text box, and the length of the string in the other textbox. (I need the length for later when I figure out how many dashes to draw) What dumb thing have I done?
just read the array normally and randomize which one you choose to be the word chosen?
Code:
string[] items = items;
Random rnd = new Random();
rnd.Next(0, items.Length);
string ChosenWord = items[rnd];
and if you dont want the same word pulled twice just make an array of the numbers already pulled and iterate through that and make sure the rnd dosent equal?
might have misunderstood something, but good luck!
Bookmarks