My problem is that I want to put a max number limit where the user would put a max question of 2 like an example and my program would show random question only 2 times but it keeps generating numbers I think my problem is with changing the textbox to int and where to place it I'm new to windows forms the first button generate random questions the second button should do the same but minus a number of question every time the user clicks I tried a lot operationbox is for the user to choose his operation(+,-,*,/) and the range box is the range of the random numbers.
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;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
rangebox1.Items.Add("[1,100]");
rangebox1.Items.Add("[1,500]");
rangebox1.Items.Add("[1,1000]");
Operationbox1.Items.Add("+");
Operationbox1.Items.Add("-");
Operationbox1.Items.Add("*");
Operationbox1.Items.Add("/");
}
int[] Rand(int v)
{
Random r = new Random();
int a = r.Next(1,v);
int b = r.Next(1,v);
int[] N = {Math.Max(a,b) , Math.Min(a,b)};
return N;
}
void generate()
{
int range = 0;
switch (rangebox1.SelectedIndex)
{
case 0:
range = 100;
break;
case 1:
range = 500;
break;
case 2:
range = 1000;
break;
default:
MessageBox.Show("Put a range !");
break;
}
int[] numbers = Rand(range);
switch (Operationbox1.SelectedIndex)
{
case 0:
questionlbl1.Text = string.Format("{0} + {1} =",numbers[0],numbers[1]);
break;
case 1:
questionlbl1.Text = string.Format("{0} - {1} =" , numbers[0], numbers[1]);
break;
case 2:
questionlbl1.Text = string.Format("{0} * {1} =" , numbers[0], numbers[1]);
break;
case 3:
questionlbl1.Text = string.Format("{0} / {1} =" , numbers[0], numbers[1]);
break;
default:
MessageBox.Show("Please insert a operation");
break;
}
}
void numberquest()
{
int numofquestionleft = Convert.ToInt32(numofquest1.Text);
int r = int.Parse(numofquest1.Text);
if (numofquestionleft > 0) generate();
numofquestionleft--;
}
private void genbutton1_Click(object sender, EventArgs e)
{
generate();
}
private void button1_Click(object sender, EventArgs e)
{
numberquest();
}
}
}
As far as I understood your question, you are facing issue with numofquestionleft variable and you want to restrict user to make max two number request. You need to modify your code. The way you are doing will not work. One solution could be,
Declare numofquestionleft as class level.
public partial class Form1 : Form
{
int numofquestionleft = 2; //here
public Form1()
{
And your numberquest() method would look like
void numberquest()
{
if (numofquestionleft > 0)
{
generate();
}
numofquestionleft--;
//You can use the textBox to communicate with user about number of turns left
//as
numofquest1.Text ="Request left " +numofquestionleft;
}
But if you still want to read max request from the textBox then the purpose of restricting users to make only two requests is lost (a user may enter some other number and you need to validate input in that case), moreover, you need to find a way to read data once.
Hope it helps.
Related
I am having problems with putting a max number of question limit I'm new to windows forms so I tried a changing the textbox of the questions number text box to int then putting a loop using (while();) but the program just freezes + how do I tell the user answer if his answer is right or wrong using (if) didn't work for me I don't know where to put it or if my way of changing the text box to int is right here is my code.
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;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
numrange.Items.Add("1,100");
numrange.Items.Add("1,500");
numrange.Items.Add("1,1000");
operation.Items.Add("+");
operation.Items.Add("-");
operation.Items.Add("*");
operation.Items.Add("/");
}
private void label1_Click(object sender, EventArgs e)
{
}
int[] Rand(int v)
{
Random r = new Random();
int a = r.Next(1,v);
int b = r.Next(1,v);
int[] N = { Math.Max(a,b) , Math.Min(a,b)};
return N;
}
void generate()
{
int range = 0;
switch (numrange.SelectedIndex)
{
case 0:
range = 100;
break;
case 1:
range = 500;
break;
case 2:
range = 1000 ;
break;
default :
MessageBox.Show("Please Insert a range!!");
break;
}
int[] Numbers = Rand(range);
int numofquest = Convert.ToInt32(maxquestion.Text);
int numofquestleft;
numofquestleft = numofquest;
while (numofquest > 0)
{
switch (operation.SelectedIndex)
{
case 0:
questionbox.Text = string.Format("{0} + {1} = ", Numbers[0], Numbers[1]);
break;
case 1:
questionbox.Text = string.Format("{0} - {1} = ", Numbers[0], Numbers[1]);
break;
case 2:
questionbox.Text = string.Format("{0} * {1} = ", Numbers[0], Numbers[1]);
break;
case 3:
questionbox.Text = string.Format("{0} / {1} = ", Numbers[0], Numbers[1]);
break;
default:
MessageBox.Show("Please insert an operation!!");
break;
}
numofquestleft--;
}
}
private void Startbutton1_Click(object sender, EventArgs e)
{
generate();
}
private void Nextbutton_Click(object sender, EventArgs e)
{
generate();
}
}
}
}
}
You did'nt change numofquest in the while loop, therefore numofquest > 0 is always true.
It should be while numofquestleft > 0. Be sure it;s numofquestleft and not numofquest
Aside from that, if you want to generate a different question each time you hit the button, consider putting the generate() with no loop inside the clickfunction, a loop will run all in one go. Something like this:
private int numofquestleft;
private int answer;
private void Start_Click(object sender, EventArgs e)
{
numofquestleft = Convert.ToInt32(maxquestion.Text);
if (numofquestleft > 0) generate();
numofquestleft--;
}
private void Nextbutton_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(userinput.Text) == answer){ MessageBox.Show("Correct");}
else {MessageBox.Show("Incorrect");}
if(numofquestleft > 0) generate();
numofquestleft--;
}
To check user's answer, first calculate the result:
switch(operation.SelectedIndex)
{
case 0:
questionbox.Text = string.Format("{0} + {1} = ",Numbers[0],Numbers[1]);
answer = Numbers[0] + Numbers[1];
break;
case 1:
questionbox.Text = string.Format("{0} - {1} = ",Numbers[0],Numbers[1]);
answer = Numbers[0] - Numbers[1];
break;
case 2:
questionbox.Text = string.Format("{0} * {1} = ",Numbers[0],Numbers[1]);
answer = Numbers[0] * Numbers[1];
break;
case 3:
questionbox.Text = string.Format("{0} / {1} = ",Numbers[0],Numbers[1]);
answer = Numbers[0] / Numbers[1];
break;
}
Then compare it with user input (convert to int), assuming when you hit Next:
private void Nextbutton_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(userinput.Text) == answer){ MessageBox.Show("Correct");}
else {MessageBox.Show("Incorrect");}
if(numofquestleft > 0) generate();
numofquestleft--;
}
This will check the answer of a question, then generate a new question when you click Next.
So I'm trying to get this Method of GetValidString(). Once I get this method done, the rest of the code should be easy.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReviewEchProject
{
class Program
{
//Arrays that can be called upon anytime
static string[,] employeeInfo = new string[20, 3]; //Used to contain First Name, Last Name, and Title
static int[] badgeArray = new int[20]; //Used to contain badge number
//Main method will be used to call upon the other methods
static void Main(string[] args)
{
Console.WriteLine("Welcome to the Employee Data Base. Please Type a letter to accept the command.");
Console.WriteLine();
Console.WriteLine("A = Add Employee E = Edit Employee S= Search Employee X= Exit");
string userInput = Console.ReadLine().ToUpper();
switch (userInput)
{
case "A":
AddEmployee();
break;
case "E":
EditEmployee();
break;
case "S":
SearchEmployee();
break;
case "X":
break;
default:
Console.WriteLine("Sorry, Invalid Input. Program will now shut down.");
break;
}
Console.WriteLine();
Console.WriteLine("Have a nice day!");
Console.ReadLine();
}
public static void SearchEmployee()
{
Console.WriteLine("I am in the SearchEmployee method");
}
public static void EditEmployee()
{
Console.WriteLine("I am in the EditEmployee method");
}
public static void AddEmployee()
{
for (int i = 0; i < badgeArray.Length; i = i + 1)
{
if (badgeArray[i] != 0)
{
Console.WriteLine("ERROR: Database is full.");
}
if (badgeArray[i] == 0)
{
int badge = GetEmployeeBadgeNumber();
badgeArray[i] = badge;
//Used to create badge number
//Not sure if done correctly, but call upon three times to place names for each slot
string firstName = GetValidString();
employeeInfo[i, 0] = firstName;
string lastName = GetValidString();
employeeInfo[i, 1] = lastName;
string titleName = GetValidString();
employeeInfo[i, 2] = titleName;
Console.WriteLine("Database upload ... ... Success.");
}
}
}
public static int GetEmployeeBadgeNumber()
{
//Needs to return an int that is at least 100 an no greater than 999.
//This method asks for the value from the user.
//Warn the user about this constraint, and stay in a loop until the user gets it right.
return 100;
}
public static string GetValidString()
{
//loop that it stays in until the user enters a valid entry, and when the user does enter a valid entry, it should return the value the user enters
//ask the user to enter a value with a message that uses the passed in messageString
//warn the user the input string must be must be at least 3 letters and less than 20. verify the length. if its not acceptable, write error message and loop again
return "Test";
}
}
}
Same conflicts apply. I'm thinking for the GetValidString() method, I should use a while loop to make sure that it can loop through everything.
**I am forced to use a 2D Array, so I have to use it.
There is far too much code here to write for you. I will however offer suggestions.
An Employee Class with properties for fName, lName, title, badge would make this easier than using 2D arrays
Create an Array of Employees => Employee[] Employees = new Employee[20]
Your methods would then turn into simple loops
First, my apologies if this is posted twice. I think the site may have burped.
I have an apparently impossible requirement for a clinical trials pharma application in my C# course. Please, someone tell me if I'm interpreting this part of the assignment incorrectly (copied verbatim from handout):
"MONTHS NUMBER OF TREATMENTS (use a Switch command)
1 -19 10 -30
20 - 39 31 -60
40 - 59 61 - 100
60 - 79 101 - 130
80 - 99 131 - 180
For Example:
If the randomly chosen # of months is from 20 through 39 - the number of treatments will be a random number from 31 through 60. If the # of months is 82 - the number of treatments is from 131 through 180."
The number of months the patient is available for the trial is input directly into a text box; my code supplies the random ACTUAL months the patient is participating in the clinical trial. I already know that ranges CANNOT be used in switch commands. Since this instructor also teaches Visual Basic, which apparently does support ranges in a switch command, she may have gotten the two confused (which, sadly, happens with alarming frequency). At any rate, everything I have read said this is flatly impossible with a switch case.
I have no idea how to accomplish this without an if/else tree instead of a switch. Can anyone give me any ideas? PLEASE, no regex or LINQ. We are not covering either one.
Here's all of my code. What is here works; file read/write isn't implemented yet, but I already understand that part.
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.Windows.Forms;
using System.IO;
namespace NallyKProgram3ClinicalTrials
{
public partial class NallyKClinicalTrialsForm : Form
{
public NallyKClinicalTrialsForm()
{ InitializeComponent(); }
private StreamWriter outputFile;
private StreamReader inputFile;
int numberOfMonthsInTrialRangeInteger;
int numberAssignedToPatientInteger;
int numberOfMonthsInTrialInteger;
int numberOfTreatmentPlanInteger;
int numberOfTreatmentsInteger;
// const string CASE_STRING; this attempt didn't work
Random rndNumber = new Random();
Boolean duplicateNameBoolean;
private void NallyKClinicalTrialsForm_Load(object sender, EventArgs e)
{
reportLabel.ResetText();
removePatientButton.Enabled = false;
writeFileButton.Enabled = false;
}
private void addPatientButton_Click(object sender, EventArgs e)
{
if (patientNameTextBox.Text.Trim() != string.Empty)
{
checkForDuplicateNames();
generatePatientNumber(rndNumber);
if (duplicateNameBoolean == false)
{
if (numberOfMonthsTextBox.Text.Trim() != string.Empty)
{
if (int.TryParse(numberOfMonthsTextBox.Text.Trim(), out numberOfMonthsInTrialRangeInteger))
{
if (numberOfMonthsInTrialRangeInteger < 1 || numberOfMonthsInTrialRangeInteger > 99)
{
errorProvider1.SetError(numberOfMonthsTextBox, "The number of months available for participation in this trial must not be less than 1 or greater than 99.");
numberOfMonthsTextBox.Focus();
numberOfMonthsTextBox.SelectAll();
}
else
{
generateNumberOfMonthsInTrial(rndNumber);
generateTreatmentPlan(rndNumber);
// determineCaseString(); NOPE!
// determineNumberOfTreatments(rndNumber); TO DO
addToListBox();
addToNamesListBox();
readFileButton.Enabled = false;
removePatientButton.Enabled = true;
writeFileButton.Enabled = true;
}
}
else
{
errorProvider1.SetError(numberOfMonthsTextBox, "Please enter a number.");
numberOfMonthsTextBox.Focus();
numberOfMonthsTextBox.SelectAll();
}
}
else
{
numberOfMonthsTextBox.Text = " ";
errorProvider1.SetError(numberOfMonthsTextBox, "You must enter a number of months.");
numberOfMonthsTextBox.Focus();
numberOfMonthsTextBox.SelectAll();
}
}
else
{
errorProvider1.SetError(namesListBox, patientNameTextBox.Text + " is a duplicate name. Please enter a different name.");
patientNameTextBox.Focus();
patientNameTextBox.SelectAll();
}
}
else
{
patientNameTextBox.Text = " ";
errorProvider1.SetError(patientNameTextBox, "You must enter a patient name.");
patientNameTextBox.Focus();
patientNameTextBox.SelectAll();
}
}
private void checkForDuplicateNames()
{
int indexInteger = 0;
duplicateNameBoolean = false;
while (indexInteger < namesListBox.Items.Count)
{
if (patientNameTextBox.Text.ToLower().Trim() ==
namesListBox.Items[indexInteger].ToString().ToLower())
{
duplicateNameBoolean = true;
indexInteger = namesListBox.Items.Count;
}
indexInteger++;
}
}
private Random generatePatientNumber(Random rndNumber)
{
numberAssignedToPatientInteger = rndNumber.Next(1000, 9999);
return rndNumber;
}
private Random generateNumberOfMonthsInTrial(Random rndNumber)
{
numberOfMonthsInTrialInteger = rndNumber.Next(1, numberOfMonthsInTrialRangeInteger);
return rndNumber;
}
private Random generateTreatmentPlan(Random rndNumber)
{
numberOfTreatmentPlanInteger = rndNumber.Next(1, 5);
return rndNumber;
}
//private void determineCaseString() NOPE, NOPE, NOPE
//{
// if ((numberOfTreatmentPlanInteger >= 1) && (numberOfTreatmentPlanInteger < 20)) CASE_STRING = "a"; // clever, but error!
// if ((numberOfTreatmentPlanInteger >= 20) && (numberOfTreatmentPlanInteger < 40)) CASE_STRING = "b";
// if ((numberOfTreatmentPlanInteger >= 41) && (numberOfTreatmentPlanInteger < 60)) CASE_STRING = "c";
// if ((numberOfTreatmentPlanInteger >= 60) && (numberOfTreatmentPlanInteger < 80)) CASE_STRING = "d";
// if ((numberOfTreatmentPlanInteger >= 80) && (numberOfTreatmentPlanInteger < 100)) CASE_STRING = "e";
//}
//private Random determineNumberOfTreatments(Random rndNumber)
//{
// numberOfTreatmentsInteger = rndNumber.Next(10, CASE_STRING);
//}
private void addToListBox()
{
patientInformationListBox.Items.Insert(0, numberAssignedToPatientInteger.ToString() + "," + numberOfTreatmentPlanInteger + "," + numberOfMonthsInTrialInteger + "," + Environment.NewLine); // number of treatments goes after the final comma
}
private void addToNamesListBox()
{
namesListBox.Items.Insert(0, patientNameTextBox.Text.Trim());
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void removePatientButton_Click(object sender, EventArgs e)
{
if (patientInformationListBox.SelectedIndex > -1)
patientInformationListBox.Items.RemoveAt(patientInformationListBox.SelectedIndex);
else MessageBox.Show("You must select an entry you wish to remove from the list.", "SELECT AN ITEM TO REMOVE", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void patientNameTextBox_TextChanged(object sender, EventArgs e)
{
errorProvider1.SetError(patientNameTextBox, "");
errorProvider1.SetError(patientInformationListBox, "");
}
private void numberOfMonthsTextBox_TextChanged(object sender, EventArgs e)
{
errorProvider1.SetError(numberOfMonthsTextBox, "");
}
}
}
You're right in that C# does not allow you to use ranges in the switch-statement. In fact, C# only allows you to write a case X: where X is a single constant literal value.
However, C# does allow you to fall through from one case to another, and thus allows you to list all the cases for which you can write one single body of code to execute.
As such, you can write the code like this:
switch (months)
{
case 1:
case 2:
case 3:
...
case 19:
treatments = r.Next(10, 31);
break;
case 20:
case 21:
case 22:
...
case 39:
treatments = r.Next(30, 61);
break;
case 40:
...
Now, is that the best you can do?
No. You can notice a pattern in the ranges here. Each range goes from X to X+19, where X is a number divisible by 20, so you can use the fact that integer division will truncate downwards:
int monthsSection = months / 20; // 0-19 --> 0, 20-39 --> 1, etc.
switch (monthsSection)
{
case 0: // 0-19
treatments = r.Next(10, 31);
break;
case 1: // 20-39
treatments = r.Next(30, 61);
break;
case 2: // 40-59
...
Yes, this will include 0 as well. If that is a problem then I would simply add a specific if-statement in front to filter it out.
If you have to use a switch, you could divide your months by 20, and then do a switch on that. Something like this:
int monthValue = numberOfTreatmentPlanInteger / 20;
switch (monthValue)
{
case 0:
// between 10 and 30 treatments
break;
case 1:
// between 31 and 60 treatments
break;
case 2:
// between 61 and 100 treatments
break;
// and so on
}
I'm trying to get my program to start the user with 20 lives and while Lives is greater than 0, flip a coin (random number generator 0(Tails) or 1(Heads)). Every time the user gets Heads it adds 1 to Heads counter and 1 to Flips counter. Every time the user gets Tails it takes 1 from Lives and adds 1 to Flips counter. I think my only problem is my random number generator!? Please be kind, total n00b :)
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.Windows.Forms;
namespace Coin_Flip_2015
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Random r = new Random(1);
private void cmdFlipCoin_Click(object sender, EventArgs e)
{
int rand = r.Next(1);
int Heads;
int counterFlips;
int Lives;
Lives = 20;
Heads = 0;
counterFlips = 0;
while (Lives > 0)
if (rand == 0)
{
Lives = Lives -= 1;
counterFlips++;
}
else if (rand == 1)
{
Heads = Heads += 1;
counterFlips++;
}
lblFlips.Text = "Flips = " + counterFlips.ToString();
lblHeads.Text = "Heads = " + Heads.ToString();
lblLivesLeft.Text = "Lives Left = " + Lives.ToString();
MessageBox.Show("sorry you are out of lives m8");
}
}
}
The Random.Next(int) method
Returns a non-negative random integer that is less than the specified maximum.
So r.Next(1) is always 0. Use r.Next(2) to get 0 or 1.
You should also move the rand = r.Next(1); inside the while loop. Now it's only assigned once, at the beginning of the method.
I'll take a swing at fixing all the issues:
public partial class Form1 : Form
{
int Heads, counterFlips, Lives;
public Form1()
{
InitializeComponent();
Lives = 20;
Heads = 0;
counterFlips = 0;
}
private Random r = new Random();
private void cmdFlipCoin_Click(object sender, EventArgs e)
{
int rand = r.Next(2);
if (Lives > 0)
{
if (rand == 0)
{
Lives -= 1;
counterFlips++;
}
else
{
Heads += 1;
counterFlips++;
}
}
else
MessageBox.Show("sorry you are out of lives m8");
lblFlips.Text = "Flips = " + counterFlips.ToString();
lblHeads.Text = "Heads = " + Heads.ToString();
lblLivesLeft.Text = "Lives Left = " + Lives.ToString();
}
}
Issues:
The Heads, counterFlips and Lives variables were stored locally, they need to be global
The code was changed so that you flip a coin each time you click the button (changed the while to an if). while loops that have the capability of never exiting should not be used in UI threads.
private Random r = new Random() notice removed the 1, use the default constructor to seed it properly.
Added an "else" to show when the lives were used, it shows your message
Changed the Lives = Lives -= 1 (same with Heads) to proper code
Used r.Next(2) to get either 0 or 1, since the upper bound is exclusive.
Other than that, I'm not sure what you are doing with the Lives and Heads numbers, so I'll leave that up to you to fix.
Here one that is a bit different.. snap beat me by a few seconds!
public partial class Form1 : Form
{
// Leave the seed alone on this line
private readonly Random _r = new Random();
// these need to be outside of your Click event
private int _counterFlips;
private int _heads;
private int _lives = 20;
public Form1()
{
InitializeComponent();
}
private void cmdFlipCoin_Click(object sender, EventArgs e)
{
// x = starting number, y = ending number + 1
// This should be either a zero or one:
int rand = _r.Next(0, 2);
switch (rand)
{
default:
_lives --;
_counterFlips++;
break;
case 1:
_heads ++;
_counterFlips++;
break;
}
if (_lives < 1)
{
MessageBox.Show(#"sorry you are out of lives m8");
button1.Enabled = false;
return;
}
lblFlips.Text = #"Flips = " + _counterFlips;
lblHeads.Text = #"Heads = " + _heads;
lblLivesLeft.Text = #"Lives Left = " + _lives;
}
}
I have a main array with a number of items, e.g. 100 (itemsArray)
I have second array of 7 items that is filled from the main array depending on what is selected (selectedItemsArray)
the second array is output to the screen, when i press left or right the next item or previous item in the array is displayed, however the 3 previous are displayed before and the next 3 are displayed after.
however once the end of the array is reached (or the item goes below 0) it crashes (not a surprise) but how can i calculate what number in the array should be selected once the end or start of 0 is reached
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.Windows.Forms;
namespace test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] itemsArray = new string[100];
int selectedIndex = 0;
private void displayItems(string[] items)
{
StringBuilder output = new StringBuilder("");
for (int i = 0; i < items.Count(); i++)
{
output.AppendLine(items[i]);
}
textBox1.Text = output.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < itemsArray.Count(); i++)
{
itemsArray[i] = "Item " + i;
}
callItems();
}
private void callItems()
{
string[] selectedItemsArray = new string[7];
Array.Copy(itemsArray, selectedIndex, selectedItemsArray, 0, 7);
displayItems(selectedItemsArray);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
case Keys.Left:
{
selectedIndex--;
callItems();
break;
}
case Keys.Right:
{
selectedIndex++;
callItems();
break;
}
}
}
}
}
i hope that makes sense and appreciate any help anyone can give me
It should work if you'd change this:
Array.Copy(itemsArray, selectedIndex, selectedItemsArray, 0, 7);
displayItems(selectedItemsArray);
to
for(int i=0; i<7; i++)
{
selectedItemsArray[i] = itemsArray[(i+selectedIndex)%itemsArray.Length];
}
Also, you may want to make sure your selectedIndex won't ge below 0 in your KeyDown handlers
case Keys.Left:
{
if(selectedIndex!=0)
{
selectedIndex--;
}
else
{
selectedIndex = itemsArray.Length-1;
}
callItems();
break;
}
same for going right side
case Keys.Right:
{
if(selectedIndex != itemsArray.Length-1)
{
++selectedIndex;
}
else
{
selectedIndex = 0;
}
callItems();
break;
}
And on top of that I'd make sure non of those will be fired, if there are no items in the itemsArray at all, because it won't work then.