简体   繁体   English

将textBox的可见性从form1设置为form2

[英]set visibility on textBox from form1 to form2

From other Form i want to set visibility for textBoxes on this form but i down't know how to call TextBoxes and set property Visible = false. 从其他窗体,我想在此窗体上设置textBoxes的可见性,但我不知道如何调用TextBoxes并设置属性Visible = false。

I try with Enums but i still can't solve problem. 我尝试使用Enums,但仍然无法解决问题。 I can not cast or do anything. 我什么也做不了。 So how can i call textBox From form1 to form2... 所以我怎样才能将textBox从form1调用到form2 ...

i am using C# and CF 3.5 我正在使用C#和CF 3.5

public enum VnosTextBoxType
    {
        Ean, PredmetObravnave, Tse, Kolicina, EnotaMere, 
        Lokacija, Zapora, Sarza, SarzaDobavitelja, Datumod, 
        DatumDo 
    } 

this are names for all my TextBoxes. 这是我所有TextBoxes的名称。 I have TextBoxes with names like txtEan, txtPredmetObravnave,.. 我有名称像txtEan,txtPredmetObravnave的TextBoxes。

What about writing on Form2 a method like this: 在Form2上写这样的方法呢?

public void SetTBVisible(string name, bool visible)
{
    this.Controls[name].Visible = visible;
}

and call this form your Form1? 将此表格称为您的Form1?

EDITED: 编辑:

public void SetTBVisible(string name, bool visible)
{
    string cName = name.ToLower();
    foreach(Control c in this.Controls)
        if (c.Name.ToLower() == cName)
        {
            c.Visible = visible;
            break;
        }
}

Make a new class called Globals.cs write: 创建一个名为Globals.cs的新类,编写:

    public static Form1 MainForm;
    public static Form2 ChildForm;

go to Form1 and make the event: form load put: 转到Form1并进行以下事件:加载表单:

Globals.MainWindow = this;

and: 和:

CheckForIllegalCrossThreadCalls = false;

and do the same in Form2 with ChildForm now you can call form2 with: Globals.ChildForm.TextBox1.Visible = false; 并使用ChildForm在Form2中执行相同的操作,现在您可以使用以下命令调用form2:Globals.ChildForm.TextBox1.Visible = false;

Edit: don't forget to make your textBox public. 编辑:不要忘记将您的textBox公开。

let say you want to set Visible = false for textbox1 of form1 假设您要为form1的textbox1设置Visible = false

when you create instance of form2 then you have pass the instance of form1 into its constructor like this 当您创建form2的实例时,您已经将form1的实例传递给了它的构造函数,如下所示

Class Form1 : Form 
{
    public void setTextbox(bool val)
    {
       this.Textbox1.visible=val;
    }
    Public void showForm2()
    {
       Form2 f2= new Form2(this);
       f2.show();
    }        
}

Class Form2 : Form 
{
    Form1 f1;

    public Form2(Form form1)
    {
        f1=form1;
    }

    public void setTb()
    {
    f1.setTextbox(false);
    }

}

I Hope this will help you 我希望这能帮到您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM