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.
Related
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?
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.
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();
}
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.
And dammit, I'm getting frustrated. I'm doing my first mini-game, I think the name in english is Tic-Tac-Toe, and so, I have a main menu where I choose the option 2 players, then i get a inputbox asking for the 2 players names (i used a visual basic reference) and store it in 2 variables that i send to my constructor (?). I'm potuguese so I don't really know how you guys call it, but I'll show you the code.
So, in the first Form, I have:
private void doisJogadoresToolStripMenuItem_Click(object sender, EventArgs e)
{
jogadorTempUm = Microsoft.VisualBasic.Interaction.InputBox("Jogador 1");
jogadorTempDois = Microsoft.VisualBasic.Interaction.InputBox("Jogador 2");
jog = new DoisJogadores(jogadorTempUm, jogadorTempDois);
DoisJogadores novoDois = new DoisJogadores();
novoDois.ShowDialog();
}
That gets sent to the other Form:
public DoisJogadores(string teste1, string teste2)
{
jogador1 = teste1;
jogador2 = teste2;
}
And I save it in the class:
private string jogador1
private string jogador2
And the values get saved there. But I tried to place them in a textbox to show the players names and it just goes blank.
Anyone that can help me?
You are showing not that form which you are passing values to. Here is a fix
private void doisJogadoresToolStripMenuItem_Click(object sender, EventArgs e)
{
jogadorTempUm = Microsoft.VisualBasic.Interaction.InputBox("Jogador 1");
jogadorTempDois = Microsoft.VisualBasic.Interaction.InputBox("Jogador 2");
// pass values to form you are creating
DoisJogadores novoDois =
new DoisJogadores(jogadorTempUm, jogadorTempDois);
novoDois.ShowDialog();
}
Also make sure you are assigning jogador1 and jogador2 to textboxes. E.g. you can subscribe to form's load event in DoisJogadores form:
private void DoisJogadores_Load(object sender, EventArgs e)
{
textbox1.Text = jogador1;
textbox2.Text = jogador2;
}