How do I translate this VB.NET assignment to C#?
I usually program in VB.NET, but am trying to use C#.
In VB.NET, if form1
has a toolStripButton1
and in UserControl
I usually write like this:
Dim first As New form1
first.toolStripButton1.enable = False
But in C#, I try:
开发者_开发知识库private void MyNameMethod() {
Form1 first = new Form1();
first. ???????
but it doesn't work. How do I translate the above VB.NET code? I can not select toolStripButton1
.
Make sure toolStripButton1
is actually on Form1
and set to public
.
first.toolStripButton1.enable = false;
The instance is first
, form1
is the name of the class.
Open Form1.Designer.cs [it is default class, may find easily from solution window, under Form1]
At the end of Form1.Designer.cs Controls are listed (as private Controls), change your control to public which you want to access.
Than on your UserControl's cs file add this codes:
private Form1 myForm;
public void AssignForm(Form1 _myForm)
{
myForm = _myForm
}
private void AccessControl()
{
myForm.myButton.Enable = ! myForm.myButton.Enable;
}
Open your Form1.cs file and you AssignForm function so that your UserControl may access your main Form. Put this code in your Form1-Constructor:
public Form1()
{
InitializeComponent();
myUserControl1.AssignForm(this);
}
This is one of the ways to access to Form1 controls in an easy way.
精彩评论