Reputation: 2075
In usercontrol I have some objects(textbox
,combobox
,etc) . In the form,I have a button that shows or hide some objects from usercontrol. I am trying to call the method from usercontrol but it doesn't work. My code:
usercontrol:
public void MinimMaxim()
{
_txtName.Visible = true;
_txtPackage.Visible = true;
_panelButton.Visible = false;
_txtBody.Visible = false;
_btnPlus.Visible = false;
}
and in form:
//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;
}
Upvotes: 1
Views: 13854
Reputation: 36
I had try Freelancer's answer and it's worked.
User Control class
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;
}
}
}
Form class
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();
}
}
}
if we create this UserControl on runtime the answer is;
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();
}
}
}
Upvotes: 2
Reputation: 1078
To access controls in user control, I normally expose some properties of that control and from the main page I can use properties to play with control.
Upvotes: 2
Reputation: 328
task variable that you create is a local variable to _buttonAdd_Click method. It cannot be accessed from any other method. It has to be a member variable if you want it to be used from other methods.
Upvotes: 3
Reputation: 9074
call method by refering following code through user control>>
yourUserControlName.methodName();
I think in your case it may be :
yourUserControlName.MinimMaxim();
Upvotes: 4