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.
Related
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.
I'm developing a maze game at beginner level on C# and what i want to do is create a timer that shows up on the top right corner of the game. The timer is supposed to count down from 1000 (999, 998, 997 every second etc). I wouldn't know how to connect a label that displays the timer that i want to create. How would i go about it? I wouldn't know how to begin on the Tick-method.
private void timer2_Tick(object sender, EventArgs e)
{
}
Hope i made myself somewhat clear.
private int _count = 1000
private void timer2_Tick(object sender, EventArgs e)
{
_count--;
myLabel.Text = _count.ToString();
}
The assumption is you have a label on your form, and you have started your timmer
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.
I have a ListView with about 100 items. I have some item selected from past and now I need to scroll down to find another item, but in that time - refresh occurs and it just automatically scroll back to last selected item.
Is there any possibility how can I turn it off or better dont scroll if I'm currently scrolling?
Thanks for responds :)
I don’t know if this will help but here the settings that do NOT have the issue with scrolling back to the selected item when a new item is added and sorted. However, I use the sort setting and do not call sort or refresh.
Create a new form and place ListView and a Timer on there. On the ListView change ONLY these properties.
Set View to Details
Click Columns add a single Column (Adjust the width to make it wider)
Set Sorting to Ascending
Set the timer Enabled to true
Set interval to 2000 (2 secs)
Double click the Tick event to create a handler
Now add the following code
public Form1()
{
InitializeComponent();
for (int x = 0; x < 100; x++)
{
listView1.Items.Add("Item #" + x);
}
}
private int y = 10;
private void timer1_Tick(object sender, EventArgs e)
{
listView1.Items.Add("Item #" + y + "b");
y += 10;
}
Now when you run this you can select any item and scroll anywhere and it will not jump back to the selected item when a new item is added. However, it will scroll down one line if the item it adds is above …
Perhaps this gets you close enough that you can play with it further to get what you need.
But that is the best I could do w/o getting into sub-classing the ListView Control using the Win32 API somehow.
This is kind of a roundabout solution (and probably a little late), but it seems to work.
I'm using EnsureVisible() on the most recent added item to have auto-scrolling happen, which is called within the timer1_Tick method. But I'm using an "inspectingList" flag that will keep auto-scrolling from happening when you click on a list item. When focus moves away from the listview, scrolling continues as normal.
Part of the solution is what jross said to do, which is create a dummy form to test with:
private void Form1_Load(object sender, EventArgs e)
{
listView1.Columns.Add("Scroll Control", 100);
for(int i = 0; i < 100; i++)
{
listView1.Items.Add(string.Format("Item #{0:000}", i));
}
timer1.Interval = 1000;
timer1.Start();
}
private int newItem = 10;
private bool inspectingList = false;
private void timer1_Tick(object sender, EventArgs e)
{
listView1.Items.Add(string.Format("Item #{0:000}", newItem));
newItem += 10;
if (!inspectingList)
{
listView1.Items[listView1.Items.Count - 1].EnsureVisible();
}
listView1.Refresh();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
inspectingList = true;
if (listView1.SelectedIndices.Count > 0)
{
listView1.Items[listView1.SelectedIndices[0]].EnsureVisible();
}
}
private void listView1_Leave(object sender, EventArgs e)
{
inspectingList = false;
}
C# question
Here is what I am trying to do. When I click a button, I want the checkboxlist to smoothly change from say (200,10) to (200,100) in size. I am successful at getting to size to change instantaneously, but I want it to look smooth.
Here is the code I wrote:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (checkedListBox1.Height < 100)
{
checkedListBox1.Size = new Size(checkedListBox1.Size.Width, checkedListBox1.Size.Height + 1);
}
else
{
timer1.Enabled = false;
}
}
I have used this coding to move objects smoothly, but never to change sizes.
So when you run this code, the box just flickers and it seems like its trying to change size, but it doesn't, and the loop never ends.
Thanks!
You need to set IntegralHeight to false to that the box can be a height that is not a multiple of the item height.
For the flickering, you should probably double-buffer the form which contains this control.