Wednesday, April 2, 2008

Accessing a control on another form.

The way to do this is easy but hard to understand. I'll just tell ya the code, and if you need additional explanation let me know.

In Form1, have a function like:

public void ToggleButton(bool toggle)
{
Button1.Enabled = toggle
}


When you call Form2, do the following:
Form frmForm2 = new Form2();
frmForm2.Owner = this; //gives access to form 1's functions.
frmForm2.show();

Now in your close event of Form2, you can toggle the button by doing something like:
((Form1)this.Owner).ToggleButton(true);

Best of Luck to you,

Another way I found is below...

Declare Form1 in Program.cs file as below.

namespace TEST
{
static class Program
{

static public Form1 Frm;
//static public hudaldaa_list hudaldaa_list

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Frm1= new Form1();
Application.Run(Frm1);
}
}
}

And button on From1 should be public.

Now you can change property of button on Form1 from anywhere like
Program.Frm1.button1.Enabled = false;


Read more!!!

... to change the properties of controls on another form.

You have two options for exposing properties of hosted controls from a form. The intuitive option is simply to make the control field public:

class Form1 : Form {

public Label labelInForm1; // Don't!
...

}

Now, when Form2 needs access to the control in Form1, it can access it directly, like so:

class Form2 : Form {

Form1 form1; // Needs to be set from somewhere
void setLabelInForm1_Click(object sender, EventArgs e){
// Set a control's property on Form1
form1.labelInForm1.Text = textForLabelInForm1.Text;

}
...
}


For this to work, the Form2 object needs to have access to the Form1 object. Form2 would normally get access to an object by exposing a property so that whoever has access to Form1 can share that access with Form2:

class Form2 : Form {
...
Form1 form1; // Set by the property
public Form1 Form1 {
get { return form1; }
set { form1 = value; }
}
}

For example, maybe Form1 creates Form2 and sets the Form1 property as it does so:

void Form1_Load(object sender, EventArgs e) {
Form2 form2 = new Form2();
form2.Form1 = this; // Allow Form2 to access Form1 public members
form2.Show();
}

Now, Form2 has access to all public members of Form1, including any controls exposed as public fields. However, there’s a problem. The problem is that if Form1 decides to make the Label control editable, it may well need to change the Label to a TextBox. Now, because the field has been exposed to the world, whenever Form1’s implementation needs to change, it has to update the client code as well, or the compiler will complain. Instead of exposing fields directly, it’s almost always better to expose public properties whose implementation can change, but whose signature remains the same:


class Form1 : Form {
...
private Label labelInForm1; // Fields should be private
public string LabelText {
get { return labelInForm1.Text; }
set { labelInForm1.Text = value; }
}
}

Once you use a property, the access code is actually simpler, since Form2 is accessing Form1 and not worrying about the internal implementation details:

void setLabelInForm1_Click(object sender, EventArgs e) {
// Set a property on Form1 (w/o worrying about the implementation)
form1.LabelText = textForLabelInForm1.Text;
}

Now, when Form1 needs to change its implementation from a Label control to a TextBox control, it can do so:

class Form1 : Form {
...
private TextBox textboxInForm1; // Not exposed to client
public string LabelText { // Client need not change
get { return textboxInForm1.Text; }
set { textboxInForm1.Text = value; }
}
}

By keeping fields private, internal implementation details can change w/o affecting clients. This is encapsulation and abstraction, both core principles of object-orientation.

All the bests...


Read more!!!

Getting info from a control on another form

Question

I know it's best to use objects instead of pulling info directly from a control, but just for my understanding, please help me figure this out:

It seems to me that if I create a textbox on Form1 and make it Public, I should then be able to put a label on Form2 and make it display the contents of the textbox by putting code like this in Form 2's load event:

label.text = form1.textbox.text

But it doesn't work. So, minor syntax issues aside, what am I not understanding? How *would* I directly grab the text value from Form 1's textbox, without using an object?

ANSWER
I know it's best to use objects instead of pulling info directly from a control, ...

A control an object, so no problem there.

It seems to me that if I create a textbox on Form1 and make it Public, I should then be able to put a label on Form2 and make it display the contents of the textbox

Yep.

by putting code like this in Form 2's load event:
label.text = form1.textbox.text

Ah. Now that doesn't work, as you've found. And it gives the cryptic message "Reference to a non-shared member requires an object reference." So what does it mean?

You declare Form1 like so:
Public Class Form1 : Inherits System.Windows.Forms.Form

Looking at it literally it says create a called Form1. It doesn't say create a Form called Form1. In fact the Form is created additionally the Class but this is behind the scenes. [It's easier to understand if you do C# because there, you must create the initial form yourself.]

If you use the name Form1 in your code, you are still talking about the Class. There no Form called Form1. This is all very different to earlier VBs, so it's no surprise that it's confusing.

How *would* I directly grab the text value from Form1's textbox, without using an object?

You to use an object. The nameless Form created as an instance of Class Form1 an object.

What you do is tell your second Form who your first is. In other words you give it an obect reference.

Here's some code.

In Class Form1:
Private Sub Form1_Load(...) Handles MyBase.Load
1 Dim Form2 As New Form2 (Me)
Form2.Show
End Sub

In line 1 it says create an instance of Form2 and assign it to an object variable called Form2. This is allowed but . Don't do it. It can cause untold confusion.

Here's some better code.

In Class Form1:
Private Sub Form1_Load(...) Handles MyBase.Load
1 Dim MyForm2 As New Form2 (Me)
2 MyForm2.Show
End Sub

And In Class Form2:
3 Public Sub New (FirstForm As Form1)
4 Me.New()
5 Label1.Text = FirstForm.TextBox1.Text
End Sub

In line 1 the first Form creates an instance of Form2 and passes itself to the new Form.
In line 2 it shows the new Form.
Line 3 declares a constructor for objects of Class Form2 which takes another Form as a parameter.
In line 4, it calls the constructor for Class Form2 as this actually sets everything up. Forget this and you'll get an error on line 5.
(Try it.) [The other option would be to copy the contents of New() into this constructor, but it's easier and less error-prone to simply call it.]
Line 5 gets the text from the first Form's TextBox, as you wanted.

As you can see, an object reference is required to do the job. And MyForm2
won't know about the first Form until it's told.

All the best...

/www.byte.om/forum/


Read more!!!