Can someone help with controls? (C# Windows Froms)

Posts 1–12 of 12 · Page 1 of 1
Can someone help with controls? (C# Windows Froms)
I need help using controls from different forms, for example:
Form1 has a checkbox

Form2 has a button

When you click the button on form2 then the checkbox on form1 is checked
I don't really advice you doing such thing.. but here is a solution (Among toons of them).

Simply create your starting form at Program.cs globally and access it's controls anywhere in the program.

[php]static class Program
{
public static Form1 mainForm;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(mainForm = new Form1());
}
}
[/php]

to access:

[php]((CheckBox)Program.mainForm.Controls["chkForm1"]).Checked = true;[/php]

Although i don't really advice this. You can also create methods, proprieties, etc.. change your controls modifier to public or internal.. w/e

Just play around with those objects/proprieties.
idk if i rembered this correctly but wasnt it easier to do it like this:

[php]
Form1 fm1 = new Form1();
fm1.checkbox1.checked = true;
[/php]
Quote Originally Posted by Kuro Tenshi View Post
idk if i rembered this correctly but wasnt it easier to do it like this:

[php]
Form1 fm1 = new Form1();
fm1.checkbox1.checked = true;
[/php]
sry for the old bump but just realized that this post was here now..

No, you can't access it like that and change it directly. Only with a created propriety probably, and maybe that won't work either. I tested multiple ways before.
Ehm?
Like this in VB.Net, for a button on Form2 :
button2_Click
if Form1.TextBox1.Text = "Hacker"
Form3.Show
Else
Me.Close
Quote Originally Posted by ZEROI9 View Post
Ehm?
Like this in VB.Net, for a button on Form2 :
button2_Click
if Form1.TextBox1.Text = "Hacker"
Form3.Show
Else
Me.Close
I Advice you to read the OP post again instead of posting useless stuff. Plus if this is C# section. Post in C#... What help would you give posting on a different language?
Got another thing that works obtaining and changing if im correct (sure did help me out before think ill need to use it in my current project to...)

Form Application:

Form1:
Code:
Form2 frm2;

public Form1()
{
initialize();
frm2 = new Form2(this);
}
Form2:
Code:
private Form1 form1;
public Form2(Form1 frm1)
{
initialize();
form1 = frm1;
}

Form2_Load(object sender,EventArgs e)
{
form1.Location = new Point(0.0);
}
for example.


Works Compiled it and works like this:

Form1:
Code:
namespace controlbox
{
    public partial class Form1 : Form
    {
        Form2 frm2;
        public Form1()
        {
            InitializeComponent();
            frm2 = new Form2(this);

        }
        public CheckBox Checkbox1
        {
            get
            {
                return checkBox1;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            frm2.Show();
        }

        

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
Form2:
Code:
namespace controlbox
{
    public partial class Form2 : Form
    {
        bool statuscheck = false;
        private Form1 form1;
        public Form2(Form1 frm1)
        {
            InitializeComponent();
            form1 = frm1;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            statuscheck = !statuscheck;
            form1.Checkbox1.Checked = statuscheck;
        }
    }
}

though i do know that you can use the main form to change all others... but this bypasses the main one making it possible to interact with more then 1.
(noob bumper should know better.)
You can also use a static boolean, and just use

Form2:
Code:
Form1.(StaticBooleanVariable) = true;
Form1:
Code:
checkbox1.Checked = (StaticBooleanVariable);
Then again you'll havta keep checking the staticbooleanvariable if it changed...
Quote Originally Posted by User1 View Post
You can also use a static boolean, and just use

Form2:
Code:
Form1.(StaticBooleanVariable) = true;
Form1:
Code:
checkbox1.Checked = (StaticBooleanVariable);
Then again you'll havta keep checking the staticbooleanvariable if it changed...
so u can do it like this? *i assume dont have C# installed yet*

form2:
[html]
Bool ChangeBoolValue = false

public void Button1_Click( Sender, e)
{
ChangeBoolValue != ChangeBoolValue;
Form1.(StaticBooleanVariable) = ChangeBoolValue;
//checkbox1.Checked = (StaticBooleanVariable);
}
[/html]

form1 (to fix the shit with update or add this to button click to patch it per click...but it will return to upper given answers)

[html]
public void timer1_Tick( Sender, e)
{
checkbox1.Checked = (StaticBooleanVariable);
}
[/html]
public static bool sbChecked;

It really depends on the control you are trying to access.. IMO just either make the control itself static or the form..
i assume that the parent setting has been changed too. since the form1 mainly is the parent idk if its changeable like above... unless form2 has been made into the parent. :/ ahwell cant test so wont bother

C# and form changings are wierd... they should have implemented it like VB would do it. (seems that C# are moar advanced coders then those at vb.)
*flame, i know...its just a strange language with DIM ect. *
Quote Originally Posted by Kuro Tenshi View Post

so u can do it like this? *i assume dont have C# installed yet*

form2:
[html]
Bool ChangeBoolValue = false

public void Button1_Click( Sender, e)
{
ChangeBoolValue != ChangeBoolValue;
Form1.(StaticBooleanVariable) = ChangeBoolValue;
//checkbox1.Checked = (StaticBooleanVariable);
}
[/html]

form1 (to fix the shit with update or add this to button click to patch it per click...but it will return to upper given answers)

[html]
public void timer1_Tick( Sender, e)
{
checkbox1.Checked = (StaticBooleanVariable);
}
[/html]
Yeah exactly like that only your variable would have to have "static" in front of it, or you can start a new thread, turn off cross thread exception thing... (forgot... I <3 intellisense) and then put it in a infinite loop with a sleep of course.

Quote Originally Posted by Kuro Tenshi View Post
i assume that the parent setting has been changed too. since the form1 mainly is the parent idk if its changeable like above... unless form2 has been made into the parent. :/ ahwell cant test so wont bother

C# and form changings are wierd... they should have implemented it like VB would do it. (seems that C# are moar advanced coders then those at vb.)
*flame, i know...its just a strange language with DIM ect. *
I used to code VB before C#, I prefer the C# method, makes more logical sense. Though how is the form changing thing weird? I believe it is the same in VB.net
Posts 1–12 of 12 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?