tags:

views:

212

answers:

6

Hi,

i have opened a mainform and call a child form like

     Form4 f = new Form4();
     f.Owner = this;
     f.Show(this);

in form4, user selects a text file, the contents of which are to be displayed in a textBox1 of mainform

i was trying something like Owner.textBox1.Text = "file contents";

but it does'nt work

thanks in advance

apals

A: 

You will need to set the "modifiers" to at least public for the properties of the control to be able to have access to it.

alt text

gmcalab
Or write a property that will grant restricted access to the targeted control's property. That is the prefered practice, though yours will work as well.
Will Marcouiller
+6  A: 

The best way to link different forms together is via events. Create an event in Form4 like FileSelected and then do something like this:

Form4 f = new Form4();
f.FileSelected += (owner, args) => {
    textBox1.Text = args.FileName;
};
f.Show(this);
Andrew Hare
yes, now there is some processing of the text required so i have to call a method like parse_names(string filename)this method is located in parent form, although i copied and pasted it again in child form, still is there a shorter way. plus the contents of the parsed_text is sent to textBox1 of mainformthanx in advance
arvind
@arvind: If you have method that you want to use in multiple forms, then create a (helper) class and move that method over there.
Hans Kesting
@arvind: if you have another problem you should edit your existing question and add the new status of your problem or you ask a complete new question, if you run into a new problem. Otherwise less people will see the current status thous less people can help you.
Oliver
the above solution helps , but there is a method parse_text to be called
arvind
i m sorry i did not mention it above, because at that time that was the BIG problem
arvind
@Andrew Hare: Will the "owner" parameter be of the correct type? Still textBox1 is owned by the parent form. And the Form class has no TextBox control. So, don't you need to type-cast owner? Am I missing something? Thanks for you explanation! =)
Will Marcouiller
thank you, but i have really found a very good way to do it. it seems flawless, i just wrote the following Form z = Application.OpenForms["mainform"]; ((mainform)(z)). parse_names(filename); it not only reduces the number of copies of parse_name method, but i dont have to declare textbox1 as global. i hope it will help others too
arvind
as i found out, it is a common procedure called crosslinking between forms. that is where is found the solution. now i can call any procedure or object of any form, if required. because placing everything public is compromising on security
arvind
@arvind: Form z = Application.OpenForms["mainform"]; The hurt here is when you refactor your code, this string won't get refactored and will break your code.
Will Marcouiller
i don't think that `Application.OpenForms[string name]` is a very good way.
Oliver
A: 

In Form4 you can cast Owner to the correct type:

var o = (Form1) this.Owner;
o.textBox1.Text = "file contents";

For this to work, the owner must be of type Form1 and textBox1 on that type must be a public member or property.

klausbyskov
thank you, but i have really found a very good way to do it. it seems flawless, i just wrote the following Form z = Application.OpenForms["mainform"]; ((mainform)(z)). parse_names(filename); it not only reduces the number of copies of parse_name method, but i dont have to declare textbox1 as global. i hope it will help others too.
arvind
Haha ok, good for you @arvind, but how was I supposed to know that there was a method called `parse_names` when you did not mention it in your question.
klausbyskov
A: 

Besides this is really bad design, you need to make textBox1 a public member of your main form and cast f.Owner to the main form type.

Like:

 Form4 f = new Form4();
 f.Owner = this;
 f.Show(this);

 // Inside Form4
 MainForm main = this.Owner as MainForm;
 if (main != null) main.textBox1.Text...
Johannes Rudolph
yes, this is really bad design and i have to deter myself not to downvote your answer, cause it is soooo wrrooooonnngg. ;)
Oliver
thank you, but i have really found a very good way to do it. it seems flawless, i just wrote the followingForm z = Application.OpenForms["mainform"];((mainform)(z)). parse_names(filename); it not only reduces the number of copies of parse_name method, but i dont have to declare textbox1 as global.i hope it will help others too.
arvind
@Oliver: Yeah, it really hurt to write this. Done my ten punishment push-ups ;-)
Johannes Rudolph
+1  A: 

A best practice would be to define yourself a property that would itself set the Text property of your private control. Here's an instance:

public partial class MainForm : Form {
    public string ContentDescription {
        set {
            textBox1.Text = value.trim();
        }
    }
}

Then after, you'll be able to access this property through type-casting to your particular type:

public partial class SecondaryForm : Form {
    public MainForm OwnerForm {
        get {
            return (MainForm)this.Owner;
        }
    }

    public void someMethod() {
        OwnerForm.ContentDescription = "file contents";
    }
}

Remember that in C#, every Control is declared private. So, to access it, the best practice is to define a property that will grant you the required access to it. Making a member public is generally not a good idea, depending on what you're trying to achieve.

EDIT For the parse method, perhaps should you consider making it public or internal so that you may access it through the correctly type-casted Owner property of your child form.

Making a hlper class might be the right solution though, so it is not GUI dependent.

Will Marcouiller
+1  A: 

As Andrew already gave the correct solution for event driven, there is also a sync (or blocking) method available:

Form4 f = new Form4;
if(f.ShowDialog() == DialogResult.OK)
{
    textBox1.Text = f.FileName;
}
Oliver