I'm using visual studio to create an RPG character creator. I want to display stat bonuses and use them to calculate things like damage and to hit numbers. However, the only way I've found to display them is using a text box, which means whenever I want to do math I have to convert the string in the text box to an int, do the math, then convert it back into a string to display it.
is there not an easier way to do this that I am just missing? Is there not a "numberbox" or something similar?
Split your user interface and your data. If you want to do additional research on this topic: The user interface is usually referred to as the view , the data as the model .
Keep your stats in a separate class in their native data types. Ie, you'd have int
s for strength, dexterity, etc. Use read-only properties for derived values, such as armor class, etc.
The only work your form should do is to display those values and update the data class if the user changes the data. This has the additional advantage of keeping your code organized: Your form only contains code related to user interaction, whereas all the "rules" for calculating the values are in your data class.
There is a "numberbox". It's called the NumericUpDown
control. The value type is Decimal
, which unfortunately means you still need to cast it back and forth if you are doing calculations in double
or int
. You can put the decimal point where ever you want, which may or may not be a feature... the user can't move the decimal point.
Heinzi's answer is good advice.
OTOH, if you want to, you can create a subclass that does the opposite, that is tying the data and the view together..
This is a Label
subclass, which has an int
property and an Add
method. You can use it or use the Value
property for calculations and, of course you can add more methods, if you need it..
You can style it in any way, including Images
; I have chosen a simple style, see below..:
class IntLabel : Label
{
int val;
public int Value
{
get { return val; }
set { val = value; Text = val.ToString(Format ); }
}
public string Format { get; set; }
public IntLabel()
{
Font = new Font("Consolas", 15f);
BackColor = Color.Brown;
ForeColor = Color.AntiqueWhite;
TextAlign = ContentAlignment.MiddleCenter;
//..
}
public IntLabel(int val, string name) : base()
{
Value = Value;
Name = name;
}
protected override void OnLayout(LayoutEventArgs levent)
{
base.OnLayout(levent);
AutoSize = false;
Size = new System.Drawing.Size(200, 30);
}
public int Add(int n)
{
Value += n;
return Value;
}
}
You could then use either way to increment the value:
intLabel1.Value += 111;
intLabel1.Add(111);
After setting
intLabel1.Format = "Score: #####0";
This would result:
No you can't display numbers, only text. But what you can do is store your stats as numbers, and only convert them to text to display them, this is very easy using ToString. If you have stored the numbers, eg in a database, you can retrieve them from the database when you need to do calculations, so you don't have to worry about converting them back from text to numbers. Not that it's that difficult to do if you needed to!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.