Click here to Skip to main content
15,905,563 members
Home / Discussions / C#
   

C#

 
QuestionSecond form using accessors of first form to alter first form object properties problem Pin
Member 210292422-Feb-09 2:54
Member 210292422-Feb-09 2:54 
AnswerEverything is working Pin
Xmen Real 22-Feb-09 3:07
professional Xmen Real 22-Feb-09 3:07 
GeneralRe: Everything is working Pin
Member 210292422-Feb-09 3:41
Member 210292422-Feb-09 3:41 
GeneralRe: Everything is working Pin
Member 210292422-Feb-09 3:43
Member 210292422-Feb-09 3:43 
GeneralRe: Everything is working Pin
Xmen Real 22-Feb-09 6:56
professional Xmen Real 22-Feb-09 6:56 
GeneralRe: Everything is working Pin
DaveyM6922-Feb-09 9:27
professionalDaveyM6922-Feb-09 9:27 
GeneralRe: Everything is working Pin
Member 210292422-Feb-09 19:11
Member 210292422-Feb-09 19:11 
GeneralRe: Everything is working Pin
DaveyM6923-Feb-09 1:38
professionalDaveyM6923-Feb-09 1:38 
Don't give up on the proper way. It can be done in 1.1. I think the only difference is you can't use the Generic event handler for your delegate so you'll need to create your own delegate.

I don't have 1.1 (I'm using 2008 where the lowest I can target is 2.0). This should work though.
// Form1.cs
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private Button button1;

    private IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        button1 = new Button();
        SuspendLayout();

        button1.Location = new Point(13, 13);
        button1.Text = "Show Form2";
        button1.Click += new EventHandler(this.button1_Click);

        Controls.Add(this.button1);
        ResumeLayout(false);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.UpdateText += new Form2.UpdateTextEventHandler(form2_UpdateText);
        form2.ShowDialog();
        form2.UpdateText -= form2_UpdateText;
        form2.Dispose();
    }

    void form2_UpdateText(string text)
    {
        Text = text;
    }
}
// Form2.cs
using System;
using System.ComponentModel;
using System.Windows.Forms;

public class Form2 : Form
{
    public delegate void UpdateTextEventHandler(string text);
    public event UpdateTextEventHandler UpdateText;

    public Form2()
    {
        InitializeComponent();
    }

    private TextBox textBox1;
    private Button button1;

    private IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        textBox1 = new System.Windows.Forms.TextBox();
        button1 = new System.Windows.Forms.Button();

        SuspendLayout();

        textBox1.Location = new System.Drawing.Point(12, 12);

        button1.Location = new System.Drawing.Point(12, 38);
        button1.Text = "Update";
        button1.Click += new System.EventHandler(this.button1_Click);

        Controls.Add(this.button1);
        Controls.Add(this.textBox1);

        ResumeLayout(false);
        PerformLayout();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OnUpdateText(textBox1.Text);
    }

    protected virtual void OnUpdateText(string text)
    {
        UpdateTextEventHandler eh = UpdateText;
        if (eh != null)
            eh(text);
    }
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

Question[Message Deleted] Pin
Chetan Patel22-Feb-09 1:59
Chetan Patel22-Feb-09 1:59 
AnswerRe: Custom Window Control, The Control does not Repaint on Posision change of SCROLL Pin
Xmen Real 22-Feb-09 2:54
professional Xmen Real 22-Feb-09 2:54 
QuestionHow to add multiple controls at runTime ? Pin
hdv21222-Feb-09 0:52
hdv21222-Feb-09 0:52 
AnswerRe: How to add multiple controls at runTime ? Pin
Member 349379922-Feb-09 1:09
Member 349379922-Feb-09 1:09 
AnswerRe: How to add multiple controls at runTime ? [modified] Pin
Member 349379922-Feb-09 1:16
Member 349379922-Feb-09 1:16 
GeneralRe: How to add multiple controls at runTime ? Pin
hdv21222-Feb-09 1:30
hdv21222-Feb-09 1:30 
GeneralRe: How to add multiple controls at runTime ? Pin
dan!sh 22-Feb-09 1:52
professional dan!sh 22-Feb-09 1:52 
GeneralRe: How to add multiple controls at runTime ? Pin
ABitSmart22-Feb-09 2:09
ABitSmart22-Feb-09 2:09 
AnswerRe: How to add multiple controls at runTime ? Pin
Luc Pattyn22-Feb-09 2:27
sitebuilderLuc Pattyn22-Feb-09 2:27 
QuestionCompile error: Using strings inside Struct with LayoutKind.Explicit Pin
Member 349379921-Feb-09 23:57
Member 349379921-Feb-09 23:57 
AnswerRe: Compile error: Using strings inside Struct with LayoutKind.Explicit Pin
Luc Pattyn22-Feb-09 2:35
sitebuilderLuc Pattyn22-Feb-09 2:35 
GeneralRe: Compile error: Using strings inside Struct with LayoutKind.Explicit Pin
Member 349379922-Feb-09 3:04
Member 349379922-Feb-09 3:04 
GeneralRe: Compile error: Using strings inside Struct with LayoutKind.Explicit Pin
Member 349379922-Feb-09 3:08
Member 349379922-Feb-09 3:08 
QuestionHow do I persist cookies between multiple HttpWebRequest sessions? Pin
god.hacks21-Feb-09 23:20
god.hacks21-Feb-09 23:20 
AnswerCan anybody help me at all? Pin
god.hacks22-Feb-09 2:57
god.hacks22-Feb-09 2:57 
QuestionClose window by itself Pin
dabuskol21-Feb-09 21:20
dabuskol21-Feb-09 21:20 
AnswerRe: Close window by itself Pin
Luis Alonso Ramos21-Feb-09 22:38
Luis Alonso Ramos21-Feb-09 22:38 

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.