I would like to make a clicks counter with a button, that's my code.
I want to make a click counter, with the help of a button, which at each click, would display in a text box a number (1 then 2 ...) Can you help me?
private vous btn_click(object sender, EventArgs e)
{
do
{
int i = 0;
label1.Text = i.ToString();
break;
}
while (i < 10);
{
i++;
}
Just to put you on the right track. The following displays from 1 to 10 and 'round again. ;-)
int x = 0;
void btn_click(object sender, EventArgs e)
{
label1.Text = ++x.ToString();
if(x==10) x=0;
}
Please try to learn some basics first.
Related
I am making a calculation, but then I came up with the idea to automatically make that calculation as I fill in textBox1. How I can call that calculation that is inside button1_Click? I know how to copy to textBox2 what you wrote in textBox1, but my knowledge is to little for to call a whole if statement calculation to auto update Total inside textBox2 when I was writing numbers inside textBox1 without a button.
private void textBox1_TextChanged(object sender, EventArgs e) { }
private void textBox2_TextChanged(object sender, EventArgs e) { }
private void button1_Click(object sender, EventArgs e)
{
aantalgroep = int.Parse(textBox1.Text);
/* Wat er gebeurd bij RadioButton1 Checked */
if (radioButton1.Checked)
{
number = aantalgroep * 8;
textBox2.Text = number.ToString();
if (aantalgroep < 10)
textBox2.Text = number.ToString();
}
}
Go in design editor, click on your textbox, click on little lighting, find TextChanged and click on arrow pointing down (next to TextChanged field). There you will have enlisted your already created method named button1_Click, select it and voila. Every time you change text in textbox you will call you method to auto calculate.
For sanity's sake, you should probably move the logic out of the click handler, since you plan to call it from various places. Once you have the logic extracted, you can call it from anywhere you want.
private void textBox1_TextChanged(object sender, EventArgs e)
{
Calculate();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
//You probably don't want to call Calculate here, due to infinite recursion
//Calculate();
}
private void button1_Click(object sender, EventArgs e)
{
Calculate();
}
private void Calculate()
{
aantalgroep = int.Parse(textBox1.Text);
/* Wat er gebeurd bij RadioButton1 Checked */
if (radioButton1.Checked) {
number = aantalgroep * 8;
textBox2.Text = number.ToString();
if (aantalgroep < 10) {
textBox2.Text = number.ToString();
}
}
}
So, I know that with a code snippet such as:
int x = 0; //class field variable
private void button1_Click(object sender, EventArgs e)
{
Label1.Text += (x++)%4 + 1;
}
a sequence of 12341234 is displayed on the form if the button is clicked 8 times.
My goal is to get 43214321 to display.
I'm able to get 32103210 with:
int x = 0; //class field variable
private void button1_Click(object sender, EventArgs e)
{
Label1.Text += 3-(x++)%4;
}
I'm also able to get 32143214 with:
int x = 1; //class field variable
private void button1_Click(object sender, EventArgs e)
{
Label1.Text += 4-(x++)%4 + ;
}
What am I doing wrong? And is there a general formula for this?
Note: My x DOES have to be initialized to 1.
Just change the formula to:
Label1.Text += 4-((x-1)++)%4;
Try using this formula:
5-(1+(x+++3)%4)
That is:
Label1.Text += (5-(1+(x+++3)%4)).ToString();
The first line of code that you've written is basically cycling between 3 to 1.
x=0;
Label1.Text += 3-(x++)%4;
x=0 || Output=3.
Label1.Text= 0+3-(0%4)=3
x=1 || Output=2.
Label1.Text= 0+3-(1%4)=2
x=2 || Output=1.
(Dry run as done above)
x=3 || Output=0.
(dry run as done above)
x=4 and the cycle repeats.
You could dry run your second line of code to understand why your answer comes the way it does, but to answer your question in concise:
int x = 0; //class field variable
private void button1_Click(object sender, EventArgs e)
{
Label1.Text += 4-(x++)%4;
}
I'm new to learning winforms and i'm stuck on the following problem and I do not think what I have done is the correct way, so any help would be appreciated.
I have 4 textboxes such as the following
private void txtBxPlayer1Bid_TextChanged(object sender, EventArgs e)
{
txtBxFundsAvialable.Text = (Convert.ToInt32(txtBxFundsAvialable.Text) - Convert.ToInt32(txtBxPlayer1Bid.Text)).ToString();
}
The 5th textbox txtBxFundsAvialable simple subtract the value of txtBxPlayer1Bid from txtBxFundsAvialable.
In designer.cs I have
this.txtBxPlayer1Bid.Leave += new System.EventHandler(this.txtBxPlayer1Bid_TextChanged);
The problem I have is, if I have 100 in txtBxFundsAvialable and enter 10 in txtBxPlayer1Bid the value in txtBxFundsAvialable should be 90, but txtBxPlayer1Bid etc seem to go into a loop and the value in txtBxFundsAvialable becomes 60. 4 textboxes X 10.
This happens for any of the 4 textboxes
The only way I can solve the problem is to set the values of the 4 textboxes to 0 in the txtBxFundsAvialable_TextChanged as shown below.
private void txtBxFundsAvialable_TextChanged(object sender, EventArgs e)
{
if (Convert.ToInt32(txtBxPlayer1Bid.Text) > 4 || (Convert.ToInt32(txtBxPlayer2Bid.Text)> 4 || (Convert.ToInt32(txtBxPlayer3Bid.Text)> 4) || (Convert.ToInt32(txtBxPlayer2Bid.Text)> 4)))
{
txtBxPlayer1Bid.Text = "0";
txtBxPlayer2Bid.Text = "0";
txtBxPlayer3Bid.Text = "0";
txtBxPlayer4Bid.Text = "0";
}
}
Is what I'm doing the correct way, as stated at the beginning, I'm new to winforms and it a canny leanning curve
I wrote a simple code with 2 textboxes that get values and a textbox with the result. Updates with TextChangedevent. Try to use it to fix your code..
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
int num1 = Int32.Parse(textBox1.Text), num2 = Int32.Parse(textBox2.Text);
textBox3.Text = (num1 - num2).ToString();
}
catch { }
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox1_TextChanged(sender, e);
}
EDIT
Try this code and link any of your "bid" textboxes to this function. textbox1 in this code is equivalent to your "available" textbox.
private void textBox_Leave(object sender, EventArgs e)
{
try
{
int num = Int32.Parse(((TextBox)sender).Text), available = Int32.Parse(textBox1.Text);
textBox1.Text = (available - num).ToString();
}
catch { }
}
Not sure how .Leave operates. Try to use .TextChanged or whatever the equivalent in WinForms.
All four (or even five) text boxes should use same event callback method.
Here is what you can do in that method:
private void txtBx_TextChanged(object sender, EventArgs e)
{
double player1 = 0, player2 = 0, player3 = 0, player4 = 0, total = 0;
if (int.TryParse(txtBxPlayer1Bid.Text, out player1)
&& int.TryParse(txtBxPlayer2Bid.Text, out player2)
&& int.TryParse(txtBxPlayer3Bid.Text, out player3)
&& int.TryParse(txtBxPlayer4Bid.Text, out player4)
&& int.TryParse(txtBxFundsAvialable.Text, out total)
{
total = player1 + player2 + player3 + player4;
}
}
I am very new to programming and I am trying to do that every time you click a button, it adds one to the value of an int and shows it in a textbox. My code is:
private void button1_Click(object sender, EventArgs e)
{
int a = 100;
a++;
txtBox1.Text = a.ToString();
}
So when I click the button it shows in the text box 101, but when I click it again, I want the textbox to show 102 and 103 etc etc. Any ideas? I assume it's very easy and using some variation of a loop but I have tried a few things and nothing seems to work. Any tips would be greatly appreciated! Thanks.
You have to store your value outside of Method Body.
private int a = 100;
private void button1_Click(object sender, EventArgs e)
{
a++;
txtBox1.Text = a.ToString();
}
What you did in your program is anytime you clicked the button, new Integer a was declared with value of 100, then you are increasing it by 1 and that's why you always seen '101'.
In your code you delcare a and assign a value to it over and over again every time you click on the button.
You should declare the variable outside of button1_Click method:
class Window1
{
int a = 100;
....
private void button1_Click(object sender, EventArgs e)
{
a++;
txtBox1.Text = a.ToString();
}
}
You need to declare a as a member of the class containing your method:
private int _a = 100;
private void button1_Click(object sender, EventArgs e)
{
_a++;
txtBox1.Text = _a.ToString();
}
If you don't do that, you will have a new instance every time the button is clicked, so you will always see 101 in your text box.
It is possible not to create global fields and store count of clicks inside the textbox.
This is especially convenient if you have several buttons.
private void button1_Click(object sender, EventArgs e)
{
if (txtBox1.Tag is int)
{
int a = (int)txtBox1.Tag;
a++;
txtBox1.Tag = a;
txtBox1.Text = a.ToString();
}
else
{
txtBox1.Tag = 100;
txtBox1.Text = 100;
}
}
int a = 100;
txtBox1.Text = a.ToString();
......
private void button1_Click(object sender, EventArgs e)
{
a++;
txtBox1.Text = a.ToString();
}
Placing int a = 100; inside the button1_Click(object sender, EventArgs e) will set a to 100 when each time function executing. If you need to have a counter place it outside from the function(Then it will initialize only once.) and increment it when executing the function.
Solution
int a = 100;
private void button1_Click(object sender, EventArgs e)
{
a++;
txtBox1.Text = a.ToString();
}
static int a = 100;
private void button1_Click(object sender, EventArgs e)
{
a++;
txtBox1.Text = a.ToString();
}
if you want to optimization your code than firstly set the textbox property text = 100 and write only one line code in button click event
private void button1_Click(object sender, EventArgs e)
{
txtBox1.Text = (Convert.ToInt32(txtBox1.Text) + 1).ToString();
}
as you know C# complie the code line by line and you have only one line code than it's give faster perfomance.
I am having problems getting my page to maintain state. View state is enabled by default but everytime I click a button it resets the form. This is the code I have
protected void Page_Load(object sender, EventArgs e)
{
Levels loadGame = new Levels(currentGame);
int [] gameNums = loadGame.getLevelNums();
int inc = 1;
foreach(int i in gameNums){
if (i != 0)
{
TextBox tb = (TextBox)FindControl("TextBox" + inc);
tb.Text = i.ToString();
tb.Enabled = false;
}
else {
//leave blank and move to next box
}
inc++;
}
This is the initial load
protected void NormalButton_Click(object sender, EventArgs e)
{
clearBoxes();//clear boxes first
setCurrentGame("normal");//setting to normal returns normal answers
Levels loadGame = new Levels(returnCurrentGame());
int[] gameNums = loadGame.getLevelNums();
int inc = 1;
foreach (int i in gameNums)
{
if (i != 0)
{
TextBox tb = (TextBox)FindControl("TextBox" + inc);
tb.Text = i.ToString();
tb.Enabled = false;
}
else
{
//leave blank and move to next box
}
inc++;
}
}
Clicking this button changes the numbers in different boxes.
protected void Button1_Click(object sender, EventArgs e)
{
}
Then I have this empty button but everytime I click it, it resets the form even though I havent set it to do anything yet. I would like the boxes to stay the same and I would also like to keep the objects alive. I'm not sure what I'm missing but please point me in the right direction. Thanks in advance
The Page_Load event occurs every time the page is loaded, including event-driven postbacks (button clicks, etc).
It looks like initialization code is in your Page_Load, so when you click the button it runs again.
There are two options:
Put everything that you want to happen only on the FIRST load in a n if statement:
Move your initialization to Page_Init.
Code sample for the first option:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack) // Teis is the key line for avoiding the problem
{
Levels loadGame = new Levels(currentGame);
int [] gameNums = loadGame.getLevelNums();
int inc = 1;
foreach(int i in gameNums){
if (i != 0)
{
TextBox tb = (TextBox)FindControl("TextBox" + inc);
tb.Text = i.ToString();
tb.Enabled = false;
}
else {
//leave blank and move to next box
}
inc++;
}
}
}
Also, recommended reading: The ASP.NET Page Lifecycle