|
I have tired as you said but it didnt work.could you write what you said?
|
|
|
|
|
messages wrote: but it didnt work. Well that doesn't tell us anything about what you tried or what errors you got.
messages wrote: could you write what you said? I could, but you will learn much more by trying it yourself. This is really not a difficult issue. All you are doing is adding a reference in each form to the other - approximately two or three lines of code.
Veni, vidi, abiit domum
|
|
|
|
|
Its not possible I cant add two references in each form,compiler doesnt complie my code,I have tired.
could you help richard please?
|
|
|
|
|
messages wrote: Its not possible Of course it's possible, you even have the code provided by OriginalGriff. If you really cannot understand what he has written for you then you may need to step back and work on something simpler.
Veni, vidi, abiit domum
|
|
|
|
|
That doesn't do what you want at all - and it is a very poor bit of design.
All that happens is that you get an increasing number of "hidden" forms building up behind the scenes until you run out of memory...
Instead, think of one form (Form1) as the Parent, and the other (Form2) as the child.
In the Form1 button press:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2().Text = textBoxform1.Text;
Hide();
form2.ShowDialog();
Text = form2.NewValue;
Show();
} This means that Form1 will hide itself, display it's child Form2, and redisplay itself when Form2 closes.
Then, add a property to Form2, and change the button handler:
public string NewValue { get { return textBoxform2.Text; } }
private void button1_Click(object sender, EventArgs e)
{
Close();
}
Form2 then closes itself when you press the button, and allows Form1 to get the new value for itself. That way, Form2 doesn't need to know that Form1 even exists, and the interconnections between the forms is reduced to the minimum.
|
|
|
|
|
Thank for your reply OriginalGriff,but a question we didnt write any thing in the form2.in the form2 we have defined a property and insert close() method.but in the form1 we get the text from textbox and set the title of the form.
is it possible to show two forms in the same time?
|
|
|
|
|
|
OriginalGriff your examples are my problem,show two forms and exchange the text,but you are solved a half of problem,is it possible to write a full code it?
|
|
|
|
|
I could - it's not exactly difficult and you have all the code you need - but how much would you learn if I did?
Seriously, it isn't difficult. You know what you need to do:
Add the event code to Form2.
Create an instance of Form2 in Form1, and attach a handler to the event.
When you press the button, show the Form2 instance and set the text.
When you press the button in Form2, raise the event.
In the Form1 handler, set the Form1 Text to the Form2 property.
The sample even does most of that already!
So which bit is difficult for you?
|
|
|
|
|
event handler is problem,I cant use of it.is it possible without events?
|
|
|
|
|
|
Because I have to write it with properties.(btw can I declare a property of type? but how?)
|
|
|
|
|
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
|
|
|
|