Like/ Dislike button not working properly - c#

I am trying to make a like and a dislike button. The like button works perfectly but when the dislike button is clicked it doesnt do anything and when it is clicked again it removes a like.
What can I do to fix this problem;
I have a like button(iconButton1) a dislike button(iconButton1) and a label(label1).
int i;
int like;
int dislike;
private void iconButton1_Click(object sender, EventArgs e)
{
like = i ++ ;
label1.Text = like.ToString();
}
private void iconButton2_Click(object sender, EventArgs e)
{
dislike = like -- ;
label1.Text = dislike.ToString();
}

It sounds like you're keeping track of the number of times a button is clicked. If this is the case, then you don't need i at all - just variables that represent the "Like" and "Dislike" buttons:
int like;
int dislike;
private void iconButton1_Click(object sender, EventArgs e)
{
// Pre-increment 'like' and display it's value
label1.Text = ++like.ToString();
}
private void iconButton2_Click(object sender, EventArgs e)
{
// Pre-increment 'dislike' and display it's value
label1.Text = ++dislike.ToString();
}
Notice there's a subtle difference between a pre-increment (++variable) and post-increment (variable++). The pre-increment will use the incremented value in the expression, and the post-increment will use the non-incremented value in the expression.
Eric Lippert describes it much better here: What is the difference between i++ and ++i?

Related

Get value from another tabpage

i'm currently having a problem getting value from another tab page in windows form using c#. I have tabPage1 and tabPage 2 inside tabControl. I want to use the value I have saved in tabpage1 for another calculation in tabPage 2 but I can't find a way to do this. Here is an example of what i want for further understanding my question. Please anyone help me fix this, thank you.
private void button1_Click_1(object sender, EventArgs e)
{
double age = Convert.ToDouble(richTextBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{ double a=0;
for (int i=1,i<age,i++)
{
a=a+i;
}
}
P.s. button1 is in tabPage1 and button2 is in tabPage 2
This is an issue with variable scoping. In your button2_Click method you are probably getting an error about age not being declared.
There are a few options:
Get the value of age in your button2_Click method again.
private void button2_Click(object sender, EventArgs e)
{
double a = 0;
double age = Convert.ToDouble(richTextBox1.Text); // Get value again locally.
for (int i = 1; i < age; i++)
{
a += 1;
}
}
This will mean that age is available within the scope of button2_Click.
Store it in a property on the form, making it available to all your methods in that Form.
public partial class Form1 : Form
{
private int Age { get; set; } // Property that stores age in the class.
private void button1_Click_1(object sender, EventArgs e)
{
Age = Convert.ToDouble(richTextBox1.Text); // Store in property not local var
}
private void button2_Click(object sender, EventArgs e)
{
double a = 0;
for (int i = 1; i < Age; i++) // Loop to property value not local var.
{
a += 1;
}
}
}
Which option you choose will depend on your use case (e.g. if pressing button 1 is necessary before pressing button 2, then you will want to go with option 2; but if you could just press button 2 without first pressing button 1 then option 1 would work).
Please note that, in your for loop you have used commas, where you need to use semicolons, my example above correct this.
In addition, as #LarsTech has mentioned, you need to use int.TryParse ideally, as the value in your textbox may not be a number.

When button clicked +1

This is about the easiest thing ever. I'm doing some exercises and already did this one but deleted it. I remember this took me 2 min, and now i don't know how to do it.
All I want is when the button is clicked a Label will go + 1
private void Button1_Click(object sender, RoutedEventArgs e)
{
int Amount;
Amount = 0;
Amount++;
Label.Content = Amount;
}
I know this is wrong because every time you press Amount will become 0 again.
This is because of the scope of the variable amount. Declare amount at a class level and it should work.
private int Amount = 0;
private void Button1_Click(object sender, RoutedEventArgs e)
{
Amount++;
Label.Content = Amount;
}
You need to store the amount as a field, or some other way of having the value exist beyond the life of the button click handler:
private int Amount = 0;
private void Button1_Click(object sender, RoutedEventArgs e)
{
Amount++;
Label.Content = Amount;
}
Here, the Amount because a field associated with the instance of your Window.
That's because when you hit the button, first you set Amount to zero, then you increment it. You have to remove this variable from the button and set it on another place.
Declare Amount outside of all methods. You're just resetting it every time you click to 0.

Test to see if a listbox item is selected

I have a form that loads 3 pre-defined scores in a list box. I want to convert a selected score into a string, and then output that string in a textbox. So far i think i've converted the item to a string, and tried setting it to the textbox but it doesn't seem to be working.
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudents.Items.Clear();
lstStudents.Items.Add("Joe Smith|93|92|98");
lstStudents.Items.Add("Mike Jones|56|61|33");
lstStudents.Items.Add("Rita Jackson|100|89|96");
lstStudents.SelectedIndex = 0;
if (this.lstStudents.SelectedIndex >= 0)
{
string a = lstStudents.Items.Cast<string>().ToString();
txtDisplay.Text = a;
}
btnUpdate.Enabled = false;
Assuming your question is about Windows Forms, One way to get the selected item is to use code like this:
txtDisplay.Text =lstStudents.SelectedItem.ToString();
It is common to want to get the selected item that the user has selected, to do this, you need to place the above code in an event to look like this for example:
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtDisplay.Text = this.lstStudents.SelectedItem.ToString();
}
An event can be wired to the control either by code or via the VS IDE, you can't just copy and paste the above code. Ask me if you don't know how to do that.
If you want to grab the first item only, then Plutonix comment above applies. You don't need the IF statement.
Since this is the process at load time, why not try just :
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudents.Items.Clear();
lstStudents.Items.Add("Joe Smith|93|92|98");
lstStudents.Items.Add("Mike Jones|56|61|33");
lstStudents.Items.Add("Rita Jackson|100|89|96");
lstStudents.SelectedIndex = 0;
txtDisplay.Text = lstStudents.Items[0].ToString();
btnUpdate.Enabled = false;
EDIT
then add at the listbox's event SelectedIndexChanged :
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtDisplay.Text = lstStudents.Items[lstStudents.SelectedIndex].ToString();
}

Trying to change output for a timer in a form in c#

I need to make a form in C# have a timer and have a label that will have be the display of the timer. The label will need to be a generic label at first saying it is a counter, but when the timer starts it needs to display the count. Currently I have the display as a number up down but, it needs to be the control that can tweak the count, which it does. It just can't be the sole counter.
Here is my assignment:
Create a Windows Application. In the main form, create a label named “lTickCount”.
Create a timer named “tPeriodic”, and a numerical control of your own choosing.
Each time the timer “ticks” increment an integer, display the integer value as a string in
lTickCount. Use the numerical control to change the update rate of the timer
interactively.
I think I have done everything correctly except for the bold part. To finish I tried to make a string in both the label and the counter. I know I shouldn't have in both, I just wanted to show you the two things I've tried to help get better feedback:
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "AAAAAAAA AAAAAAAA ########";
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
TickCounter.Text = "The timer has started";
tPeriodic.Enabled = true;
}
else
{
TickCounter.Text = "The timer has ended";
tPeriodic.Enabled = false;
}
}
private void TickCounter_ValueChanged(object sender, EventArgs e)
{
TickCounter.Text = TickCounter.Value.ToString();
}
private void tPeriodic_Tick(object sender, EventArgs e)
{
TickCounter.Value += 1;
}
private void label1_Click(object sender, EventArgs e)
{
TickCounter.Text = TickCounter.Value.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Can someone help me figure out what I'm doing wrong and point me in the right way?
If you are going to try to add to a string (A label value) you need to convert it to an Integer first.
A couple ways to do this:
TextCount.Text = (Convert.ToInt32(TextCount.Text) + 1).ToString();
is one way, of course you could still use your += or any other math syntax for basic addition of +1.
You can also use tryParse, and in fact this should probably be used to verify you have an integer in the first place:
int count;
if (int.TryParse(TextCount.Text, out count))
{
count++;
TextCount.Text = count.ToString();
}
int count;
int tmrInterval = 1000; //1 sec
private void tPeriodic_Tick(object sender, EventArgs e)
{
count++;
lTickCount.Text = count.ToString();
}
private void TickCounter_ValueChanged(object sender, EventArgs e)
{
if (TickCounter.Value == 0)
{
return; // or stop the timer
}
tPeriodic.Interval = TickCounter.Value * tmrInterval;
}
tPeriodic.Interval is the time till next tick in milliseconds.
You are updating the timer interval according to tmrInterval and the value of the numeric control. You can change the interval or the formula i wrote to your own.
valter
Ok I found that:
tPeriodic.Interval = 1000 / Convert.ToInt32(TickCounter.Value * TickCounter.Value);
seemed to work in the numericupdown class.
Thanks for the help.

ASP.Net C# Variables not storing on web page

So i have the code..
int var1 = 0;
protected void cmdvar1_Click(object sender, EventArgs e)
{
var1= var1+ 10;
lblvar1.Text = var1.ToString();
}
And when clicking this, its great.. it takes the int, adds 10 to it, then displays it.. however it won't display any more than 10, did some playing around and came to the conclusion, that its not because the label isnt updating, it just simply isnt adding 10 to the previous 10 on the variable. What am i doing wrong? What am I mising? Do i need to store the variable info in a cookie or something?
This is due to the lifecycle of ASP.NET. Storing private fields behind the web page isn't the same as how it works with WinForms. If you want to persist information across post backs you need to store it in some persistent storage i.e. Session/ViewState/Database etc.
private int var1 = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// load var1 from cache
Int32.TryParse((Session["var1"] ?? 0).ToString(), out var1);
}
}
protected void cmdvar1_Click(object sender, EventArgs e)
{
var1 += 10;
Session["var1"] = var1; // cache var1
lblvar1.Text = var1.ToString();
}
So I would strongly suggest looking into a different platform. Perhaps ASP.NET MVC... However you can use something like the following to get arround your problem.
private int MyNum
{
get{ return (int)ViewState["MyNum"] ?? 0; }
set { ViewState["MyNum"] = value; }
}
Then just use MyNum as your integer your incrementing.
Assuming that lblvar1 is a Label control then you can do the following. The reason this will work is because .NET automatically take care of the state of UIControl
protected void cmdvar1_Click(object sender, EventArgs e)
{
var var1 = Convert.ToInt32(lblvar1.Text);
var1= var1+ 10;
lblvar1.Text = var1.ToString();
}
As suggested in the comments, you have to wrap your universe around statelessness before you will get to producing meaningful web applications.
The ASP.NET equivalent to accomplish state-like behavior is to use the Session collection which is available to every web page.
protected void cmdvar1_Click(object sender, EventArgs e)
{
int var1 = (int)Session["yourInteger"];
var1 += 10;
Session["yourInteger"] = var1;
lblvar1.Text = var1.ToString();
}
You are obviously setting an initial value for Session["yourInteger"] somewhere else, just one time.
The problem with Session is that it makes your application potentially buggy and somewhat unscalable. The more you use it, the worse on both accounts.
Use Session body, HTTP is a stateless Protocol, once you postback you loose the current variable value,
Solution:
int var1=0;
protected void cmdvar1_Click(object sender, EventArgs e)
{
if(Session["var1"]!=null)
var1=int.Parse(Session["var1"].ToString());
else
var1=0;
var1= var1+ 10;
lblvar1.Text = var1.ToString();
Session["var1"]=var1;
}

Categories

Resources