Accessing data on main form from another form

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

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
 
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)
}
 
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
 
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,

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
 
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!

Morten Wennevik said:
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);
}
}
 
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!

Morten Wennevik said:
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);
}
}
 
Back
Top