Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi
I am building a kids web browser and i need to change the view of some of the buttons in the main browser depending on if the parent wants that option available to the child.

I have made a form which will allow the parent to show or hide printer button but since it is in a new form it doesn't recognise the name of the buttons in the main form

i know this is really simple, but i don't know how to join them

here is my code

public partial class printer : Form
    {
        public printer()
        {
            InitializeComponent();
        }
        public void btnShowPrinter_Click(object sender, EventArgs e)
        {
            loggedIn = false;
            btnPrint.Visible = true;
        }
        private void btnHidePrinter_Click(object sender, EventArgs e)
        {
            loggedIn = false;
            btnPrint.Visible = false;
        }
    }




loggedIn and btnprint are both getting errors

The name 'btnPrint' does not exist in the current context

how do i make it read from the other form please?

thank you in advance
Posted
Updated 5-May-11 5:20am
v2

The way to go here is that the form that does the configuration will modifiy a reference of the configuration model for your application. The other form you referenced will get the configuration model when initializing itself to reflect in its GUI what was configured. If the configuration model changes while the latter form is still open you should introduce a configuration changed event for which said form will hook up an event handler. Thus if the browser form was openend when the configuration changed it will be notified of that event and can change it's GUI accordingly.

Best Regards,

-MRB
 
Share this answer
 
Comments
Marc A. Brown 5-May-11 14:01pm    
Great answer. And of course you can also persist the config and reload it the next time app starts as well.
sportsonpc 5-May-11 18:01pm    
thanks for the help
Please don't!
That's the simple answer. You can't access the buttons on a differnt form because they are declared private by default. If they weren't, then the two forms are always tied together - if you change one, you have to look at how that change might affect the other form. This makes re-use difficult, and can affect reliability.

Instead, provide a public property or method in the second form, and access them from the first. The property or method makes changes to the form controls, so you can change the controls, without affecting the outside world.

All that leaves is for you to interact with the correct instance of the second form!
When you displayed, it you did something along the lines of:
MySecondForm secondForm = new MySecondForm();
secondForm.Show();
All you have to do is move secondForm to be a private class level variable, and you can access it when you need to:
secondForm.DisablePrinting();
or
secondForm.CanPrint = false;
 
Share this answer
 
Comments
Tarakeshwar Reddy 5-May-11 11:16am    
5, for the answer and correcting me.
Ed Nutting 5-May-11 12:28pm    
+5 that :) Much agreed not to make buttons public unless that's exactly what you want.
Marc A. Brown 5-May-11 13:59pm    
Terrific answer.
sportsonpc 5-May-11 18:01pm    
thanks for the help
Sergey Alexandrovich Kryukov 5-May-11 19:59pm    
Totally agree on "don't". My 5. As to collaboration in general, please see my answer.
--SA
Store the values in the registry. When the options form loads parse registry entries that dictate control state.. eg, dword reg entry chk1 = 0, checkbox loads unchecked, = 1, loads checked.
Another method is using the applications settings page, create a bool value, then access it through the forms.
bool ButtonVisible setting
this.btnOption.Visibile = Properties.Settings.Default.ButtonVisible;
Note you have to call: Properties.Settings.Default.Save(); after changing a value.

Here's a more complete example, including using the PropertyChanged event on the main form to propogate the change..
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        //Properties.Settings.Default.ButtonVisible = true;
        Properties.Settings.Default.PropertyChanged += new PropertyChangedEventHandler(Default_PropertyChanged);
    }
    void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        button1.Visible = Properties.Settings.Default.ButtonVisible;
        this.Refresh();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        frmOptions f = new frmOptions();
        f.Show(this);
    }
}


options form:
MSIL
<pre lang="cs">
    public partial class frmOptions : Form
    {
        public frmOptions()
        {
            InitializeComponent();
        }
        private void frmOptions_Load(object sender, EventArgs e)
        {
            Properties.Settings.Default.ButtonVisible = false;
            Properties.Settings.Default.Save();
        }
    }
 
Share this answer
 
v5
Comments
Marc A. Brown 5-May-11 14:03pm    
Nice point about persisting the configuration.
sportsonpc 5-May-11 18:01pm    
thanks for the help
SKOTAJI 6-May-11 2:12am    
Nice concept !
You can use session variables to store the values and use it in the other form. Take a look at this article [^] on msdn.

Ignore, saw web browser and took it to be asp.net project.
 
Share this answer
 
v3
Comments
sportsonpc 5-May-11 11:06am    
cheers Tarakeshwar

but is it not possible to write something like

Form1.loggedIn -false;

or something similar, i tried that line and it didn't like it

I thought that it was just simply making a reference to the form the button and method was on???
OriginalGriff 5-May-11 11:06am    
Um. Forms don't have sessions?
Tarakeshwar Reddy 5-May-11 11:13am    
My bad, I just read web browser and took it to be Asp.net, didn't see his code.
First of all, I agree with Griff on "don't"!

For the form collaboration, please see my universally applicable method based on implementation of an interface by the form class:
How to copy all the items between listboxes in two forms[^]; please also see other methods and discussion.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900