gpt4 book ai didi

c# - 用户控件形式的call方法

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:24 24 4
gpt4 key购买 nike

在用户控件中,我有一些对象(textboxcombobox等)。在表单中,我有一个按钮,用于显示或隐藏用户控件中的某些对象。我正在尝试从usercontrol调用该方法,但是它不起作用。我的代码:

用户控制:

public void MinimMaxim()
{
_txtName.Visible = true;
_txtPackage.Visible = true;
_panelButton.Visible = false;
_txtBody.Visible = false;
_btnPlus.Visible = false;
}


并采用以下形式:

//method that creates taskcontrols at every button click
private void _buttonAdd_Click(object sender, EventArgs e)
{
TaskControl task= new TaskControl();
}
//call function from usercontrol
private void button_Click(object sender, EventArgs e)
{
task.MinimMaxim = true;
}

最佳答案

我尝试了Freelancer的答案,并且成功了。

用户控制类

using System;
using System.Windows.Forms;

namespace SOF_15631067
{
public partial class UserControl1
: UserControl
{
public UserControl1()
{
InitializeComponent();
}

private void UserControl1_Load(object sender, EventArgs e)
{

}

public void MinimMaxim()
{
_txtName.Visible = true;
_txtPackage.Visible = true;
_panelButton.Visible = false;
_txtBody.Visible = false;
_btnPlus.Visible = false;
}
}


}

表格类

using System;
using System.Windows.Forms;

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

private void button1_Click(object sender, EventArgs e)
{
userControl11.MinimMaxim();
}
}


}

如果我们在运行时创建此UserControl,答案是:

using System;
using System.Windows.Forms;

namespace SOF_15631067
{
public partial class Form1 : Form
{
UserControl1 uc1 = new UserControl1();
public Form1()
{
InitializeComponent();

**Controls.Add(uc1);**
}

private void button1_Click(object sender, EventArgs e)
{
uc1.MinimMaxim();
// userControl11.MinimMaxim();
}
}


}

关于c# - 用户控件形式的call方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15631067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com