Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
AnswerRe: create Web browser in C# without using web browser control Pin
Eddy Vluggen10-Oct-14 9:46
professionalEddy Vluggen10-Oct-14 9:46 
AnswerRe: create Web browser in C# without using web browser control Pin
Pete O'Hanlon10-Oct-14 9:56
mvePete O'Hanlon10-Oct-14 9:56 
Questionhi friends Pin
Member 111430869-Oct-14 22:52
Member 111430869-Oct-14 22:52 
AnswerRe: hi friends Pin
Eddy Vluggen10-Oct-14 7:51
professionalEddy Vluggen10-Oct-14 7:51 
QuestionPlaying media with 2 different channels in stereo file using NAudio library Pin
Praveen Raghuvanshi9-Oct-14 17:55
professionalPraveen Raghuvanshi9-Oct-14 17:55 
AnswerRe: Playing media with 2 different channels in stereo file using NAudio library Pin
Eddy Vluggen10-Oct-14 7:53
professionalEddy Vluggen10-Oct-14 7:53 
QuestionWinform Change Label Text from secondary form action Pin
Derek Kennard9-Oct-14 1:56
professionalDerek Kennard9-Oct-14 1:56 
AnswerRe: Winform Change Label Text from secondary form action Pin
BillWoodruff9-Oct-14 3:00
professionalBillWoodruff9-Oct-14 3:00 
fyi: OriginalGriff has written a series of articles here on CP illustrating different methods of passing data; I think you'll learn a lot reading them: [^].

Your code: you are creating new instances of Forms every time you execute some of your methods, and since there's no reference kept to the Form instances created, as soon as you exit the method code, that Form instance goes into the void.

Do create your Form instances once, keep references to them, and re-use them.

Assuming the Form with the three labels is your main form in a WinForm application, and it creates and shows the second Form, the issue of how to update the Labels on the main form can be addressed in several different ways. I'll illustrate one of them:
C#
// in the main form (Form1)

// create a new instance of the secondary form
private Form2 theForm2 = new Form2();

private void Form1_Load(object sender, EventArgs e)
{
    // assign an anonymous method to the ChangeLabels Action field of the secondary form
    theForm2.ChangeLabels = delegate(string s1, string s2, string s3)
    {
        // update the labels
        label1.Text = s1;
        label2.Text = s2;
        label3.Text = s3;
    };

    // expose the secondary form
    theForm2.Show();
}

// in the secondary form (Form2)

// a template for a method that takes three string parameters and returns void
public Action<string, string, string> ChangeLabels;

// a button in the secondary form that will call the method
// injected into the secondary form by its creator, the main form
private void TestChangeForm1Labels_Click(object sender, EventArgs e)
{
    // test
    ChangeLabels("1", "2", "3");
}
This is really a variation on the "classic way" of handling this kind of asynchronous update of one Form from another by defining a Public Event in the secondary Form which the "consumer" Form can then subscribe to if it has a reference to the secondary form.

I say "variation" because an Action (or a Func) in C# is a delegate, and can be used directly in the same way "classic" EventHandlers ... which have the "object sender, EventArgs e" business are used. In this example we "inject" a pointer to a method in the instance of the main form into the instance of a secondary form: essentially we give the secondary form a call-back to the main form.

I chose to illustrate this method here because: I am bored with writing standard event-handling code in answers here on CP, and felt like doing something different, and, to me, this technique is kind of "leaner and meaner" Smile | :)

But, there are many ways one can implement form-to-form (object-to-object) communication; this is only one of them.
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali


modified 9-Oct-14 17:42pm.

GeneralRe: Winform Change Label Text from secondary form action Pin
Derek Kennard9-Oct-14 15:28
professionalDerek Kennard9-Oct-14 15:28 
QuestionMake Cell Read Only after clicking the Cell in DataGridView Pin
avisharma@sharma9-Oct-14 1:28
avisharma@sharma9-Oct-14 1:28 
AnswerRe: Make Cell Read Only after clicking the Cell in DataGridView Pin
Simon_Whale9-Oct-14 1:36
Simon_Whale9-Oct-14 1:36 
GeneralRe: Make Cell Read Only after clicking the Cell in DataGridView Pin
avisharma@sharma9-Oct-14 2:05
avisharma@sharma9-Oct-14 2:05 
Questionusing "naked" Action/Func as EventHandlers ? C# language question PinPopular
BillWoodruff9-Oct-14 1:07
professionalBillWoodruff9-Oct-14 1:07 
AnswerRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Eddy Vluggen9-Oct-14 9:04
professionalEddy Vluggen9-Oct-14 9:04 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
BillWoodruff9-Oct-14 11:13
professionalBillWoodruff9-Oct-14 11:13 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Eddy Vluggen10-Oct-14 7:38
professionalEddy Vluggen10-Oct-14 7:38 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Richard Deeming9-Oct-14 22:20
mveRichard Deeming9-Oct-14 22:20 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Eddy Vluggen10-Oct-14 7:50
professionalEddy Vluggen10-Oct-14 7:50 
AnswerRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Nicholas Marty10-Oct-14 5:24
professionalNicholas Marty10-Oct-14 5:24 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
BillWoodruff10-Oct-14 11:23
professionalBillWoodruff10-Oct-14 11:23 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Nicholas Marty13-Oct-14 1:13
professionalNicholas Marty13-Oct-14 1:13 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
BillWoodruff13-Oct-14 1:34
professionalBillWoodruff13-Oct-14 1:34 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Nicholas Marty13-Oct-14 2:17
professionalNicholas Marty13-Oct-14 2:17 
QuestionTransaction in active directory Pin
Nitin K8-Oct-14 23:21
Nitin K8-Oct-14 23:21 
AnswerRe: Transaction in active directory Pin
Eddy Vluggen9-Oct-14 0:30
professionalEddy Vluggen9-Oct-14 0:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.