Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

c# - How can I get a Boolean from form 2 to form1?

how can I get a bool from class2 in Form2 to class1 in form1?

I've tried calling the variables, but that wasn't a success. Or i did something wrong

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;
    }

Class 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 .*/ }

My question is How I can create and access to properties in Form3UpgradesGunSounds from the form Form1Game

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1- Create a new property In Form2 like this

  public partial class Form2: Form
    {
        public static bool BolleanProperty { get; set; }
        // ...
    }

2- in the static constructor set property BolleanProperty = true

public partial class Form2: Form
{
    public static bool BolleanProperty { get; set; }
    static Form2()
    {
        BolleanProperty = true;

    }
    public Form2()
    {
        InitializeComponent();
    }
}

3- Now in Form1, you can access the property in Form2

 private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = Form2.BolleanProperty.ToString();
        }

Updated my answer for the new contributor Luuk Scherjon

To do this in your case, you can follow these steps

  1. Create tow public properties in Form3UpgradesGunSounds.

    public bool TankCannon100 { get; set; }
    public bool TankCannon120 { get; set; }
    
  2. In Form3UpgradesGunSounds_MouseDoubleClick event

    replace _tankCannon100 & _tankCannon120 with the properties was created

    if (...) // FireTankCannon100
      TankCannon100 = true;
    else if (...) // FireTankCannon120
      TankCannon120 = true;
    
  3. Now in Form1Game > MoleShooter_MouseClick you can access the properties created in Form3UpgradesGunSounds

    public void MoleShooter_MouseClick(object sender, MouseEventArgs e)
        {
            // ...  
            Form3UpgradesGunSounds fr3UpgradesSounds = new Form3UpgradesGunSounds();
    
            if (!fr3UpgradesSounds.TankCannon100)
            {
                // do something 
            }
            if (fr3UpgradesSounds.TankCannon120)
            {
                // do something 
            }
    
            // ... 
        }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...