Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / April 2005

Tip: Looking for answers? Try searching our database.

Accessing data on main form from another form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave - 01 Dec 2004 18:37 GMT
Can someone tell me how I would go about setting up a form the accesses the
controls on another form in VC.NET? I have a graph set up on the main form
and would like to open another form that has formatting options for the
graph. Changes on the options form will update the properties of the graph
on the main form.

Thanks in advance for your help.

Dave
Morten Wennevik - 01 Dec 2004 18:53 GMT
Hi Dave,

In your settings form create a public/protected property for each of the  
parameters (or group of parameters) you want to display.  Set the values  
the constructor or as separate properties.  When closing  
(DialogResult.OK/Yes etc) update the main form properties.

This sample will open up an OptionDialog in the Options method of  
MainClass.  It will then set the initial properties before displaying, and  
retrieve the these properties if the user clicks OK (assuming it will fire  
DialogResult.OK).

public MainClass
{

    private void Options()
    {
        OptionDialog od = new OptionDialog();
        od.Left = graphStart.Left;
        od.Top = graphStart.Left;
        od.GraphName = "Hello World";

        if(od.ShowDialog() == DialogResult.OK)
        {
            graphStart = new Point(od.Left, od.Top);
            graphName = od.GraphName;
        }
    }
}

public OptionDialog
{
    private TextBox leftBox;
    private TextBox topBox;
    private TextBox graphBox;

    public int Left
    {
        get
        {
            return Int32.Parse(leftBox.Text); // you might want to try/catch
        }
        set
        {
            leftBox.Text = value.ToString();
        }
    }

    // same with top and graph (int/string)
}

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Dave - 01 Dec 2004 19:55 GMT
Morten,

This works great, but I was really trying to update the MainClass graph
while the OptionDialog is still visible. This would allow the user, for
example, to see the result of the option changes made as they experiment
with various selections rather than having to close the window after each
trial.

Dave

> Hi Dave,
>
[quoted text clipped - 46 lines]
> // same with top and graph (int/string)
> }
Morten Wennevik - 01 Dec 2004 20:34 GMT
In that case, pass 'this' as a parameter in the OptionDialog constructor  
and create public properties in the MainClass instead.

public MainClass
{
    public int Left
    {
        get
        {
            return graphStart.Left;
        }
        set
        {
            graphStart.Left = value;
            RedrawGraph();
        }
    }

    private void Options()
    {
        OptionDialog od = new OptionDialog(this);
        od.ShowDialog();
    }
}

public class OptionDialog
{
    private MainClass parent;
    private TextBox leftBox;
    private TextBox topBox;
    private TextBox graphBox;

    public OptionDialog(MainClass caller)
    {
        parent = caller;
    }

    btUpdate_Click(object sender, EventArgs e)
    {
        parent.Left = Int32.Parse(leftBox.Text);
    }
}

> Morten,
>
[quoted text clipped - 62 lines]
>> Happy Coding!
>> Morten Wennevik [C# MVP]

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Marco De Sanctis - 02 Dec 2004 07:45 GMT
Everything's ok with that last example, the only thing I suggest is not to
show the options form as a Modal Form: try using owner-owned relation:
from the graph form code:

private void Options()
{
   OptionDialog od = new OptionDialog();
   this.AddOwnedForm(od);
   od.Show();
}

From OptionDialog form you can refer to Graph Form looking at the property
Owner. Owned forms are great: you can still interact with your main form,
but they are never painted behind their owner. Moreover, if you close or
minimize the Owner Form, all the Owned ones are closed or minimized too.

Bye!

> In that case, pass 'this' as a parameter in the OptionDialog constructor
> and create public properties in the MainClass instead.
[quoted text clipped - 105 lines]
>>> Happy Coding!
>>> Morten Wennevik [C# MVP]
GrkEngineer - 20 Apr 2005 00:55 GMT
I'm trying to implement this, but I'm running into C++ errors. In the .h file
containing my MainClass I include a header referring to the ChildClass. But
when I want to access any methods or variables from the MainClass from my
ChildClass, I need to do a typecast. Well my ChildClass has no clue what
MainClass is. When I try to add an include in my ChildClass the MainClass.h
file to define MainClass I get errors. Please help!

> In that case, pass 'this' as a parameter in the OptionDialog constructor  
> and create public properties in the MainClass instead.
[quoted text clipped - 105 lines]
> >> Happy Coding!
> >> Morten Wennevik [C# MVP]

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.