如何从Form2的class2到form1的class1获得布尔值?
我尝试调用变量,但这没有成功。还是我做错了事
Class Form3UpgradesGunSounds
// If you double click, it will select the sounds
private void Form3UpgradesGunSounds_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.X > 36 && e.X < 336 && e.Y > 35 && e.Y < 93) // FireTankCannon100
{
_tankCannon100 = true;
}
else if (e.X > 336 && e.X < 670 && e.Y > 35 && e.Y < 93) // FireTankCannon120
{
_tankCannon120 = true;
}
this.Close();
}
public bool GetTankCannon100()
{
return _tankCannon100;
}
public bool GetTankCannon120()
{
return _tankCannon120;
}
类 Form1Game :
public void MoleShooter_MouseClick(object sender, MouseEventArgs e)
{
// ...
Form3UpgradesGunSounds fr3UpgradesSounds = new Form3UpgradesGunSounds();
bool _f1tankCannon100 = fr3UpgradesSounds.GetTankCannon100();
bool _f1tankCannon120 = fr3UpgradesSounds.GetTankCannon120();
if (_f1tankCannon100 == false)
{
F1TankCannon100();
}
else if (_f1tankCannon120 == true)
{
F1TankCannon120();
}
// ...
}
public void F1TankCannon100() { /*. Do something .*/ }
public void F1TankCannon120() { /*. Do something .*/ }
我的问题是 如何从Form3UpgradesGunSounds
Form1Game
中的属性
答案 0 :(得分:2)
1-像这样在Form2
中创建一个新属性
public partial class Form2: Form
{
public static bool BolleanProperty { get; set; }
// ...
}
2- static constructor集属性BolleanProperty = true
public partial class Form2: Form
{
public static bool BolleanProperty { get; set; }
static Form2()
{
BolleanProperty = true;
}
public Form2()
{
InitializeComponent();
}
}
3-现在在Form1
中,您可以在Form2
中访问属性
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = Form2.BolleanProperty.ToString();
}
更新了我对新贡献者Luuk Scherjon 的回答
要执行此操作,请按照以下步骤操作
在Form3UpgradesGunSounds
中创建丝束public properties。
public bool TankCannon100 { get; set; }
public bool TankCannon120 { get; set; }
在Form3UpgradesGunSounds_MouseDoubleClick
事件中
用属性替换_tankCannon100
和_tankCannon120
if (...) // FireTankCannon100
TankCannon100 = true;
else if (...) // FireTankCannon120
TankCannon120 = true;
现在在Form1Game
> MoleShooter_MouseClick
中,您可以访问在Form3UpgradesGunSounds
中创建的属性
public void MoleShooter_MouseClick(object sender, MouseEventArgs e)
{
// ...
Form3UpgradesGunSounds fr3UpgradesSounds = new Form3UpgradesGunSounds();
if (!fr3UpgradesSounds.TankCannon100)
{
// do something
}
if (fr3UpgradesSounds.TankCannon120)
{
// do something
}
// ...
}