Click here to Skip to main content
15,916,941 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i have 2 forms,namely form1 and form2. Form1 is the main form and form2 is secondary form which contains 2 checkbox namely checkbox1 and checkbox2. checkbox1 is suppose to disable a button in form1.
what i want is that after the buttons are disable i want form1 is save so that when i close and open form1 i should see the changes has taken place
Posted

The check box is easy: create an event and a property in Form2 which Form2 handles. When the checkbox is clicked, signal the event and form1 can access the property to decide if the button should be enabled or disabled.
Transferring information between two forms, Part 2: Child to Parent[^] Shows you how to do it.
Saving is more complex: it depends on a huge number of factors, such as how you want to save it, how much there is to save, and so forth.
The easiest is to use Settings, then there is a file you read and write yourself, then CSV, XML, a database, and finally the registry - and that list is in the order in which I would start looking.

So how much do you want to save?
 
Share this answer
 
Comments
MaximusDebois 19-Mar-14 5:31am    
using the settings
OriginalGriff 19-Mar-14 5:34am    
And you know how to do that? Or do you need help?
MaximusDebois 19-Mar-14 5:46am    
yes i need help please
OriginalGriff 19-Mar-14 6:29am    
OK - look at your project in VS, and open the "Properties" branch in the Solution explorer pane.
Double click on "Settings.settings" and a settings page will open.
Change the "Name" to "ButtonEnabled" (or a name that makes sense given what the button does) and change the "Type" to "bool". Leave "Scope" at "User", but set "Value" to "True" - this will allow your button to power up enabled when it runs for the first time.
Save and close the settings page.
Now, if you don't have one, add a Load event handler to your Form1, and add the line:
myButton.Enabled = Properties.Settings.Default.ButtonEnabled;
Then, when you change the Enabled status just add:
Properties.Settings.Default.ButtonEnabled = myButton.Enabled;
Properties.Settings.Default.Save();
And the system will do everything else for you as regards storing it.
gggustafson 19-Mar-14 14:19pm    
I like that - good answer!
Hi,

Assuming that you have 2 buttons on Form1, and their names are: button1 and button2, you need to create settings (1 link will be good for start) for each control state you want to save. Example:
1. Setting name: Button1Enabled, Type: Bool
2. Setting name: Button2Enabled, Type: Bool

Then you should handle your Form1 events to store/restore buttons state.
1. When the form1 is closed save your settings to configuration file:
C#
protected override void OnClosed(EventArgs e)
{
    base.OnClosed(e);
    Properties.Settings.Default.Button1Enabled = button1.Enabled;
    Properties.Settings.Default.Button2Enabled = button2.Enabled;
    Properties.Settings.Default.Save();
}

2. Whe the form1 is loaded load your setting from configuration file:
C#
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    button1.Enabled = Properties.Settings.Default.Button1Enabled;
    button2.Enabled = Properties.Settings.Default.Button2Enabled;
}


For further referrence you should take a look at this links:
Windows Forms User Settings in C#[^]
http://msdn.microsoft.com/en-us/library/0zszyc6e%28v=vs.110%29.aspx[^]

Hope it helps you :)
 
Share this answer
 
v2
Comments
MaximusDebois 19-Mar-14 6:44am    
what about using the setting file in form2 save button disable in form1
Marcin Kozub 19-Mar-14 8:12am    
Did you read my answer and articles to which links were provided? Application settings are available to your entire application and are accesible from any place of your code.
Please explain more clearly what are you trying to achieve...
XML
in general for work in this domain you should have some knowledge of reflextion (meta programming)
but for your problem you can use <b>control.controls</b> to coloect list of control
then save in an suitable data structure.
use code same as this :
<pre lang="c#">
        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach(Control c in this.Controls)
                if(c is CheckBox)
                    listBox1.Items.Add(c.Text+":"+(c as CheckBox).Checked);
        }
 
Share this answer
 
v3

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