如何访问另一个表单的表单控件?

发布于 2024-10-14 19:47:05 字数 614 浏览 2 评论 0原文

我有两个 Form 类,其中一个有一个 ListBox。我需要为 ListBoxSelectedIndex 属性设置一个设置器,我想从第二个 Form 调用该属性。

目前我正在执行以下操作:

表格 1

public int MyListBoxSelectedIndex
{
     set { lsbMyList.SelectedIndex = value; }
}

表格 2

private ControlForm mainForm; // form 1

public AddNewObjForm()
{
     InitializeComponent();
     mainForm = new ControlForm();           
}

public void SomeMethod()
{
     mainForm.MyListBoxSelectedIndex = -1;
}

这是执行此操作的最佳方法吗?

I have two Form classes, one of which has a ListBox. I need a setter for the SelectedIndex property of the ListBox, which I want to call from the second Form.

At the moment I am doing the following:

Form 1

public int MyListBoxSelectedIndex
{
     set { lsbMyList.SelectedIndex = value; }
}

Form 2

private ControlForm mainForm; // form 1

public AddNewObjForm()
{
     InitializeComponent();
     mainForm = new ControlForm();           
}

public void SomeMethod()
{
     mainForm.MyListBoxSelectedIndex = -1;
}

Is this the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

寒冷纷飞旳雪 2024-10-21 19:47:05

让它们成为 Singleton 并不是一个完全坏的主意,但我个人不喜欢那样做。我宁愿将一种形式的引用传递给另一种形式。这是一个例子。

Form1 触发 Form2 打开。 Form2 具有重载构造函数,它将调用 form 作为参数并提供对 Form2 成员的引用。这样就解决了通讯问题。例如,我在 Form1 中将 Label 属性公开为公共,并在 Form2 中进行了修改。

通过这种方法,您可以通过不同的方式进行沟通。

示例项目的下载链接

//您的 Form1

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

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(this);
        frm.Show();
    }

    public string LabelText
    {
        get { return Lbl.Text; }
        set { Lbl.Text = value; }
    }
}

//您的 Form2

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

    private Form1 mainForm = null;
    public Form2(Form callingForm)
    {
        mainForm = callingForm as Form1; 
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.mainForm.LabelText = txtMessage.Text;
    }
}

替代文字
(来源:ruchitsurati.net

替代文字
(来源:ruchitsurati.net

Making them Singleton is not a completely bad idea, but personally I would not prefer to do it that way. I'd rather pass the reference of one to another form. Here's an example.

Form1 triggers Form2 to open. Form2 has overloaded constructor which takes calling form as argument and provides its reference to Form2 members. This solves the communication problem. For example I've exposed Label Property as public in Form1 which is modified in Form2.

With this approach you can do communication in different ways.

Download Link for Sample Project

//Your Form1

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

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(this);
        frm.Show();
    }

    public string LabelText
    {
        get { return Lbl.Text; }
        set { Lbl.Text = value; }
    }
}

//Your Form2

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

    private Form1 mainForm = null;
    public Form2(Form callingForm)
    {
        mainForm = callingForm as Form1; 
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.mainForm.LabelText = txtMessage.Text;
    }
}

alt text
(source: ruchitsurati.net)

alt text
(source: ruchitsurati.net)

不醒的梦 2024-10-21 19:47:05

像这样访问表单的控件:

formname.controls[Index]

您可以转换为适当的控件类型,示例:

DataGridView dgv = (DataGridView) formname.Controls[Index];

Access the form's controls like this:

formname.controls[Index]

You can cast as appropriate control type, Example:

DataGridView dgv = (DataGridView) formname.Controls[Index];
无声无音无过去 2024-10-21 19:47:05

如果您不想像 Joe Dabones 建议的那样循环遍历“所有”控件,还有另一种方法。
在 Form2 中创建一个函数并从 Form1 中调用它。

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

    public void SetIndex(int value)
    {
        lsbMyList.SelectedIndex = value;
    }
}

public partial class Form1 : Form
{
    public Form2 frm;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        frm=new Form2();
        frm.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm.SetIndex(Int.Parse(textBox1.Text));
    }
}

There is one more way, in case you don't want to loop through "ALL" controls like Joe Dabones suggested.
Make a function in Form2 and call it from Form1.

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

    public void SetIndex(int value)
    {
        lsbMyList.SelectedIndex = value;
    }
}

public partial class Form1 : Form
{
    public Form2 frm;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        frm=new Form2();
        frm.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm.SetIndex(Int.Parse(textBox1.Text));
    }
}
羁客 2024-10-21 19:47:05

如果ChildForm想要访问ParentForm

ParentForm实例传递给ChildForm构造函数。

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

    public string ParentProperty{get;set;}

    private void CreateChild()
    {
         var childForm = new ChildForm(this);
         childForm.Show();
    }
}

public partial class ChildForm : Form
{
    private ParentForm parentForm;

    public ChildForm(ParentForm parent)
    {
        InitializeComponent();

        parentForm = parent;
        parentForm.ParentProperty = "Value from Child";
    }   
}

If ChildForm wants to access the ParentForm

Pass ParentForm instance to the ChildForm constructor.

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

    public string ParentProperty{get;set;}

    private void CreateChild()
    {
         var childForm = new ChildForm(this);
         childForm.Show();
    }
}

public partial class ChildForm : Form
{
    private ParentForm parentForm;

    public ChildForm(ParentForm parent)
    {
        InitializeComponent();

        parentForm = parent;
        parentForm.ParentProperty = "Value from Child";
    }   
}
深爱成瘾 2024-10-21 19:47:05

我通常使用单例设计模式来实现这样的http://en.wikipedia.org/wiki/Singleton_pattern< /a> .我将创建应用程序在单例下运行的主窗体,然后创建我想要在其他区域触摸的窗体和控件的访问器。然后,其他窗体可以获得指向它们想要修改的控件的指针,或者它们想要更改的应用程序主要部分中的数据。

另一种方法是在不同的表单上设置事件以进行通信,并使用主表单作为各种类型的中心,将事件消息从一个表单传递到应用程序中的另一个表单。

I usually use the Singleton Design Pattern for something like this http://en.wikipedia.org/wiki/Singleton_pattern . I'll make the main form that the application is running under the singleton, and then create accessors to forms and controls I want to touch in other areas. The other forms can then either get a pointer to the control they want to modify, or the data in the main part of the application they wish to change.

Another approach is to setup events on the different forms for communicating, and use the main form as a hub of sorts to pass the event messages from one form to another within the application.

如歌彻婉言 2024-10-21 19:47:05

这很简单,首先您可以像这样访问其他表单:
(假设您的其他表单是 Form2

//in Form 1
Form2 F2 = new Form2();
foreach (Control c in F2.Controls)
         if(c.Name == "TextBox1")
            c.Text = "hello from Form1";

就是这样,您只需在 Form1Form2 中的 TextBox1 中写入即可。

It's easy, first you can access the other form like this:
(let's say your other form is Form2)

//in Form 1
Form2 F2 = new Form2();
foreach (Control c in F2.Controls)
         if(c.Name == "TextBox1")
            c.Text = "hello from Form1";

That's it, you just write in TextBox1 in Form2 from Form1.

贪恋 2024-10-21 19:47:05

这里还有另一个执行“查找并突出显示”的示例。还有第二个表单(模式),它打开并包含一个文本框来输入一些文本,然后我们的程序在 RichTextBox(在调用表单中)中查找并突出显示搜索到的文本。 为了在调用表单中选择RichTextBox元素,我们可以使用.Controls.OfType()方法:

private void findHltBtn_Click(object sender, EventArgs e)
{
    var StrBox = _callingForm.Controls.OfType<RichTextBox>().First(ctrl => ctrl.Name == "richTextBox1");
    StrBox.SelectionBackColor = Color.White;

    var SearchStr = findTxtBox.Text;
    int SearchStrLoc = StrBox.Find(SearchStr);

    StrBox.Select(SearchStrLoc, SearchStr.Length);
    StrBox.SelectionBackColor = Color.Yellow;
}

也在同一个类中(modal的表单) ,要访问调用表单,请使用@CuiousGeek 的答案中提到的技术:

public partial class FindHltModalForm : Form
{
        private Form2 _callingForm = null;
        public FindHltModalForm()
    {
        InitializeComponent();
    }
    public FindHltModalForm(Form2 CallingForm)
    {
        _callingForm = CallingForm;
        InitializeComponent();
    }
//...

Here's also another example that does "Find and Highlight". There's a second form (a modal) that opens and contains a textbox to enter some text and then our program finds and highlights the searched text in the RichTextBox (in the calling form). In order to select the RichTextBox element in the calling form, we can use the .Controls.OfType<T>() method:

private void findHltBtn_Click(object sender, EventArgs e)
{
    var StrBox = _callingForm.Controls.OfType<RichTextBox>().First(ctrl => ctrl.Name == "richTextBox1");
    StrBox.SelectionBackColor = Color.White;

    var SearchStr = findTxtBox.Text;
    int SearchStrLoc = StrBox.Find(SearchStr);

    StrBox.Select(SearchStrLoc, SearchStr.Length);
    StrBox.SelectionBackColor = Color.Yellow;
}

Also in the same class (modal's form), to access the calling form use the technique mentioned in the @CuiousGeek's answer:

public partial class FindHltModalForm : Form
{
        private Form2 _callingForm = null;
        public FindHltModalForm()
    {
        InitializeComponent();
    }
    public FindHltModalForm(Form2 CallingForm)
    {
        _callingForm = CallingForm;
        InitializeComponent();
    }
//...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文