tags:

views:

1227

answers:

8

I have form with button and checkbox. if i hit button static void is called which call non static void which shows messagebox with the checkbox.checked.toString() The problem is if i change the checkbox value it always shows false

Code is

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

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void y()
        {
            MessageBox.Show(checkBox1.Checked.ToString());
        }

        static void x()
        {
            Form1 f = new Form1();
            f.y();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            x();
        }



    }
}
+1  A: 

I guess you have probably came from a Visual Basic background like I do.

In C#, form are just instances of a Form class, they does not have special status like in the days of VB6.

When you call new Form1() you are basically creating a new form not accessing the same form. As anyone form can have multiple instances because it really is just a C# class underneath.

You can fix this by having the x() method takes the current form as a parameter

static void x(Form1 theForm)
{
    theForm.y();
}

private void button1_Click(object sender, EventArgs e)
{
    x(this);
}

The this parameter inside a form class points to the form instance itself.

You should now gets the correct value instead of the default value when the form is being created.

I suppose you have a need for x() to be static, no? But if that isn't the case, removing static from x() might be a better solution.

void x()
{
    this.y();

    // or you can just omit the this qualifier and call just y();
}
chakrit
Of course, if he does it this way, x might as well be a static method so he doesn't have to pass in the form object.
R. Bemrose
Added that, thanks!
chakrit
A: 

It's because your instantiating a new Form1 inside x(), try passing 'this' to the x method as a parameter.

Adam Naylor
Of course, that defeats the point of making it static in the first place...
R. Bemrose
Only from the compilers pov. not from an architectural pov.
Adam Naylor
A: 

You're creating a new Form1 instance. Why are you doing that? When you create a new form, a new checkbox is created as well. The checkbox on the form looks like is set to false (not checked) by default, therefore, every time you create a new instance of the form, it comes up as false.

BFree
+1  A: 

Method x instantiates a new form. The check box on the new form will also be new (created with the form) and will have a default value of false.

What exactly are you trying to do? Why create a new form when the button is pressed? If you really want to do this then you need to set the new form's check box state after you call Form f = new Form1();

ng5000
A: 

Try

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

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public void y()
    {
        MessageBox.Show(checkBox1.Checked.ToString());
    }

    static void x(Form f)
    {
        f.y();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        x(this);
    }

}
codemeit
A: 

The checkbox1 in y() is on a completely different Form1 - the one you created in x. Simply get rid of x() and it should work:

    private void button1_Click(object sender, EventArgs e)
    {
        y(); // not x();
    }
Marc Gravell
A: 

your issues are in the x() method, what you're doing there is actually making an entirely new form and checking that forms check box, which would obviously be instantiating as false. Rather then calling x() you should call y().

Or, alternatively, put the messagebox.show in the buttonclick method itself.

A: 

If you need to access some form instance from static method, you need to save somewhere a reference to that form.

class Program
{
  public static Form thatForm;

  public static void Main(string[] args)
  {
    MyForm form = new MyForm();
    thatForm = form;
    Application.Run(form);
  }
}

class MyForm : Form
{
  void Foo()
  {
    Program.thatForm.somethingPublic();
  }
}
abatishchev