Hi,
I am new to to this forum and this is my first post.
I am working on a C# project. General outline of the project is:
FORM 1 collects data as entered by the user (text boxes, radio buttons, check boxes, numeric up-down controls etc) .
FORM 1 does some calculations on this data and prepares a text message. I have a class created to do this job and updated Message is with the Class.
FORM2 should take this Data and display it using a Rich text box).
User typically opens FORM 2 as and when it is needed to see the Message. User can kill/minimize FORM 2 . There a button on FORM 1 to show FORM 2.
FORM 1 must update Data on the FORM 2 whenever a new Message is generated provided FORM 2 is either showing or in minimized state.
FORM 2 also needs a method to get the message from Form 1 when Form 2 is instanced.
Now I have two FORMs ready, but question is:
1> How to transfer the message from FORM 1 to FORM 2. Can I access object that holds the Message text from FROM 2?
2> FORM 2 can be opened / minimized / killed. I want FORM 1 to refresh the Rich Text box even if the FORM 2 is in minimized condition.
Any help?
(Sorry for my bad English, it is my Second language)
Regards
Babali
Loading
NarayanPosted Oct 2, 2011, 11:16 PM
That is just a sample to show how to use delegate in your scenario
, it can be handled .
Thanks,
Narayan
Babali PKPosted Sep 26, 2011, 12:45 AM
I tried your example program, it works but only problem I noticed is that , Open Form 2 button when clicked creates a new instance of Form 2 regardless whether Form 2 is already created or not. This mean if one clicks that button repeatedly soon there will be dozens of Form 2.
Anyway, will check the delegate concept more in deep, sounds to be more cleaner method .
Thanks once again.
Regards
Babali PKPosted Sep 26, 2011, 12:23 AM
Sounds interesting, will check this and share my finding with you.
Regards
Babali
Babali PKPosted Sep 26, 2011, 12:22 AM
I have a small doubt here , when I create a FORM 2 but keep it hidden means we are consuming System Resources when it is not needed , is it correct?
While on the same topic, earlier I attempted making FORM 1 as parent and FORM 2 as a child, that worked but only problem was FORM 2 always showed behind some controls on FORM 1 , a picture box, FORM 2 always used to get behind that picture box.I could never get FORM 2 at front, so decided to drop MDI scheme. Do you have any suggestion on this?
Regards
Babali
NarayanPosted Sep 25, 2011, 11:09 PM
You can use delegate to hold a reference to that method used for updating data. Its more cleaner way of doing these kind of things.
Please take a look at the attached sample.
I have two forms form1 and form2
form1 has a combobox and button, click the button 2 to open form2.
When you change combobox item, those text will be changed in form2.
Thanks,
Narayan
Sam HobbsPosted Sep 25, 2011, 10:41 PM
If you want to do it the way you are doing it (using Application.OpenForms) then you can execute Application.OpenForms without casting to FORM2. Then check the return value of Application.OpenForms and if it is null then you know not to use that value. If it is not null then it is safe to assume there is not a problem so you can next cast the return value (from Application.OpenForms) to FORM2.
Babali PKPosted Sep 25, 2011, 9:57 PM
In the mean time I tried something which worked , but I am a bit skeptical about the approach. Please guide me.
What I am doing is:
FORM 2 which displays the Message string, is instanced as:
public string messageIn;
public FORM2(string myMessage)
messageIn = myMessaage; // input string to on FORM public variable
In FORM 2 I have a public method:
public void ShowMessage(messageIn)
I call this ShowMessage in the form_load method:
private void FORM2_Load(object sender, EventArgs e)
{
ShowMessage(messageIn);
}
In FORM 1 , have to call this ShowMessage in FORM 2 so that update message gets displayed on FORM 2.
There are two possibilities:
1> FORM 2 is not yet instanced
If it is not instanced , I don't bother.
2> FORM 2 is already instanced, then I must update it.
So check that part first as:
In FORM1:
bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
if (f.Text == "FORM2")
{
IsOpen = true; // FORM found
break;
}
} // Loop to check next FORM
// If FORM2 is already instance then and then only update
// If FORM2 is not instanced , no need to update
if (IsOpen == true)
{
// FORM2 already instanced , OK to update it
FORM2 f1 = (FORM2)Application.OpenForms["FORM2"];
f1.ShowMessage(message);
}
When user Clicks on the button on FORM1 to view message, I first check first the whether the form is already instance or not.
If it is there already, I call the the method in FORM2, else I instance the FORM2 as:
FORM2 frm2 = new FORM2(message);// message goes as FORM2 constructor input argument
frm2.Show();
I am a bot worried the use of:
FORM2 f1 = (FORM2)Application.OpenForms["FORM2"];
Is it safe (legitimate) or it might pose some potentail problem later?
Regards
Babali
Sam HobbsPosted Sep 25, 2011, 12:05 PM