从另一种形式访问控件值 [英] Accessing a controls value from another form

查看:58
本文介绍了从另一种形式访问控件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个简单的井字游戏.我有两种形式,Form1frmStats.在我的frmStats上,我有一个标签lblDraw. 我想要这样,当玩家平局时,标签将增加1.如何从Form1的代码访问它?

I made a simple tic,tac,toe game. I have two forms, Form1 and frmStats. on my frmStats I have a Label lblDraw. I want it so when the players get in a draw, the label will increment by one. How do I access that from Form1's code?

我的Form1代码:

if (winner != 0)
  this.Text = String.Format("Player {0} Wins!", winner);
else if (winner == 0 && turnCounter == 9)
  this.Text = "Draw!";
 //this is where i want/think the code should be to change the label
else
  ...

推荐答案

首先将标签lblDraw设置为

frmStats形式

In frmStats form

 public string strNumber
 {
    get
    {
        return lblDraw.Text;
    }
    set
    {
        lblDraw.Text = value;
    }
 }


Form1

    if (winner != 0)
        this.Text = String.Format("Player {0} Wins!", winner);
    else if (winner == 0 && turnCounter == 9)
    {
        this.Text = "Draw!";
        //this is where i want/think the code should be to change the label
        frmStats frm = new frmStats();
        string number = frm.strNumber;
        frm.strNumber = (Convert.ToInt32(number) + 1).ToString(); //incrementing by 1
    }

,否则只需将Label lblDraw修饰符设置为 public ,这是不推荐的.

or else simply set the Label lblDraw modifier as public, which is not recommended.

这篇关于从另一种形式访问控件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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