basic incremental on button click - c#

I'm trying to write a simple button that when you click it adds 1 and updates the label but it only works on the first click after that it doesn't update the label anymore
protected void Page_Load(object sender, EventArgs e)
{ }
protected void ClickerButton_Click(object sender, ImageClickEventArgs e)
{
Lbl_PairsCooked.Text = CookSneaker(NumOfPairs).ToString();
}
public int CookSneaker(int num)
{
num += 1;
return num;
}
The image button i made only works on the first click...

See the snippet below on how to get it working. Most importantly you need to read on IsPostBack the link
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8 to understand the difference.
public partial class _Default : Page
{
int NumOfPairs;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//this line reads the text from label, converts and assigns to NumPairs. The UI is updated on the button click which takes this new NumOfPairs.
int.TryParse(Label1.Text, out NumOfPairs);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = CookSneaker(NumOfPairs).ToString();
}
public int CookSneaker(int num)
{
num += 1;
return num;
}
}

Related

Call method inside a Form

Hi I am learning C# and struggling with a Form in which a btnCalc_click event should call a method calcArea and produce the output in a textBox1.text
the error is in row: textBox1.Text = calcArea.ToString();
who can help with with the correct syntax ?
namespace Meetkunde
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txbLengte_TextChanged(object sender, EventArgs e)
{
double length = 0;
length = double.Parse(txbLength.Text);
}
private void txbBreedte_TextChanged(object sender, EventArgs e)
{
double width = 0;
width = double.Parse(txbWidth.Text);
}
public double calcArea(double length, double width)
{
double area = 0;
area = (length * width);
return area;
}
private void label3_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btnCalc_Click(object sender, EventArgs e)
{
textBox1.Text = calcArea.ToString();
}
}
}
As I mentioned above - you need to pass the method the parameters as you defined them in the method. You don't need Text Changed events for this either - plus they aren't doing anything.
Try changing your code to something like (not real sure of what exactly you named textboxes):
private void btnCalc_Click(object sender, EventArgs e)
{
double width = Convert.ToDouble(txbBreedte.Text);//txbWidth.Text?
double length= Convert.ToDouble(txbLengte.Text);//txbLength.Text?
textBox1.Text = calcArea(length, width).ToString();
}
calcArea.ToString();
when you are calling this, you aren't calling the function, you are referencing the function, not the return
do
calcArea(parameters).ToString();
replace parameters with what you want to calculate.

How to do On="1" and Off="2" within one button?

I'm using two buttons for "on" and "off". When I press "On" button, it passes a string value "1", and pressing the "Off" button passes the string value "2".
How can I do this within one button? By default button should be off, when I press it should on and when i press again it should off.
public partial class _Default : System.Web.UI.Page
{
SerialPort ardo;
protected void Page_Load(object sender, EventArgs e)
{
ardo = new SerialPort();
ardo.PortName = "COM5";
ardo.BaudRate = 9600;
}
protected void Button1_Click(object sender, EventArgs e)
{
string stop = "1";
ardo.Open();
ardo.Write(stop);
ardo.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string On = "2";
ardo.Open();
ardo.Write(On);
ardo.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//consider initially its stopped
if(Session[“currentState”] == null)
Session[“currentState”] = “1”;
if(Session[“currentState”].ToString() == “1”)
{
Session[“currentState”]= “2”;
}
else
{
Session[“currentState”]= “1”;
}
ardo.Open();
ardo.Write(Session[“currentState”].ToString());
ardo.Close();
}
it set to hidden text box.
then you can check if statement it is pressed or not
protected void Button1_Click(object sender, EventArgs e)
{
// hf means hidden textbox
if(hf.text==1){
// your on code
hf.text=0;
}
if(hf.text==0){
//your off code
hf.text=1;
}
}
Declare a string variable
private string isOn = "2";
and then on the button click event you can handle it like
protected void Button1_Click(object sender, EventArgs e)
{
if(isOn.Equals("2"))
isOn = "1";
else
isOn = "2";
}

Call a LIST<DOUBLE> inside button1 click event into button2 click event in c#

I have two buttons in my C# windows form application. Button1 and Button2.
i want to use a variable and a list calculated in Button1's event as an input variable in Button2's event. how can I do that? Example:
private void button1_Click(object sender, EventArgs e)
{
int a;
// some steps
// after these steps, assume a gets the value of 5 so a = 5 at this point.
// also there is a list which gets its values after these steps
List<double> parameterValues = new List<double> {
i.GetDouble(), S.GetDouble(), L.GetDouble(),B.GetDouble()
};
}
Here is the code for button2 event, in this I want to be able to use the value of a calculated in button1's code.
private void button2_Click(object sender, EventArgs e)
{
int b = a + 5;
// some code to call the list as well
}
You have to make int a global in order to use it in both buttons.
public int a;
private void button1_Click(object sender, EventArgs e)
{
a = 5;
// some steps
// after these steps, assume a gets the value of 5 so a = 5 at this point.
}
private void button2_Click(object sender, EventArgs e)
{
int b = a + 5;
}
You have a scope issue currently. The value you want to use inside button click 2 must be at least modular to the forms class in order to use in both methods. In this example, "outerValue" is modular and can be accessed by both. Have a read through this article to get a better overview of variable scope.
https://msdn.microsoft.com/en-us/library/ms973875.aspx
public partial class Form1 : Form
{
private int outerValue = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int a = 5;
outerValue = a + 5;
}
private void button2_Click(object sender, EventArgs e)
{
int b = outerValue + 5;
}
}

not need to use Viewstate to retain state in asp.net?

I saw some code using Viewstate to retain state between user requests, below is the code to use a button to increment the number in a textbox input:
public partial class WebForm1 : System.Web.UI.Page
{
int ClicksCount = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = "0";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if(ViewState["Clicks"] != null)
{
ClicksCount = (int)ViewState["Clicks"] + 1;
}
TextBox1.Text = ClicksCount.ToString(); ;
ViewState["Clicks"] = ClicksCount;
}
}
But I don't need to use Viewstate to achieve the same goal, and it is much more simple, here is my code:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = "0";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int i = Int32.Parse(TextBox1.Text);
i++;
TextBox1.Text = i.ToString();
}
}
so what's wrong with my code
There is nothing wrong with your code: the contents of the ASP.NET controls (as is the TextBox1 in your example) are already integrated in the ViewState automatically, and hence there is no need to manually enter them. You would use directly the ViewState to store anything that is not part of your design components (for instance an int variable that you want to increase each time).

i++ keep on incrementing during page load

i++ keep on increasing whenever I reload the page.It should only increment when I trigger the button but I found out that during page reload it also increments.
I did the !IsPostBack but I still encounter the problem.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cart_number();
}
}
private static int i;
private void cart_number()
{
lbl_cart_number.Text = i++.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
cart_number();
}
When you reload a page, it means that it's not a IsPostBack. You should remove cart_number(); from your Page_Load. The Page_Load will be triggered each time there is an interaction between the browser and the webserver.
Remove cart_number() method call from your 'Page_Load'. No need to call that method on Page_Load. Any specific reason why you call from Page_Load()?
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cart_number("1");
}
}
private static int i;
private void cart_number(string flag)
{
int lbl=0;
lbl =int.Parse(lbl_cart_number.Text);
if(flag!="1"){
i=lbl;
if(i>=0){
lbl_cart_number.Text =( i+1).ToString();
}
}
else
{
lbl_cart_number.Text ="0";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
cart_number("2");
}

Categories