如何在文本框或标签上显示变量的内容 [英] How to display contents of a variable on to a textbox or label

查看:230
本文介绍了如何在文本框或标签上显示变量的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款游戏,该游戏将分数保存在变量中,记分板始终在表单上可见,并且每次玩家获胜时都会不断更新,现在我真的无法弄清楚如何显示这些分数了.我开发的代码给出了转换错误
我在这里做错了什么......

Hi, i am developing a game and this game saves scores in variables,the scoreboard is always visible on the form and keeps on updating each time a player wins, now i cant really figure out how i can display these scores. the code i developed gives a conversion error
what am i doing wrong here....

int player = 0; //variable//
            if (cnt == 4)   //if this condition is met//
                player++;   //value of the variable should be incremented//
                textBox1.text = player;  //value of the variable should be displayed on the form//



代码示例或替代方法会有所帮助



code samples or alternatives would help

推荐答案

另一种变化是:
Another variation is:
textBox1.text = player.ToString();


此版本中的一个细微差别是您可以定义数字显示的格式.例如,您可以将格式定义为:


A slight difference in this version is that you can define the format for the number presentation. For example, you can define the format as:

textBox1.text = player.ToString("#00");


而数字4的结果将是"04".

详细信息: Int32.ToString方法 [


And the result with number 4 would be "04".

More info: Int32.ToString Method [^]


您直接将整数值分配给string.
除非您在分配之前将其强制类型转换为string ,否则它将无法正常工作.

尝试textBox1.text = Convert.ToString(player);
You are directly assigning an integer value to a string.
This won''t work until you type cast it into a string before assignment.

Try textBox1.text = Convert.ToString(player);


这篇关于如何在文本框或标签上显示变量的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆