473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Accessing function and elements from one form on other

I have two window app. I have Tree in main window and in code of that window
I have function BuildTree(); This function start with tree.Clear(); So it
completely rebuild tree. When I open other window as dialog after clicking on
`Save` button I whant call BuildTree() function. Sorry, I am jus a biginer.
How am I suupoused to do that? An yet I whant change lable text on main
window. How to access lable control on main form from dialog window?

Thanks in advance
Nov 16 '05 #1
2 1757
Hi Sergey,

The dialog needs to have a reference to the main form.
If you create the Dialog from MainForm you can add an overloaded constructor accepting a MainForm parameter

// in MainForm
Dialog d = new Dialog(this); // this would be MainForm
d.Show()
....
// in Dialog
public class Dialog
{
private MainForm parent;
public Dialog(MainForm m)
{
parent = m;
}
}

Then, whenever you need to do something on MainForm, like calling BuildTree you can do

parent.BuildTree();

If when clicking Save you also close the Dialog a better way would be to expose properties in the Dialog that MainForm uses

//in MainForm
Dialog d = new Dialog();
if(d.ShowDialog() == DialogResult.Yes)
{
// access properties of the Dialog if necessary
BuildTree();
}

// in Dialog
private void buttonSave_Click(object sender, EventArgs e)
{
// if you have assigned a dialogresult to the button you can simple close the form
// otherwise you will have to set it now
this.DialogResult == DialogResult.Yes;

// any resulttype would work as long MainForm knows it
this.Close();
}

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
The easy and ugly way for doing so is to make public properties and methods
in the main window, and then pass a reference to the main window instance to
the dialog (so that you create a constructor that takes a main form instance
in that dialog), from which you call upon the functions you want in main
form.

However, there are better and more respected ways. The point is that you
must try to hide main form from the dialog, while still being able to
actually access it.
Let's say that you define an interface that the main form implements:

interface ITreeForm
{
// Method for modifying label's text.
void SendMessage(string text);

// Method for rebuilding the tree.
void BuildTree();
}
So that you could define the code for dialog's constructor:

// a local var to keep the form around:
ITreeForm form = null;

public MyDialogCtor(ITreeForm treeform)
{
form = treeform;
}

// The save method
void Save()
{
// do your saving..
...
// and then call upon the interface..
if ( ITreeForm != null )
{
form.BuildTree();
form.SendMessage("Whatever");
}
}

Implementing this interface by the main form means that any other form can
call functions on it whithout knowing that it is actually, "the main form".
Thus you can pass the interfce reference around.

"Sergey Romanov" <serg4172(NoSpam)@mail.ru> wrote in message
news:AA**********************************@microsof t.com...
I have two window app. I have Tree in main window and in code of that window I have function BuildTree(); This function start with tree.Clear(); So it
completely rebuild tree. When I open other window as dialog after clicking on `Save` button I whant call BuildTree() function. Sorry, I am jus a biginer. How am I suupoused to do that? An yet I whant change lable text on main
window. How to access lable control on main form from dialog window?

Thanks in advance

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DU | last post by:
Assuming you have a typical form built in this manner: <form action="..."> <p><input id="idInputText" name="nameInputText" type="text" size="20"></p> <p><input id="idCheckbox"...
4
by: f_salazar | last post by:
English Version !! Hi !! I have a page with a Form, inside the form I have listbox with 'X' elements. Wend I submit the page, an ASP process inside the page have to read those elements and...
5
by: Craig Anderson | last post by:
Can anyone tell me the best way to access a hidden object in a form? I could use a hard-coded index to the elements of the form, but it's too easy to add something before the hidden object and mess...
3
by: Christopher Benson-Manica | last post by:
I appreciate all the responses to my earlier post about accessing named elements. However, I'm still wondering about my actual problem, which is that I need to initialize some arrays of named...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
7
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
6
by: Chuck Anderson | last post by:
My knowledge of JavaScript is limited. I learn from example and then adapt those examples to suit my needs. I have stumped myself on this one. I have a form with checkboxes that I want to...
3
by: judy.j.miller | last post by:
Does anyone know why i can't access a form element value using dot notation in firefox, when i'm in a function. Works ok in the body. I'm trying to do this: var FarTemp = faren.temp.value; I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.