Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#] Help with global variables

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
ssaccount
Master Cheater
Reputation: 0

Joined: 29 Aug 2007
Posts: 391
Location: Finland

PostPosted: Sat May 03, 2008 12:24 pm    Post subject: [C#] Help with global variables Reply with quote

I have little problem.

I have form1 and form2. There is label in form1 and label in form2
and in form1 there is button which opens form2.

My problem is that when the form2 opens i want its label get same value as form1's label.

So how am i supposed to do that?

_________________
Programming in C++
(Or at least trying to Very Happy)
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Sat May 03, 2008 1:36 pm    Post subject: Reply with quote

okay so first of all you need to make the label at form1 public
lets say that form1 is "Main"
what you do in form2_load is:
Code:

private void form2_load(object sender, EventArgs e)
{
 Main MainForm = new Main();
 label2.text = MainForm.label1.text;
}


this should work fine

_________________
Stylo
Back to top
View user's profile Send private message
killersamurai
Expert Cheater
Reputation: 0

Joined: 10 Sep 2007
Posts: 197
Location: Colorado

PostPosted: Sat May 03, 2008 1:39 pm    Post subject: Reply with quote

If you view the code for Form2, you should see
Code:

public Form2()
{
InitializeComponent();
}


Change it to
Code:

Form1 frm1;

        public Form2(Form1 temp)
        {
            frm1 = temp;
            InitializeComponent();
        }


Now, you can get the information. But you have to set the label of form1 to public. Or, you can create a public function to return what is on the label.
Code:

public string GetLabel()
        {
            return label1.Text;
        }


This is how you would open up form2
Code:

Form2 frm2 = new Form2(this);
Back to top
View user's profile Send private message
ssaccount
Master Cheater
Reputation: 0

Joined: 29 Aug 2007
Posts: 391
Location: Finland

PostPosted: Sat May 03, 2008 1:43 pm    Post subject: Reply with quote

Thx it's working now Smile
_________________
Programming in C++
(Or at least trying to Very Happy)
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Mon May 05, 2008 8:32 pm    Post subject: Reply with quote

killersamurai wrote:
If you view the code for Form2, you should see
Code:

public Form2()
{
InitializeComponent();
}


Change it to
Code:

Form1 frm1;

        public Form2(Form1 temp)
        {
            frm1 = temp;
            InitializeComponent();
        }


Now, you can get the information. But you have to set the label of form1 to public. Or, you can create a public function to return what is on the label.
Code:

public string GetLabel()
        {
            return label1.Text;
        }


This is how you would open up form2
Code:

Form2 frm2 = new Form2(this);


I agree with your idea of using the constructor, but I'd have done it differently:

Code:

public Form2(string sLabelText)
{
    myLabel.Text = sLabelText;
    InitializeComponent();
}


Then, when creating the form, just do

Code:

      Form2 newForm = new Form2(myLabel.Text);
      newForm.Show();

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites