C#如何互相访问表单对象

时间:2018-09-08 05:02:11

标签: c# winforms

在此代码下,我试图找出这些表单如何自由访问它们的两个对象。在这些代码中,

  • ITEMCount可以访问SIMSProduct的对象
  • 尽管SIMSProduct可以看到ITEMCount对象,但另一方面 (没有给出与“ SIMSProduct.SIMSProduct(ITEMCount)”的所需形式参数“ view”相对应的参数)

//Form that use SIMSPRODUCT controls
public partial class ITEMCount : Form
{
    SIMSProduct  _view;
    public  ITEMCount(SIMSProduct view)
    {
        InitializeComponent();
        _view = view;
        DisplayQuantity();
    }
}


// Form controls that been accessed by ITEMCOUNT
public partial class SIMSProduct : UserControl
{
    ITEMCount _view;
    public SIMSProduct(ITEMCount view)
    {
        InitializeComponent();
        _view = view;
    }          

    private void btn_select_Click(object sender, EventArgs e)
    {
        if (searchclicked = string.IsNullOrEmpty(txt_code.Text))
        {
            CustomNotifcation.Show("Please select your item", CustomNotifcation.AlertType.warning);
        }
        else
        {
            if (searchclicked = string.IsNullOrEmpty(txt_name.Text))
            {
                CustomNotifcation.Show("Please select your item", CustomNotifcation.AlertType.warning);
            }
            else
            {
                new ITEMCount(this).ShowDialog();
            }
        }
    }
}

//Code for calling the SIMSProduct form
public partial class SIMSMain : UserControl
{
    public SIMSMain()
    {    
        InitializeComponent();
    }

    private void btn_products_Click(object sender, EventArgs e)
    {
        var obj = new SIMSProduct(); // this where error point out
        obj.Dock = DockStyle.Fill;
        panel3.Controls.Clear();
        panel3.Controls.Add(obj); 
    }
}

1 个答案:

答案 0 :(得分:0)

访问流程如何工作:

public partial class MyFirstForm : Form
{
    private string SomeString = "Some private variable";
    public string PublicString = "Some public variable";

    public MyFirstForm()
    {
        InitializeComponent();            
    }

    private void OnButtonClick(object sender, EventArgs e)
    {
        using(Form2 f = new Form2(this))
        {
            f.ShowDialog();
        }
    }
}

public class Form2 : Form
{
    private MyFirstForm form;

    public Form2(MyFirstForm form)
    {
        InitializeComponent();

        if(form == null)
            throw new Exception("Form you passed is not assigned!");

        this.form = form;
        form.SomeString = "I just changed string in first form";
    }
}

在评论中,您问我what if i create this in form 2 : private string SomeoneString = "Some private variable"; How can i access this in form 1 ?-答案是您不能-不能以简单的方式,这是因为private变量不能直接从其他类访问。有3个解决方案。首先是将变量公开,并像这样使用它:

public partial class TestForm : Form
{
    TestForm2 tf2; //Create global Variable for Second form which will be accessible from whole First Form Class

    public string StringInFirstForm = "Blank"; //Need to be public

    public TestForm()
    {
        InitializeComponent();

        tf2 = new TestForm2(this); //Assign new object(form) to globally created variable(Second form)
        tf2.Show(); //Important to use Show not ShowDialog since if you use ShowDialog it will stop further code from executing
    }
}

public partial class TestForm2 : Form
{
    TestForm tf;

    public TestForm2(TestForm tf)
    {
        InitializeComponent();
        this.tf = tf;

        tf.StringInFirstForm = "I have changed string";
    }
}

访问私有变量的第二种方法是使用属性:

public partial class TestForm : Form
{
    TestForm2 tf2; //Create global Variable for Second form which will be accessible from whole First Form Class

    public string StringProperty
    {
        //Since there is no get{ } user can only SET property and when he does that property gets passed value and put that value into private variable in this class
        set
        { 
            StringInFirstForm = value;
            MessageBox.Show("You have changed value of StringInFristForm using property named StringProperty");
        }
    }
    private string StringInFirstForm = "Blank";

    public TestForm()
    {
        InitializeComponent();

        tf2 = new TestForm2(this); //Assign new object(form) to globally created variable(Second form)
        tf2.Show(); //Important to use Show not ShowDialog since if you use ShowDialog it will stop further code from executing
    }
}

public partial class TestForm2 : Form
{
    TestForm tf;

    public TestForm2(TestForm tf)
    {
        InitializeComponent();
        this.tf = tf;

        tf.StringProperty = "I have changed string";

        string Test = tf.StringProperty; // This will return error which will say that property could only be set but not get
    }
}

在类中设置私有值的第三种方法是通过公共方法。

public partial class TestForm : Form
{
    TestForm2 tf2;

    private string StringInFirstForm = "Blank";

    public TestForm()
    {
        InitializeComponent();

        tf2 = new TestForm2(this);
        tf2.Show();
    }

    public void ChangeVariable(string Whatever)
    {
        StringInFirstForm = Whatever;
        MessageBox.Show("You have changed StringInFirstForm value using method ChangeVariable");
    }
}

public partial class TestForm2 : Form
{
    TestForm tf;

    public TestForm2(TestForm tf)
    {
        InitializeComponent();
        this.tf = tf;

        tf.ChangeVariable("Some String");
    }
}

几乎没有访问变量的可能性。希望对您有所帮助:)