I know I should have been more descriptive in the subject, but the question is too long to fit.
I'm trying to make a charicter creation application for a RPG. I want to have it so that when a person clicks the + button (button2), it will subtract one for the remaining points which is displayed in label20, add it to the Strength score (int Str) and then display the modified Strength in label11. Next, I have a subtract button (button10) that does the same but adds one to the remaining points and subtracts 1 from the Strength score (int Str) and displayes the remaining points and Strength in thier appropriate label boxes.
To complicate matters worse, I have to
1: have to do this for 7 other stats, which none of them can go lower than a set value (1) or higher than 20.
2: disable the ability to raise stats after the remaining points hits 0.
Mind you, I only need to figure out the Strength stat because I can mod the code to fit all the other Stats I didn't include or this post would be much too long and more confusing than guiding.
Oh, yes, I should give some more specifics for I don't know what info one needs.
This function is used only on form2, but the variables (Srt, ect.) will be needed latter on in my program.

Button Madness (one of many posts to come)
ahmedilyas
You are declaring variables in Load event of the form which means that they dont exist out of the Load event. And that not used warning is true that you are declaring and initializing variables in the Load event and using no where in Load Event. That's why you get error here:
private void label11_Click_2(object sender, EventArgs e)
{
label11.Text = Convert.ToString(Str);
///Here I get this error message: Error 10 The name 'Str' does not exist in the current context
}
because the way you choose makes sure that Str will not be visible or be avaialble to any codd block out side the Load Event.
You need to Declare Variables at class level, Initialize them in Contructor of the class and use anywere in the class without having any error.
Cheers ;)
jmodares.com
When you define a varaible inside a function, it only exists inside that function. You need to separate the definition of the variables (int Str;) from their initialization (Str = 10;) The definitions should go at the class level (outside of any method).
cmwith
Both methods cause an error. It's probably something with my code. Here it is:
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;namespace
Game{
public partial class Form2 : Form{
public Form2(){
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e){
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){
}
private void label10_Click(object sender, EventArgs e){
}
private void label7_Click(object sender, EventArgs e){
}
private void button10_Click(object sender, EventArgs e){
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e){
}
private void label12_Click(object sender, EventArgs e){
}
private void label13_Click(object sender, EventArgs e){
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e){
}
private void textBox1_TextChanged(object sender, EventArgs e){
string CharicterName = NameBox.Text;}
private void button11_Click(object sender, EventArgs e){
Form5 f = new Form5(); Form2.ActiveForm.Visible = false;f.Show();
}
private void Form2_Load(object sender, EventArgs e){
/*
///I get a warning that states: the variable "Str" is assigned but not used.
///It does this with every variable I have below
*/
int RemainingPoints = 25;
int Str = 10;
int Sta = 10;
int Dex = 10;
int Per = 10;
int Agil = 10;
int Intel = 10;
int Wis = 10;
int Appear = 10;
}
public Form2(BindingContext bc, DataSet ds)
{
}
private void AddStrButton_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void label11_Click(object sender, EventArgs e)
{
}
private void label11_Click_1(object sender, EventArgs e)
{
}
private void CharicterDescription_Click(object sender, EventArgs e)
{
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
}
private void ClassBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button18_Click(object sender, EventArgs e)
{
}
private void label20_Click(object sender, EventArgs e)
{
}
private void label11_Click_2(object sender, EventArgs e)
{
label11.Text = Convert.ToString(Str);
///Here I get this error message: Error 10 The name 'Str' does not exist in the current context
}
}
}
iterationx
What would be perfect in this case is creating a UserControl, maybe called PlayerStatEditor or something. Each one would only edit one of the stats--you would duplicate it 7 times on the form. Put the labels and buttons needed for one stat on the user control in design mode. You'll probably want to provide at least the following public interface:
public string StatName { get {} set {}} // corresponds to the label text, like "Strength"
public int Minimum { get {} set {}}
public int Maximum { get {} set {}}
public int Value { get {} set {}}
public event EventHandler ValueChanged;
Search around for UserControl tutorials or ask here if you need more info.
Code_Man888
I fixed that by moving the label11.Text = Convert.ToString(Str) up to the line below
int Appear = 10;
But my original question about getting the various buttons to do what I wanted still remains. Thanks though! That made some headway.
AlfonsAberg
You need to structure your program something like this
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Game
{
public partial class Form2 : Form
{
private int RemainingPoints;
private int Str;
private int Sta;
private int Dex;
private int Per;
private int Agil;
private int Intel;
private int Wis;
private int Appear;
private string CharicterName;
public Form2()
{
InitializeComponent();
CharicterName = "Choose a name";
RemainingPoints = 25;
Str = 10;
Sta = 10;
Dex = 10;
Per = 10;
Agil = 10;
Intel = 10;
Wis = 10;
Appear = 10;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
CharicterName = NameBox.Text;
}
private void AddStrButton_Click(object sender, EventArgs e)
{
if(remainingPoints > 0)
{
Str++;
remainingPoints--;
label11.Text = Str.ToString();
}
}
}
}
vmadan16
Oh I almost forgot! Code sample! Here is as far as I got before ripping out my hair! This is only the attempt to get the Str interger displayed in label11
label11.Text = Str;
This creates an error message!
JavaBoy
C# is a typesafe language and it doesnot allow you to put string objects into Integers and vice versa.
Whenever you need to perform arithematics, You have to make sure sure all values are in correct numeric types like int, float, decimel etc...
The above line gives error because you are putting an int value in a string obejct.
try:
label11.Text = Str.ToString();
or
label11.Text = Convert.ToString(Str);
Cheers ;)