|
You use a property to access the data, but you use an Event to tell the other form when you have new data for it.
Think about it: the logic needs to be that the user types on one form, and presses a button. The text needs to be available via a property (or the other form can't access it) but the other form needs to know when the user has finished typing and pressed the button! So you have a property to get the data, and a event to tell you when to read it.
A property is easy to define:
public string MyProperty
{
get { return myTextBox.Text; }
set { myTextBox.Text = value; }
} Declares a property of type "string" called MyProperty, that Form1 can access:
Form2 f2 = new Form2();
f2.MyProperty = "hello!";
...
|
|
|
|
|
Hi OriginalGriff
I exactly wrote what you said for form2
Form2 f2 = new Form2();
f2.MyProperty = "hello!";
...
but it works very well for form1 but doesnt work for form2.
I dont know why C# doenst understand it.
|
|
|
|
|
Why would you want to write that in Form2?
Form2 has the property. Form1 access the property.
Think of it as a car: Form1 is you, Form2 is a car, the glove box is a property of the car.
You can open the glove box of the car In the same way
Form1 can access the property of Form2
|
|
|
|
|
Unless there is a compelling reason to create new instances of the Forms with every click of the Button(s), then you want to create the two Forms once.
There are many ways you could have the two Forms interact; the method shown here works by giving each Form a valid reference to the other Form.
In the case of Form1, the "Main Form," which creates Form2: by definition, it "knows about" ... has a valid reference to ... Form2.
Look at the Load EventHandler in Form1: notice that it sets the Public Property of Type Form1 in Form2 with a valid reference to Form1.
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 form2 = new Form2();
private void Form1_Load(object sender, EventArgs e)
{
form2.referenceToForm1 = this;
}
private void button1_Click(object sender, EventArgs e)
{
form2.Text = textBox1.Text;
form2.Show();
Hide();
}
} Form 2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form1 referenceToForm1 { get; set; }
private void button1_Click(object sender, EventArgs e)
{
referenceToForm1.Text = textBox1.Text;
referenceToForm1.Show();
Hide();
}
} Note that you could get into an "undefined" state if you did this:
1. clicked the Button on form1 which would show form2, and hide form1
2. then, closed form2: at that point your Application is still running, and form1 is hidden ... so it won't show in the TaskBar, so: you are now running in "outer space," and HAL will not open the pod-bay doors.
Finally, I am not sure why you are implementing this kind of functionality which I find rather strange: perhaps as a learning experiment ?
"What Turing gave us for the first time (and without Turing you just couldn't do any of this) is he gave us a way of thinking about and taking seriously and thinking in a disciplined way about phenomena that have, as I like to say, trillions of moving parts.
Until the late 20th century, nobody knew how to take seriously a machine with a trillion moving parts. It's just mind-boggling." Daniel C. Dennett
|
|
|
|
|
well its very important to know and your code does not work when I click the button on the form2 it shows this error
Object reference not set to an instance of an object
|
|
|
|
|
I am almost certain that you have not wired-up the Load Event of Form1, and that is why the reference to the instance of Form1, 'form1,' in the instance of Form2 is null, and you get this error message.
good luck, Bill
"What Turing gave us for the first time (and without Turing you just couldn't do any of this) is he gave us a way of thinking about and taking seriously and thinking in a disciplined way about phenomena that have, as I like to say, trillions of moving parts.
Until the late 20th century, nobody knew how to take seriously a machine with a trillion moving parts. It's just mind-boggling." Daniel C. Dennett
|
|
|
|
|
You can simply do following steps to get your code works:
- In the definition of each form (Form1.cs and Form2.cs for example), declare a property of type Form and name it as TargetForm or so.
- In the main function in program.cs, you initiate the two forms at once, then keep a reference between them, like:
var form1 = new Form1();
var form2 = new Form2();
form1.TargetForm = form2;
form2.TargetForm = form1;
then hide one of them if necessary.
- Update your event handling function, for example for button1.click in the form1:
private void button1_Click(object sender, EventArgs e)
{
this.TargetForm.Show();
this.TargetForm.Text = textBoxform1.Text;
Hide();
}
|
|
|
|
|
When debugging, I can see the properties of this tasklist object, but I can't figure out how to access the properties programatically. As you can see in the screen capture below, the properties enumerate while debugging, how can I access those values?
Thanks!
http://img833.imageshack.us/img833/7081/g4rm.jpg[^]
|
|
|
|
|
For public properties, you can access them 'programmatically' as usual. For non-public ones, try to use reflection. There are a lot of articles on the internet telling about it.
|
|
|
|
|
If you can't access them directly in the usual way, then they are not intended to be available and the access modifier has been set to private or protected to prevent you getting at them.
While it would be possible (if rather slow) to access them via Reflection it is generally a very poor idea. They are not normally available for a reason, and because they aren't designed to be available there is no guarantee that they will work in the same way in the next version, or even exist at all.
Relying on items you have not been given normal access to is a very poor idea, and will often come back to bite you when you least expect it. I would not recommend it, not at all.
|
|
|
|
|
It is a public class that I got from code project actually.
A New Task Scheduler Class Library for .NET[^]
There is probably an easy way to modify this class code, I'm just not able to do it due to experience. Do you still feel the properties are hidden, or since this is a public class, I could expose them?
|
|
|
|
|
IF the properties are public, you can access them directly (or cast them into appropriate value).
|
|
|
|
|
Would you consider those public, if they are exposed by the IDE?
I'm able to cast this way
IEnumerable<Object> stList = serverTaskList.Cast<Object>();
but still not able to get intellisense to recognize the property names
|
|
|
|
|
The TaskList class only implements IEnumerable , not IEnumerable<T> . As a result, the implicit type of your task variable is Object , and you would need to cast it as a Task before you could access the properties.
The simplest solution is to specify the type name in your foreach loop:
foreach (Task task in serverTaskList)
{
...
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks Richard. You're the man, you nailed it with that simple statement ... ugh I feel so foolish!
How did you know it only implemented IEnumerable and not IEnumerable<t> ?
|
|
|
|
|
turbosupramk3 wrote: How did you know it only implemented IEnumerable and not IEnumerable<T>?
I looked at the code[^] in the article[^] you linked to.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
hello
I don't understand this, my app is perfectly not leaking memory and responsively and Task Manager "Detail" tab shows my app is consuming stead 350MB (I checked, update speed is Normal, so I am not looking at some stale counter) on this machine with 3900MB (i.e. almost 4GB)
Now, how come, when I close my process, the memory usage drops from 70% to 30%!?! Right away! as reported on Task Manager!? A 40% drop on this 3900GB machine is 1560MB - that's four times memory I am using. What am I reading here...?
Thanks in Advance
dev
|
|
|
|
|
First, STOP using Task Manager to tell you how much memory your .NET app is using.
It's telling you how much memory the .NET CLR has RESERVED for your app, not how much it's actually using.
This has been documented countless thousands of times all over the web. I know I've posted the explanation dozens of times here on CP.
.net memory task manager[^]
|
|
|
|
|
|
devvvy wrote: and second? This is not a C# question.
Veni, vidi, abiit domum
|
|
|
|
|
you have just contributed to the generally well being of code project Rich
dev
|
|
|
|
|
Just? That was nearly a week ago.
Veni, vidi, abiit domum
|
|
|
|
|
You already got the "second", I just didn't specify it.
|
|
|
|
|
dude, if google is more useful than you what good are you
dev
|
|
|
|
|
Google is only as useful as the person using (or NOT using) it.
If you can't do research and teach yourself, you're screwed in this business.
|
|
|
|