![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2008
Posts: 32
Rep Power: 0
![]() |
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 subSo.. How to do this on C# Thanks, Jewel.
__________________
C# |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Re: Form1 <- Label <- Form2
Try:
c# Syntax (Toggle Plain Text)
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2008
Posts: 32
Rep Power: 0
![]() |
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 form2string str = form2.textbox2.Text; Someone can help :o?
__________________
C# Last edited by Jewel; Jun 3rd, 2008 at 4:01 AM. |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Re: Form1 <- Label <- Form2
Can we see some code? How are you loading form2?
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2008
Posts: 32
Rep Power: 0
![]() |
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# |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Feb 2008
Posts: 32
Rep Power: 0
![]() |
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.
__________________
C# |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Feb 2008
Posts: 32
Rep Power: 0
![]() |
Re: Form1 <- Label <- Form2
Thanks! modifed it little to fit my code. Problem Solved
__________________
C# |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 194
Rep Power: 1
![]() |
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. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Apr 2008
Location: flagstaff az
Posts: 18
Rep Power: 0
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |