Results 1 to 12 of 12
  1. #1
    XxTylerxX's Avatar
    Join Date
    Jun 2009
    Gender
    male
    Location
    MPGH
    Posts
    1,269
    Reputation
    14
    Thanks
    522

    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
    Respect List:
    Nooby Banana
    Bombsaway
    Luke

  2. #2
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    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.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  3. #3
    Kuro Tenshi's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Location
    Where arth thou be
    Posts
    3,635
    Reputation
    70
    Thanks
    746
    My Mood
    Blah
    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]
    DigiDrawing|+ ( (Elfen Archer) )
    Link:
    https://www.mpgh.net/forum/148-showro...en-archer.html


    @ Anime Section,Otaku/weeabo (orz.) @Graphics Section, Novice DigiArtist


    neuest gift from Yura~Chan:
    https://bakyurayuu.deviantar*****m/#/d372taw
    2nd Place MOTM#9 Theme: CharMods - Combat Arms [No - Thanks] button
    come on you know that don't want to push that ordinary button

  4. #4
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    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.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  5. #5
    ZEROI9's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Posts
    88
    Reputation
    10
    Thanks
    18
    My Mood
    Amazed
    Ehm?
    Like this in VB.Net, for a button on Form2 :
    button2_Click
    if Form1.TextBox1.Text = "Hacker"
    Form3.Show
    Else
    Me.Close

  6. #6
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    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?
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  7. #7
    Kuro Tenshi's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Location
    Where arth thou be
    Posts
    3,635
    Reputation
    70
    Thanks
    746
    My Mood
    Blah
    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.)
    Last edited by Kuro Tenshi; 11-29-2010 at 07:08 AM.
    DigiDrawing|+ ( (Elfen Archer) )
    Link:
    https://www.mpgh.net/forum/148-showro...en-archer.html


    @ Anime Section,Otaku/weeabo (orz.) @Graphics Section, Novice DigiArtist


    neuest gift from Yura~Chan:
    https://bakyurayuu.deviantar*****m/#/d372taw
    2nd Place MOTM#9 Theme: CharMods - Combat Arms [No - Thanks] button
    come on you know that don't want to push that ordinary button

  8. #8
    User1's Avatar
    Join Date
    Jul 2009
    Gender
    female
    Location
    Above the influence
    Posts
    4,065
    Reputation
    61
    Thanks
    4,294,967,295
    My Mood
    Crappy
    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...
    Any donations would help


    Quote Originally Posted by Bombsaway707

    HOLY SHIT ITS USER1
    Quote Originally Posted by Blood

    HOLY SHIT ITS USER1
    Quote Originally Posted by Alby-kun


    HOLY SHIT ITS USER1
    Quote Originally Posted by Ali

    HOLY SHIT ITS USER1
    Quote Originally Posted by CodeDemon
    HOLY SHIT ITS USER1
    Quote Originally Posted by Jussofresh View Post
    HOLY SHIT ITS USER1!
    [21:13] CoderNever: HOLY SHIT ITS USER1!

  9. #9
    Kuro Tenshi's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Location
    Where arth thou be
    Posts
    3,635
    Reputation
    70
    Thanks
    746
    My Mood
    Blah
    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]
    Last edited by Kuro Tenshi; 12-04-2010 at 11:45 AM.
    DigiDrawing|+ ( (Elfen Archer) )
    Link:
    https://www.mpgh.net/forum/148-showro...en-archer.html


    @ Anime Section,Otaku/weeabo (orz.) @Graphics Section, Novice DigiArtist


    neuest gift from Yura~Chan:
    https://bakyurayuu.deviantar*****m/#/d372taw
    2nd Place MOTM#9 Theme: CharMods - Combat Arms [No - Thanks] button
    come on you know that don't want to push that ordinary button

  10. #10
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    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..
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  11. #11
    Kuro Tenshi's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Location
    Where arth thou be
    Posts
    3,635
    Reputation
    70
    Thanks
    746
    My Mood
    Blah
    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. *
    DigiDrawing|+ ( (Elfen Archer) )
    Link:
    https://www.mpgh.net/forum/148-showro...en-archer.html


    @ Anime Section,Otaku/weeabo (orz.) @Graphics Section, Novice DigiArtist


    neuest gift from Yura~Chan:
    https://bakyurayuu.deviantar*****m/#/d372taw
    2nd Place MOTM#9 Theme: CharMods - Combat Arms [No - Thanks] button
    come on you know that don't want to push that ordinary button

  12. #12
    User1's Avatar
    Join Date
    Jul 2009
    Gender
    female
    Location
    Above the influence
    Posts
    4,065
    Reputation
    61
    Thanks
    4,294,967,295
    My Mood
    Crappy
    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
    Any donations would help


    Quote Originally Posted by Bombsaway707

    HOLY SHIT ITS USER1
    Quote Originally Posted by Blood

    HOLY SHIT ITS USER1
    Quote Originally Posted by Alby-kun


    HOLY SHIT ITS USER1
    Quote Originally Posted by Ali

    HOLY SHIT ITS USER1
    Quote Originally Posted by CodeDemon
    HOLY SHIT ITS USER1
    Quote Originally Posted by Jussofresh View Post
    HOLY SHIT ITS USER1!
    [21:13] CoderNever: HOLY SHIT ITS USER1!

Similar Threads

  1. can someone help with my hw...
    By ZeroXD in forum Homework & Learning Section
    Replies: 7
    Last Post: 09-24-2011, 09:14 PM
  2. [Help] Can someone help with a mouse thing
    By KissU in forum C++/C Programming
    Replies: 7
    Last Post: 08-28-2011, 06:23 PM
  3. [Help] CAN SOMEONE HELP WITH INJECTOR ISSUES?
    By 13radP in forum Operation 7 General
    Replies: 5
    Last Post: 12-20-2010, 12:45 AM
  4. Can someone help with nickname?
    By gsingh1 in forum WarRock Discussions
    Replies: 7
    Last Post: 09-15-2010, 10:10 PM
  5. CAN SOMEONE HELP ME WITH WINDOWS API
    By scorpoistak in forum Combat Arms Help
    Replies: 0
    Last Post: 08-01-2010, 03:34 PM