473,386 Members | 1,846 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,386 software developers and data experts.

C# beginners question

Hi

I'm sure there is a very simple answer to this but it's got me stuck
for hours!

How do you change the properties of parent forms? e.g. if you have a
dialog box that has a textbox on it, when the user clicks an OK Button,
and the value is saved to the database, how do you then update the
treeview on the main parent form?

I'm creating a new instance of the form within the child, which gives
me access to the Nodes but it doesn't allow me to change any thing?

To make things simpler, I tried to change the .Text property of the
parent form within the child form but I can't even seem to do that.

I'm sure I'm missing something really simple!

Thanks for your time
Susan

Feb 28 '06 #1
9 1281
Hi,

Well I wouldn't really do it that way.

Your parent form needs to check the result of the child dialog and then act
accordingly. As a general rule you wouldn't have a child telling the parent
what to do - well not in my family :-)

So,

parent creates child dialog
child dialog does stuff
parent checks child state and then thinks "OK, I'll change this text, or
light up this button etc."

Hope this general comment helps,

Cheers,

Adam.
<su*************@googlemail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hi

I'm sure there is a very simple answer to this but it's got me stuck
for hours!

How do you change the properties of parent forms? e.g. if you have a
dialog box that has a textbox on it, when the user clicks an OK Button,
and the value is saved to the database, how do you then update the
treeview on the main parent form?

I'm creating a new instance of the form within the child, which gives
me access to the Nodes but it doesn't allow me to change any thing?

To make things simpler, I tried to change the .Text property of the
parent form within the child form but I can't even seem to do that.

I'm sure I'm missing something really simple!

Thanks for your time
Susan

Feb 28 '06 #2
On 28 Feb 2006 04:05:09 -0800, su*************@googlemail.com wrote:
Hi

I'm sure there is a very simple answer to this but it's got me stuck
for hours!

How do you change the properties of parent forms? e.g. if you have a
dialog box that has a textbox on it, when the user clicks an OK Button,
and the value is saved to the database, how do you then update the
treeview on the main parent form?

I'm creating a new instance of the form within the child, which gives
me access to the Nodes but it doesn't allow me to change any thing?

To make things simpler, I tried to change the .Text property of the
parent form within the child form but I can't even seem to do that.

I'm sure I'm missing something really simple!

Thanks for your time
Susan


Susan,

there are multiple ways of doing this, but first things first. When
clicking the OK button you should set the dialogresult of the
dialogbox to DialogResult.Ok (and initially the dialogresult to
cancel)
When you create the form like this:

if (new MyDialog.ShowDialog() == DialogResult.Ok)
{
// Update treeview
}

You know when the user clicked OK.

You could then reload the entire treeview or (to save time and
resources) ask for the textbox value and update the selectednode.text

greetz, Leon.
Feb 28 '06 #3

<su*************@googlemail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hi

I'm sure there is a very simple answer to this but it's got me stuck
for hours!

How do you change the properties of parent forms? e.g. if you have a
dialog box that has a textbox on it, when the user clicks an OK Button,
and the value is saved to the database, how do you then update the
treeview on the main parent form?


If the dialog is modal then you just update the main form when it returns.

If it is not modal then having an event on the dialog is the cleanest way
forward.

If you think of the dialog as just another sort of control but with rather
more data than a simple control then you can see how to go about things.

Often you'll find that you want to use a something like CancelEventArgs to
allow the main form to veto the action and prevent the dalog from closing.
Feb 28 '06 #4
Adan, Leon

Thanks for your quick replies,

Everything suddenly makes so much more sense!

I've changed things around in the way you suggest and it's working
great now. It's such a great feeling to get around this hurdle! =)

Susan

Leon Friesema wrote:
On 28 Feb 2006 04:05:09 -0800, su*************@googlemail.com wrote:
Hi

I'm sure there is a very simple answer to this but it's got me stuck
for hours!

How do you change the properties of parent forms? e.g. if you have a
dialog box that has a textbox on it, when the user clicks an OK Button,
and the value is saved to the database, how do you then update the
treeview on the main parent form?

I'm creating a new instance of the form within the child, which gives
me access to the Nodes but it doesn't allow me to change any thing?

To make things simpler, I tried to change the .Text property of the
parent form within the child form but I can't even seem to do that.

I'm sure I'm missing something really simple!

Thanks for your time
Susan


Susan,

there are multiple ways of doing this, but first things first. When
clicking the OK button you should set the dialogresult of the
dialogbox to DialogResult.Ok (and initially the dialogresult to
cancel)
When you create the form like this:

if (new MyDialog.ShowDialog() == DialogResult.Ok)
{
// Update treeview
}

You know when the user clicked OK.

You could then reload the entire treeview or (to save time and
resources) ask for the textbox value and update the selectednode.text

greetz, Leon.


Feb 28 '06 #5
Following on from my original message,

How would i go about retrieving a value from the parent form in the
child?

e.g.
A user right clicks on a TreeView node and selects "Rename..."
A dialog form comes up, but how do I populate a textbox with the
SelectedNode on the parent form?

I've tried this:
StockCategories mainForm = new StockCategories();
stockCategoryName.Text =
mainForm.treeView.SelectedNode.ToString();

But mainForm.treeView.SelectedNode.ToString() is null. I guess I'm
going totally wrong with trying to create "mainForm"?

I also tried calling the child form like this: editStockCategory f =
new editStockCategory(treeView.SelectedNode.Text);
and then when declaring the child form I put this: public
editStockCategory(string nodeText)

But I'm not sure how to make nodeText available in the child form Load
event.

Arghhh.

Susan

Feb 28 '06 #6
On 28 Feb 2006 06:19:52 -0800, "su*************@googlemail.com"
<su*************@googlemail.com> wrote:
Following on from my original message,

How would i go about retrieving a value from the parent form in the
child?

e.g.
A user right clicks on a TreeView node and selects "Rename..."
A dialog form comes up, but how do I populate a textbox with the
SelectedNode on the parent form?

I've tried this:
StockCategories mainForm = new StockCategories();
stockCategoryName.Text =
mainForm.treeView.SelectedNode.ToString();

But mainForm.treeView.SelectedNode.ToString() is null. I guess I'm
going totally wrong with trying to create "mainForm"?

I also tried calling the child form like this: editStockCategory f =
new editStockCategory(treeView.SelectedNode.Text);
and then when declaring the child form I put this: public
editStockCategory(string nodeText)

But I'm not sure how to make nodeText available in the child form Load
event.

Arghhh.

Susan


The SelectedNode.ToString() doesn't return to value for the Text
attribute of the node. Use SelectedNode.Text instead.
But first, before doing that, make sure that a node is selected to
prevent errors.

if (MyTreeview.SelectedNode != null)
{
... do my stuff

Setting Text is also not correct. The Text attribute for the form is
just the Dialog caption, not the actual textbox. You could try to set
to modifiers of the textbox to internal or public. That way you could
access the textbox using

mainForm.TextBox1.Text

But I would suggest to create a new property in the code of the form
which uses the textbox like this:
internal string NodeValue
{
get { return Textbox1.Text; }
set { Textbox1.Text = value; }
}

that way you could use
mainForm.NodeValue = SelectedNode.Text

in total: you create the form like this (based upon the "fact" that
your childform performs a database-update all by itself):

StockCategories mainForm = new StockCategories();
mainForm.NodeValue = MyTreeview.SelectedNode.Text
if (mainForm.ShowDialog() == DialogResult.Ok)
{
MyTreeview.SelectedNode.Text = mainForm.NodeValue;
}

Besides that, one suggestion, do not use "mainForm" as variable name,
since it is not your mainForm :-)

greetz,
Leon
Feb 28 '06 #7
HI,

You pass a reference to the parent form to the child form, also you need to
make accesible the controls you want to have access to (either by declaring
them public or by using properties )

Another possible pattern to update one form from other is using a method
that indicate that a changed was performed and the interface (form's
control) need to be reload with updated data. you can do this by declaring
an interface like

public interface IDataUpdated{ void NewDataSaved(); } and having the parent
class implement it, the child could get a reference to that instance and
after the updated call the method.
This approach is more flexible as you can have several child forms and these
forms need not to be modals. (using DialogResult usually imply that the
child form has to be modal)

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<su*************@googlemail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Following on from my original message,

How would i go about retrieving a value from the parent form in the
child?

e.g.
A user right clicks on a TreeView node and selects "Rename..."
A dialog form comes up, but how do I populate a textbox with the
SelectedNode on the parent form?

I've tried this:
StockCategories mainForm = new StockCategories();
stockCategoryName.Text =
mainForm.treeView.SelectedNode.ToString();

But mainForm.treeView.SelectedNode.ToString() is null. I guess I'm
going totally wrong with trying to create "mainForm"?

I also tried calling the child form like this: editStockCategory f =
new editStockCategory(treeView.SelectedNode.Text);
and then when declaring the child form I put this: public
editStockCategory(string nodeText)

But I'm not sure how to make nodeText available in the child form Load
event.

Arghhh.

Susan

Feb 28 '06 #8

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2****************@TK2MSFTNGP12.phx.gbl...
HI,

You pass a reference to the parent form to the child form


Then you have created a mutual dependency between the forms. They cannot be
developed, tested or deployed independently.

Use interfaces and/or events.
Mar 1 '06 #9
Hi,

"Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message
news:qD******************@fe3.news.blueyonder.co.u k...

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
HI,

You pass a reference to the parent form to the child form


Then you have created a mutual dependency between the forms. They cannot
be developed, tested or deployed independently.

Use interfaces and/or events.


True, you create a dependency, the best way is th use an interface that
expose only the needed public data member that you need


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 1 '06 #10

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

Similar topics

7
by: Will | last post by:
Pardon two post in a row to the newsgroup but I want to try and expedite this, if you guys don't mind helping out... I running Windows XP Pro and wanted to download Python and any additional...
4
by: blah | last post by:
I m actually a Novice in Python as well as Linux, When i look up things on the internet about Linux Flavours, They are written so complex that it is difficult for me to understand, i am asking if...
4
by: Eggnog | last post by:
Hi, Is there a newsgroups for beginners questions? Cheers, Nawg
6
by: William Foster | last post by:
Does anyone know of a good online tutorial for C# focused on beginners. I have been to many great sites like csharpfriends, csharp-corner etc looking for good tutorials and have had no luck. Any...
18
by: John Salerno | last post by:
Hi all. I was hoping some of you could recommend a book or two that would help me get started with the basics of C#. I have a slight knowledge of programming, but basically I want to start out like...
2
by: =?Utf-8?B?UmFrZXNo?= | last post by:
Hi, I have worked in Windows development for some time and want to learn ASP.Net - Web Application. Can some one redirect me to some site where I can find some good start up for beginners course...
4
by: aman firoz | last post by:
do you people got anything for c beginners..... help me out guys
0
by: Dual_b00t | last post by:
hi i created a site called PHP Together its for beginners also for gurus to help out the beginners if you are learning PHP and feel alone then drop by . ciao Mark
19
by: yltkhuu | last post by:
1. How does having a widely adopted C++ standard help game programmers? 2. What are the advantages ans disadvantages of employing the "using" directive? 3. Why might you define a new name for an...
6
by: Lars | last post by:
Hi all If this is the wrong list for beginners trouble I apologize. please refer to me the correct list in such case. I am trying to create a simple linked list but I keep getting segmentation...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.