Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 2nd, 2008, 4:42 PM   #1
Jewel
Programmer
 
Join Date: Feb 2008
Posts: 32
Rep Power: 0 Jewel is on a distinguished road
Form1 <- Label <- Form2

It was very easy to do in VB.Net but with C# it's alot harder.. Here's some code -->

Got 2 forms. On form1 there's Label 1. And when i press button1 which is in Form2 i want that Form1 Label1 text changes..

VB.Net ( Just a fast code ) =

Button1_Click
            form1.label1.text = Easy
end sub

So.. How to do this on C#

Thanks,
Jewel.
__________________
C#
Jewel is offline   Reply With Quote
Old Jun 2nd, 2008, 6:41 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: Form1 <- Label <- Form2

Try:
c# Syntax (Toggle Plain Text)
  1. Application.OpenForms["Form1"].Text = "Easy";
OpenLoop is offline   Reply With Quote
Old Jun 3rd, 2008, 3:38 AM   #3
Jewel
Programmer
 
Join Date: Feb 2008
Posts: 32
Rep Power: 0 Jewel is on a distinguished road
Re: Form1 <- Label <- Form2

That was not exactly what i was looking for..

I want to control the Label1(which is on Form1) from From2.

I tried to google my problem. Found this
It is like my problem. I tried that
   1.
      Form2 form2 = new Form2();
   2.
      fom2.ShowDialog();//enforces user to not back to form1 unless finishing work on form2
And then
string str = form2.textbox2.Text;

Someone can help :o?
__________________
C#

Last edited by Jewel; Jun 3rd, 2008 at 4:01 AM.
Jewel is offline   Reply With Quote
Old Jun 3rd, 2008, 6:38 AM   #4
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: Form1 <- Label <- Form2

Can we see some code? How are you loading form2?
OpenLoop is offline   Reply With Quote
Old Jun 3rd, 2008, 8:07 AM   #5
Jewel
Programmer
 
Join Date: Feb 2008
Posts: 32
Rep Power: 0 Jewel is on a distinguished road
Re: Form1 <- Label <- Form2

Something like this

Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form form2 = new Form2();
            form2.Show();

        }

        private void label1_Click(object sender, EventArgs e)
        {
            
        }
    }
}

Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestProject
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Yea.. When i press this button Testbox1.Text goes to Label1 which is on Form1
            TestProject.Form1.label1.Text = textBox1.Text;

        }
    }
}
__________________
C#
Jewel is offline   Reply With Quote
Old Jun 3rd, 2008, 11:05 AM   #6
Jewel
Programmer
 
Join Date: Feb 2008
Posts: 32
Rep Power: 0 Jewel is on a distinguished road
Re: Form1 <- Label <- Form2

Here's the Project -->

If the Checkbox is checked in Options menu then Show the PictureBox if not Checked
then do not show.
Attached Files
File Type: zip Test.zip (146.1 KB, 1 views)
__________________
C#
Jewel is offline   Reply With Quote
Old Jun 3rd, 2008, 6:05 PM   #7
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: Form1 <- Label <- Form2

You can do this in several ways. You can make the checkbox in Option public, then when the option button is clicked, create a new Option forum and do ShowDialog, after it returns check the status of the checkbox and hide/show the image.

Take a look at the attached. I opted for a public boolean flag.
Attached Files
File Type: zip Test.zip (897.4 KB, 6 views)
OpenLoop is offline   Reply With Quote
Old Jun 4th, 2008, 9:23 AM   #8
Jewel
Programmer
 
Join Date: Feb 2008
Posts: 32
Rep Power: 0 Jewel is on a distinguished road
Re: Form1 <- Label <- Form2

Thanks! modifed it little to fit my code. Problem Solved
__________________
C#
Jewel is offline   Reply With Quote
Old Jun 9th, 2008, 6:49 AM   #9
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 194
Rep Power: 1 BstrucT is on a distinguished road
Re: Form1 <- Label <- Form2

I would just like to add that there is quite a few ways of doing this in C#.
I mean, accessing let's say a text value between forms can be tricky
for the newb out there. So I have included some easy examples for all
the newcomers who might have read this post and need help with the
basics.

Here is 2 easy ways you can try:

Example: You have two forms, form1 and form2.
On form1 create 1 x textbox(textBox1) and 1 x button(button1).
On form2 create 1 x label(label1).

1st Way: Constructors.

in Form2's class code the following constructor...
public Form2(string myStringtextBox)
{
      InitializeComponent();
      label1.Text = myStringtextBox;
}

Okay, now go to Form1's button click event for button1...

private void button1_Click(object sender, System.EventArgs e)
{
     Form2 frm = new Form2( textBox1.Text );
     frm.Show();
}

2nd Way: Properties
Add a property in Form1 to collect value from the text box...
public string _textBox
{
     get{ return textBox1.Text; }
}

Now add a property in Form2 to set the label's text...
public string _textBox
{
     set{ label1.Text = value; }
}

Once you are comfortable with these, you can try to do the same, but with the delegates approach.

Enjoy.
>BstrucT
__________________
Be kinder than necessary because everyone you meet is fighting some kind of battle.
BstrucT is offline   Reply With Quote
Old Jun 9th, 2008, 10:52 AM   #10
ghg5
Newbie
 
Join Date: Apr 2008
Location: flagstaff az
Posts: 18
Rep Power: 0 ghg5 is on a distinguished road
Re: Form1 <- Label <- Form2

I highly suggest using the second method which bstruct listed. The first is ok except you are initializing the second form each time...if you already have a 2nd form there is no need for this. Setting the property blocks like he did in the second set of code is a good way to go.
ghg5 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Outputting command data to a label? PaCkEtPiRaTe Visual Basic 2 Sep 6th, 2007 5:20 PM
how do you put a caption into a label when clicked? 13EN Visual Basic 2 Jun 8th, 2006 3:26 PM
MVS 2005 Label resizing Kilo C# 4 May 28th, 2006 1:08 AM
Problem moving Label value to TextBox Bman C# 3 Nov 29th, 2005 6:28 PM
how to move a vb label across the screen and then back again adudley Visual Basic 10 Nov 28th, 2005 7:29 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC