c#如何从表单2上的按钮单击更改表单1中的字符串

时间:2015-05-10 00:51:37

标签: c# winforms

我正在创建一个Zombie Survival RPG游戏,当我按下一个按钮(来自" MainGame"形式的移动按钮)时,我希望这样做,以及来自" Look的所有字符串-Out"形式改变我想要的任何东西。 (单击移动按钮时,播放器最后一次可以看到不同的项目。)

这是我的代码:

表格1(按钮移动点击)

 public partial class Look_out : Form
    {
        string item1 = "";
        string item2 = "";
        string item3 = "";
        string item4 = "";
        string item5 = "";
        public Look_out()
        {
            InitializeComponent();
            item1 = "Butterfly Knife";
            item2 = "";
            //String to Label
            lbl_item1.Text = item1;
            lbl_item2.Text = item2;
            lbl_item1.Visible = true;

            if (item1 == "")
            {

            }
        }
    }

表格2(需要更改的数据)

console.log

2 个答案:

答案 0 :(得分:0)

请,做一个属性:

public string YourString
{
   { get; set; }
},

因此在创建Form Look_out后传递它的值

答案 1 :(得分:0)

您可以使用以下代码执行此操作:

<强> Form1.cs的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.theVariable = textBox1.Text;
            form2.Show();
        }
    }
}

<强> Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        public string theVariable //This is your method to pass the string to the new form (Form2)
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }
    }
}