How to increment textbox when a button is pressed in another page - c#

So this is really bugging me - I am trying to make it that everytime a item is chosen in one page a textbox in another page should be incremented.
If items are greater than 0 txtbox should increment textbox
if ((Menu.VanillaQ > 0) || (Menu.LCQ > 0) || (Menu.ChocQ > 0) )
{
Orders++;
NoOfOrder.Text = Orders.ToString();
}
}
Menu Class declaring variables
public static double VanillaQ;
public static double LCQ;
public static double ChocQ;
//Code for one Item
if (VanillaQ <= 100)
{
VanillaQ = VanillaQ+1;
txtAmount.Text = VanillaQ.ToString(); //Gets quantity of each item and sets it as a string
}
Make new order btn-sets all variables to 0
private void Orderbtn_Click(object sender, RoutedEventArgs e)
{
Menu.BanQ = 0;
Menu.VanillaQ = 0;
Menu.StrawQ = 0;
Menu.MintQ = 0;
Menu.ChocQ = 0;
Menu.CurryQ = 0;
Menu.EggQ = 0;
Menu.LagerQ = 0;
Menu.LCQ = 0;
Frame.Navigate(typeof(Type));
}
Right now it only increments one time no matter how many loops I take. And after I press the Make new order button the variables are all resetted to 0 therefore after if item is chosen variable will be greater than 0 and textbox should be incremented. But it only increments once which really confuses mr :s

Related

How to check range in checkedlistbox c# winforms?

I have a checkeboxlist with 100 items. Obviously user can check items one by one as many as he need, but I would like to give to user option check range of items (let's say with Shift hold button). So, user check one of the items (let's say item index 5) and then press and hold shift button and check next item (index 10), so I range of the items should be checked from 5...10
I have not found anything about such implementation, looks like it doesn't exist and no one did such kind of things.
How to do it?
Keep track of your last index:
int lastIndex = -1;
In your form's constructor, wire things up:
public Form1() {
InitializeComponent();
checkedListBox1.CheckOnClick = true;
checkedListBox1.SelectedIndexChanged += CheckedListBox1_SelectedIndexChanged;
checkedListBox1.MouseDown += CheckedListBox1_MouseDown;
}
And then use these methods to change the items in the range:
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e) {
lastIndex = checkedListBox1.SelectedIndex;
}
private void CheckedListBox1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left && Control.ModifierKeys == Keys.Shift) {
var useIndex = Math.Max(lastIndex, 0);
var x = checkedListBox1.IndexFromPoint(e.Location);
if (x > -1 && x != useIndex) {
if (useIndex > x) {
for (int i = useIndex - 1; i > x; i--) {
checkedListBox1.SetItemChecked(i, !checkedListBox1.GetItemChecked(i));
}
} else {
for (int i = useIndex + 1; i < x; i++) {
checkedListBox1.SetItemChecked(i, !checkedListBox1.GetItemChecked(i));
}
}
}
}
}

C# How to assign i and j in 2D array when i press on a button

I was asked to make a tic tac toe in windows form and i have to use 2D array
I am trying to store 1 in the array for each X and -1 for each 0
then i will add the values in each row ,column and diagonal and check if its 3 or -3
the problem is i don't know how to assign and i and j for each element in the array after i press on a corresponding button
private void storeInboard(int i, int j, object sender, EventArgs e)
{
{
if ((sender as Button).Text == "X")
board[i][j] = 1;
else if ((sender as Button).Text == "O")
board[i][j] = -1;
}
}
here i check for each column by giving its number
private bool checkCol(int col)
{
for (int i = 0; i < 3; i++)
{
rowSum += board[i][col];
if (colSum == 3 || colSum ==-3 )
return true;
else
colSum = 0;
}
return false;
}
checking for winner
private bool checkWinner()
{
return (checkCol(0) || checkCol(1) || checkCol(2) || checkDiag1() || checkDiag2() || checkRow(0) || checkRow(1) || checkRow(2));
}
here is the button click event >> its is assigned for all the buttons
private void button_click(object sender, EventArgs e)
{
if (turn)
{
(sender as Button).Text = "X";
}
else
(sender as Button).Text = "O";
turn = !turn;
turnCount++;
(sender as Button).Enabled = false;
if (checkWinner() && turnCount <=9)
MessageBox.Show("Winner !!!");
else
MessageBox.Show("Tie -.-");
so I just want to know how can i send an i and j for the event storeInboard for each button i click
thanks in advance
This question is not about arrays or some 2D stuff but binding information to winform controls.
You have several ways to do this.
1) In visual studio property panel fill buttons Tag property with an ordinal number one by one from left to right starting with zero. Left-upper button = 0, middle-upper = 1 and so on.
Then in button_click you can do something like:
var tag = int.Parse((string)((Button)sender).Tag);
int colIndex = tag % 3;
int rowIndex = tag / 3;
2) Maybe the buttons are placed in a Panel. Then you can iterate through the children of the parent panel in button_click and see if one equals to sender.
int c = 0;
foreach (var item in parentPanel.Controls)
{
if (item == sender)
break;
c++;
}
if (c<parentPanel.Controls.Count) // found
{
int colIndex = c % 3;
int rowIndex = c / 3;
}
// else -- ugh, something went wrong, maybe not only the buttons have this event
Much more solution is possible.

Using an if statement to see if a button is clicked in Windows Forms C#

I have a Windows Form application that is being used to practice the Bubble Sort algorithm. Basically I have four different radio buttons which each displays a certain number of random integers in a listbox when clicked. What I want is to be able to click a button that says "Sort" to then sort all the integers in that listbox. Since each radio button displays different integers in the listbox, I have to do this under the radioButton1_CheckedChange method, so then I may have easy access to the random array that has been created for the listbox. I am currently struggling with getting my code to make the button1_Clicked event handler to be set to "true" when the button is clicked. Here's an excerpt of my code below to show you guys what I mean.
bool buttonClick = false;
private void button1_Click(object sender, EventArgs e)//When button is clicked, button click is true
{
buttonClick = true;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)//Max 100 button
{
int smallMaxSize = 101;
int[] array = new int[smallMaxSize];
Random numGenerator = new Random();
numGenerator.Next(smallMaxSize);
for (int i=0; i<101; i++)//Generates 100 random numbers from 1-100
{
array[i] = numGenerator.Next(smallMaxSize);
Numbers.Items.Add(array[i]);
}
if (buttonClick == true)
{
Numbers.Items.Clear();
int Out;
int In;
for (Out = smallMaxSize - 1; Out > 1; Out--)
{
for (In = 0; In < Out; In++)
{
if (array[In] > array[In + 1])
{
int temp = array[In];
array[In] = array[In + 1];
array[In + 1] = temp;
}
}
}
}
As you can see, I've set up a private bool for the button click, which is always set to false. Under the button1_Click event handler, I have that change to true whenever the "Sort" button is clicked. Then I have an "if" statement under the Radio Button method that says "if buttonClick == true, run this code". When I run the code and click the "Sort" Button, nothing happens. Anyone have any ideas that could lead me in the right direction?
Windows Forms is event driven programming meaning that things only happen when an event occurs, this is generally a user interaction with the form, but it can be a timer, loop, etc.
Your problem is that you have your Bubble Sort code in radioButton1_CheckedChanged method which will only fire when the radioButton1 checked property changes. Basically what is happening is that when you click the radiobutton you are creating the random values then checking to see if the button was clicked, which obviously it hasn't been because you are still in the radioButton1_CheckedChanged.
You need to move your Bubble Sort code into button1_Click method OR you can click your Sort button first and then click a radiobutton.
int[] array;
int smallMaxSize = 101;
private void button1_Click(object sender, EventArgs e)//When button is clicked, button click is true
{
int Out;
int In;
for (Out = smallMaxSize - 1; Out > 1; Out--)
{
for (In = 0; In < Out; In++)
{
if (array[In] > array[In + 1])
{
int temp = array[In];
array[In] = array[In + 1];
array[In + 1] = temp;
}
}
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)//Max 100 button
{
Numbers.Items.Clear();
array = new int[smallMaxSize];
Random numGenerator = new Random();
numGenerator.Next(smallMaxSize);
for (int i=0; i<101; i++)//Generates 100 random numbers from 1-100
{
array[i] = numGenerator.Next(smallMaxSize);
Numbers.Items.Add(array[i]);
}
}
Your code does currently run the sort algorithm if you check one of your checkboxes. Thats because your sorting logic is inside the handler of the checkbox check event (the radioButton1_CheckedChanged method). That means all what's inside the radioButton1_CheckedChanged method will just run if you click the checkbox. But that's not what you want. If you want the sorting method to run when you click the button then it has to be inside the button1_click method.
Your bool variable is more or less useless.
Try this:
private void button1_Click(object sender, EventArgs e)//When button is clicked, button click is true
{
int smallMaxSize = 101;
int[] array = new int[smallMaxSize];
Random numGenerator = new Random();
numGenerator.Next(smallMaxSize);
for (int i=0; i<101; i++)//Generates 100 random numbers from 1-100
{
array[i] = numGenerator.Next(smallMaxSize);
Numbers.Items.Add(array[i]);
}
if (buttonClick == true)
{
Numbers.Items.Clear();
int Out;
int In;
for (Out = smallMaxSize - 1; Out > 1; Out--)
{
for (In = 0; In < Out; In++)
{
if (array[In] > array[In + 1])
{
int temp = array[In];
array[In] = array[In + 1];
array[In + 1] = temp;
}
}
}
}
}

Placing A Thousand Lines Of 24 Integers In A Listview

I have placed a ListView on my main form.
I put 24 columns in it
I set the view to details
I have an array of integers like this
public static int[] myArrayOfIntegers = new int[24];
public static bool TheArrayIsNowReady = false;
int i;
int j;
int k;
string UserSeesThis;
Now for my request for help, please. There are 151 properties and 233 Methods associated with a ListView. Could someone please help me to understand how to place my 24 integers in this listview for the user to see them ?
if (TheArrayIsNowReady)
{
for (i = 0; i < 24; i++)
{
UserSeesThis = myArrayOfIntegers[i].ToString();
listView1._____what___do___I___put___here___[i] = UserSeesThis;
}
}
After doing one line, I'm going to have to put 500 more lines in the list box, and let the user scroll back through them. (We're hunting bogus numbers, if it matters)
In my dream app, I'll let the user scroll back five or six thousand lines.
The user is going to run a test, and an external box will send the PC 500 sets of 24 numbers. I want to put those on the screen for the user to see. I would like to be able to handle the same five and ten times over.
1) Create a listviewitem, ListViewItem item = new ListviewItem("firstvalue");
2) Create subitems for each column, item.SubItems.add("value");
3) Add item to list, listView1.Items.Add(item);
public static int[] myArrayOfIntegers = new int[24];
public static bool TheArrayIsNowReady = false;
int i, j, k;
string UserSeesThis = "?";
ListViewItem item;
private void Form1_Load(object sender, EventArgs e)
{
TheArrayIsNowReady = true;
if (TheArrayIsNowReady)
{
for (i = 0; i < 24; i++)
{
myArrayOfIntegers[i] = i;
if (i == 0) { item = new ListViewItem(myArrayOfIntegers[0].ToString()); }
item.SubItems.Add(myArrayOfIntegers[i].ToString());
UserSeesThis = "?";
}
listView1.Items.Add(item);
}
}

Global array not being accessed correctly

Below is my current code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int[] trialArray = new int[10];
public int trialCounter = -1;
private void button1_Click(object sender, EventArgs e)
{
bool button1Click = true;
if (button1Click == true)
{
ITIpanel.Visible = true;
for (int i = 0; i < trialArray.Length; i++) { trialArray[i] = -1; } // Set default value through array
int counter = 0;
Random rnd = new Random();
while (counter < 10 / 2)
{ // Red trials, fill half array
int index = rnd.Next(0, 10 - 1);
if (trialArray[index] == -1) { trialArray[index] = 1; ++counter; } //if unchanged value, change it
}
while (counter < 10)
{
int index = rnd.Next(0, 10);
if (trialArray[index] == -1) { trialArray[index] = 2; ++counter; }
}
}
}
private void ITIpanel_Paint(object sender, PaintEventArgs e)
{
if (ITIpanel.Visible == true)
{
trialCounter += 1;
timer1.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
ITIpanel.Visible = false;
timer1.Enabled = false;
if (trialArray[trialCounter] == 1) { redstimPanel.Visible = true; }
else { bluestimPanel.Visible = true;}
if (trialCounter == 9) { Application.Exit(); }
}
public int counter = 0;
public event EventHandler Clicked5TimesEvent;
private void OnClicked5TimesEvent()
{ if (Clicked5TimesEvent != null) { Clicked5TimesEvent(this, EventArgs.Empty); } }
private void bluestimPanel_MouseDown(object sender, EventArgs e)
{
//FR requirement
counter++; if (counter % 5 == 0) { redstimPanel.Visible = false; ITIpanel.Visible = true; }
}
private void redstimPanel_MouseDown(object sender, EventArgs e)
{
//FR requirement
counter++; if (counter % 5 == 0) { redstimPanel.Visible = false; ITIpanel.Visible = true; }
}
}
}
As you can see, I am attempting to make a global array with 10 items. On the button click the 10 items are supposed to be altered such that half contain the value 1 and the other half contain the value 2.
Then, on the timer tick, depending on the value in the trialCounter, which determines the part of the array to be accessed, it should display either the redstimPanel or the bluestimPanel.
Therefore, if the 'trialCounter' is equal to 8, and 8 in the TrialArray is equal 1, the 'redstimPanel' should become Visible. Alternatively, if 8 in the 'TrialArray' is equal to 2, the 'bluestimPanel' should become Visible.
This, however, is not working as I would like it to. Thus, there are clearly some issues with my code. Do you all have any suggestions?
You never reset counter, or have the second loop (the one setting the 2s) be the full array.
There is also an error with the random number, rnd.Next(a,b) a - lower bound (inclusive), b - upper bound (exclusive). So it should be rnd.Next(0,10); so you have a chance of populating the last array position.
while (counter < 10 / 2) { // Red trials, fill half array
int index = rnd.Next(0, 10);
if (trialArray[index] == -1) { trialArray[index] = 1; ++counter; } //if unchanged value, change it
}
//Counter on the first loop here is already 5 (it exited the previous loop)
//So allow it to get to 10, and populate the FULL array.
while (counter < 10) {
int index = rnd.Next(0, 10);
if (trialArray[index] == -1) { trialArray[index] = 2; ++counter; }
}
Allow me to give you some tips and some explanations regarding your code:
First of all, you probably wanted that local button1Click variable to know later on whether the button has been clicked or not. For that to work, you should place it outside that function, otherwise it's never going to be used, and will be true with every button click, something like this:
bool button1Click = false;
private void button1_Click(object sender, EventArgs e)
{
if (!button1Click)
{
When you have a condition, you want the code to decide, whether an expression is true or false you may omit the part "== true" because it doesn't add anything new.
You have two whiles. Your idea was to run the counter until 5, with the first piece of code, and then from 5 to 10 the second piece of code. Now let me try to explain what is actually going on. The counter will go on until 5 filling 1s at random indices. Then at 5, the expression in the while will become false and it breaks out from the loop. Since the second while has the very same expression, it simply avoids it and goes on. One of the many solutions would be to have an if in the loop like this:
while (counter < 10)
{
if (counter<5)
{
// fill red
}
else
{
// fill blue
}
}
The way you fill up the values in your array. Have you thought about what's going to happen when the same index will be generated several times? It means it'll overwrite the previous value while certain index will remain -1.

Categories

Resources