Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...