count in initially 0.
array list contain 3 questions at 0 1 2 index.
using click event on button it shows only the 0 index value whenever i click on button.
i want that whenever i click on button array index incremented and show the next question.
public void Button1_Click(object sender, EventArgs e)
{
if (count != 3)
{
lbl_question.Text = question_list[count].ToString();
rdb_op1.Text = op1_list[count].ToString();
rdb_op2.Text = op2_list[count].ToString();
rdb_op3.Text = op3_list[count].ToString();
rdb_op4.Text = op4_list[count].ToString();
count = count + 1;
}
declare a static variable so that it doesn't reset when you load the page
public static int count { get; set; }
and then use it in your event
public void Button1_Click(object sender, EventArgs e)
{
if (count != 3)
{
lbl_question.Text = question_list[count].ToString();
rdb_op1.Text = op1_list[count].ToString();
rdb_op2.Text = op2_list[count].ToString();
rdb_op3.Text = op3_list[count].ToString();
rdb_op4.Text = op4_list[count].ToString();
count = count + 1;
}
else
{
count = 0;
}
Related
I'm trying to remove to selected index in my Listbox but I get an error:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
Please help me, I'm a newbie to C#.
public void listBoxName_SelectedIndexChanged(object sender, EventArgs e)
{
int index = listBoxName.SelectedIndex;
labelName.Text = clients[index].Name;
labelAge.Text = clients[index].Age.ToString();
labelPhone.Text = clients[index].Phone;
labelAddress.Text = clients[index].Address;
}
private void buttonClearFields_Click(object sender, EventArgs e)
{
textBoxName.Text = "";
textBoxAge.Text = "";
textBoxPhone.Text = "";
textBoxAddress.Text = "";
}
private void buttonRemove_Click(object sender, EventArgs e)
{
listBoxName.Items.RemoveAt(listBoxName.SelectedIndex);
}
int index = listBoxName.SelectedIndex;
if (index < 0 || index > clients.Length) return;
listBoxName.Items.RemoveAt requires the passed index to be in the range 0..(n-1), where n is the number of items currently in listBoxName.
It looks like in your case listBoxName.SelectedIndex returns a negative value.
It could be either because listBoxName is empty, or it contains items but none is selected.
A possible fix would be:
private void buttonRemove_Click(object sender, EventArgs e)
{
int selIdx = listBoxName.SelectedIndex;
if (selIdx < 0 || selIdx >= listBoxName.Items.Count) return;
listBoxName.Items.RemoveAt(selIdx);
}
My question is about.
if i type easter bunny it needs to get shown 10x using the while statement.
private void textBox1_TextChanged(object sender, EventArgs e)
{
int paashaas = 0;
int p = paashaas;
while (p <= 4)
{
MessageBox.Show("The value of i is : " + paashaas);
paashaas = paashaas + 1;
}
}
This seems unreal to me
What you're doing in the below code is:
making an int (paashaas), setting it to 0,
then making another int (p), setting the value of the second int (p) also to 0,
then keep checking the second int (p) in the while loop, while changing the first int (paashaas) every time. Because of this, you will get stuck in the while loop. The value of p doesn't get changed, only the value of paashaas.
private void textBox1_TextChanged(object sender, EventArgs e)
{
int paashaas = 0;
int p = paashaas;
while (p <= 4)
{
MessageBox.Show("The value of i is : " + paashaas);
paashaas = paashaas + 1;
}
}
Also, i would use a for loop if i were you, if you want to do a certain action x times. For example take this code:
for(int i=1; i<=4; i++)
{
MessageBox.Show("The value of i is: " + i);
}
This code will show 4 message boxes, with the text:
The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
I think the working code that you're looking for is:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text == "easter bunny")
{
for(int i=0; i<10; i++)
{
MessageBox.Show($"You entered easter bunny in the textbox! {i}");
}
}
}
I am currently working on a homework assignment that I am struggling with. The goal of the program is to:
Create a program that allows users to record and view names of up to 1000 events. The names of the events are to be stored in an array. The names can be stored and viewed sequentially only: I,e, the first time the user presses “Set” to store a name, it would be placed in index 0 of the array; the second time the user stores a name it would be stored in index 1, etc. Similarly, the first time the user presses “View”, the item found in index 0 of the array is shown to the user; the second time the user presses “View”, the item found in index 1 of the array is shown, etc.
When the user presses the “Set” button (btnSet), an event name is inserted into the array in the next free index, sequentially as described above. The name to insert to the array is taken from txtEventName.
When the user presses the button “View” (btnView), the name of the next event to view sequentially (as described above) is shown to the user. The event name is shown to the user in txtName.
I currently have the code:
namespace ArrayHW
{
public partial class Form1 : Form
{
int[] eventsArray;
const int SIZE = 1000;
public Form1()
{
InitializeComponent();
}
private void btnSet_Click(object sender, EventArgs e)
{
eventsArray = new int[SIZE];
for (int index = 0; index < eventsArray.Length - 1; index++)
{
eventsArray[index] = Convert.ToInt32(txtEventName.Text);
}
}
private void btnView_Click(object sender, EventArgs e)
{
for (int index = 0; index < eventsArray.Length- 1; index++)
{
Debug.WriteLine(eventsArray[index]);
txtName.Text = Convert.ToString(eventsArray[index]);
}
}
}
}
When I run the form, I only get the result for index = 0,1, 2, 3, etc or whatever I had just input into the array in
private void btnSet_Click(object sender, EventArgs e)
{
eventsArray = new int[SIZE];
for (int index = 0; index < eventsArray.Length - 1; index++)
{
eventsArray[index] = Convert.ToInt32(txtEventName.Text);
}
}
rather than it showing up in sequential order like it is supposed to. Could anyone show me a better way to approach this problem, or help me find out what I am doing wrong? Thank you very much.
Please read the comments in the code block. Hopefully that will help you resolve your problem.
public partial class Form1 : Form
{
const int SIZE = 1000;
int[] eventsArray = new int[SIZE];
//as per your requirement, you would need these
//to display and set items at proper index in the array.
int _setIndex = 0;
int _viewIndex = 0;
public Form1()
{
InitializeComponent();
}
private void btnSet_Click(object sender, EventArgs e)
{
//this is where your problem is. Every time the Set btn is clicked,
//you are creating a new array. Therefore you are only seeing what you added in the
//last click. Anything that was added to the array on the prior clicks are lost because
//you just created a new array with the line below.
//eventsArray = new int[SIZE];
//You would need to comment the line above because this is code block is being
//executed on every btnSet click. New up this array only once by moving this to global scope.
//for (int index = 0; index < eventsArray.Length - 1; index++)
//{
// eventsArray[index] = Convert.ToInt32(txtEventName.Text);
//}
//Since we have created fields to keep track of _setIndex, all we need to do is:
if (_setIndex < SIZE)
{
eventsArray[_setIndex] = Convert.ToInt32(txtEventName.Text);
_setIndex++;
}
}
private void btnView_Click(object sender, EventArgs e)
{
//this wont be necessary.
//for (int index = 0; index < eventsArray.Length - 1; index++)
//{
// Debug.WriteLine(eventsArray[index]);
// txtName.Text = Convert.ToString(eventsArray[index]);
//}
if(eventsArray.Length > _viewIndex)
{
txtName.Text = Convert.ToString(eventsArray[_viewIndex]);
//this is to fulfill the requirement below:
// Similarly, the first time the user presses “View”, the item found in index 0
// of the array is shown to the user; the second time the user presses “View”,
// the item found in index 1 of the array is shown, etc.
_viewIndex++;
}
}
}
Try this:
int[] eventsArray = new int[SIZE];
int index = 0;
const int SIZE = 1000;
private void btnSet_Click(object sender, EventArgs e)
{
eventsArray[index++] = Convert.ToInt32(txtEventName.Text);
}
This button is populate which means click on this button will auto generate random numbers .
This is my code:
protected void Button1_Click(object sender, EventArgs e)
{
int rid = RandomNumber(-111, 999);
int rid1 = RandomNumber(-111, 999);
int rid2 = RandomNumber(-222, 888);
int rid3 = RandomNumber(-333, 777);
int rid4 = RandomNumber(-222, 777);
int rid5 = RandomNumber(-333, 444);
int rid6 = RandomNumber(-555, 888);
int rid7 = RandomNumber(444, 999);
int rid8 = RandomNumber(111, 222);
int rid9 = RandomNumber(222, 333);
txt1.Text = rid.ToString();
txt2.Text = rid1.ToString();
txt3.Text = rid3.ToString();
txt4.Text = rid4.ToString();
txt5.Text = rid5.ToString();
txt6.Text = rid6.ToString();
txt7.Text = rid7.ToString();
txt8.Text = rid8.ToString();
txt9.Text = rid9.ToString();
}
The second button is sort list.
How to take all the numbers and follow acceding to put back in the 9 different textbox ?
This is the coding for button sortlist:
protected void Button2_Click(object sender, EventArgs e)
{
int no1;
int no2;
int no3;
int no4;
int no5;
int no6;
int no7;
int no8;
int no9;
//int answer;
no1 = int.Parse(txt1.Text);
no2 = int.Parse(txt2.Text);
no3 = int.Parse(txt3.Text);
no4 = int.Parse(txt4.Text);
no5 = int.Parse(txt5.Text);
no6 = int.Parse(txt6.Text);
no7 = int.Parse(txt7.Text);
no8 = int.Parse(txt8.Text);
no9 = int.Parse(txt9.Text);
int[] a = new int[] {no1,no2,no3,no4,no5,no6,no7,no8,no9 };
Array.Sort(a);
foreach (var str in a)
{
MessageBox.Show(str.ToString());
}
}
I can display sort ACS in MessageBox but I can't put the number ACS into textbox
But still can't get the answer, where was wrong?
Thank you for help.
You could throw the generated numbers into a list, sort the list and assign the numbers accordingly. Thus, txt1.Text = sortedRandList[0]; and so on for the rest.
To get slightly cleaner code, you could consider also throwing all the text boxes within a list, and eventually end up doing textBoxesList[i] = sortedRandList[i];. That should clean up the code a little bit.
You can create List of ints and then sort it like this :
List<int> rids = null;
protected void Button1_Click(object sender, EventArgs e)
{
rids = new List<int>()
{
RandomNumber(-111, 999),
RandomNumber(-111, 999),
RandomNumber(-222, 888),
RandomNumber(-333, 777),
RandomNumber(-222, 777),
RandomNumber(-333, 444),
RandomNumber(-555, 888),
RandomNumber(444, 999),
RandomNumber(111, 222),
RandomNumber(222, 333)
};
DisplayValues(); // use it if you want to show your values in UI
}
protected void sortButton_Click(object sender, EventArgs e)
{
rids.Sort();
DisplayValues()
}
private void DisplayValues()
{
for (int i = 0; i < Controls.Count; i++)
{
if (Controls[i] is TextBox) if(Controls[i]).ID.Contains("txt"))
(Controls[i] as TextBox).Text = rids[Int32.Parse(Controls[i].ID.Replace("txt", "")) - 1].ToString();
}
}
Hi i am having a problem here. So you can see whenever the txtBtn0 and txtBtn1 is clicked it increments its own array which is then used for squareChecked string. But what I want to do first is to give out an error message if txtBtn0 nor txtBtn1 is not clicked. But it does not pop up anything.
public partial class MainForm : Form
{
public int[] clickNumBoxArray = Enumerable.Repeat(1, 81).ToArray();
public MainForm()
{
InitializeComponent();
} ... ... ...
private void btn1_Click(object sender, EventArgs e) {
UserSquare checkClickedBox = new UserSquare();
string checkClickBox = checkClickedBox.squareChecked();
if (checkClickedBox == null) {
MessageBox.Show("You did not enter any text on the form");
}
}
private void txtBtn1_Click(object sender, EventArgs e) {
clickNumBoxArray[1]++;
if (clickNumBoxArray[1] % 2 == 0) {
txtBtn1.BackColor = System.Drawing.Color.DarkOrange;
} else {
txtBtn1.BackColor = System.Drawing.Color.WhiteSmoke;
}
}
private void txtBtn0_Click(object sender, EventArgs e) {
clickNumBoxArray[0]++;
if (clickNumBoxArray[0] % 2 == 0) {
txtBtn0.BackColor = System.Drawing.Color.DarkOrange;
} else {
txtBtn0.BackColor = System.Drawing.Color.WhiteSmoke;
}
}
This is the other class
class UserSquare {
public string squareChecked() {
string clickedBoxes = null;
MainForm numBoxArray = new MainForm();
int[] clickNumBoxArray = numBoxArray.clickNumBoxArray;
for (int i = 0; i < 81; i++) {
if (clickNumBoxArray[i] % 2 == 0) {
clickedBoxes += "txtBtn" + i + ", ";
}
} return clickedBoxes;
}
The line:
for (int i = 0; i < 81; i++)
should be:
for (int i = 0; i < 80; i++)
The array clickNumBoxArray only had 80 elements, indices 0 to 79. You're looping through 81 items, indices 0 to 80.
About IndexOutOfRange Exception Your problem is that you implement your list here
public int[] clickNumBoxArray = Enumerable.Repeat(1, 80).ToArray();
as starts from 1 and finished 80 = 80 elements
but in method your for loop starts from 0 and finished 81 = 81 elements
and about controlling by error.. simply and tricky.. you can implement an internal / public boolean as default false and set to true in a mouse event as you need (i.e.mouseclick event)..end of your error method or where you need else, set to back false to be ready for another click-event controlling
About ButtonClick not fire its event (if im not misunderstood) : Did you deleted your some methods of buttons ? could you been forgot to implement it back ?
open your design mode click once onto your controls which you want to check, and then open properties => events.. if your events implemented then it should be as so : (i.e.)
(property) MouseClick (value) Button1_MouseClick
I fixed it by moving
public int[] clickNumBoxArray = Enumerable.Repeat(1, 81).ToArray();
to UserSquare class. I figured it out that UserSquare class only gets the value when program is run but not updating.