Click here to Skip to main content
15,902,636 members
Home / Discussions / C#
   

C#

 
GeneralRe: An exception thrown that I can't figure out how to fix Pin
Luc Pattyn28-Jan-10 14:32
sitebuilderLuc Pattyn28-Jan-10 14:32 
GeneralRe: An exception thrown that I can't figure out how to fix Pin
AspDotNetDev28-Jan-10 20:43
protectorAspDotNetDev28-Jan-10 20:43 
GeneralRe: An exception thrown that I can't figure out how to fix Pin
Luc Pattyn28-Jan-10 22:05
sitebuilderLuc Pattyn28-Jan-10 22:05 
GeneralRe: An exception thrown that I can't figure out how to fix Pin
DaveyM6928-Jan-10 15:14
professionalDaveyM6928-Jan-10 15:14 
GeneralRe: An exception thrown that I can't figure out how to fix Pin
Darrall29-Jan-10 4:52
Darrall29-Jan-10 4:52 
GeneralRe: An exception thrown that I can't figure out how to fix Pin
DaveyM6929-Jan-10 6:03
professionalDaveyM6929-Jan-10 6:03 
GeneralRe: An exception thrown that I can't figure out how to fix Pin
Darrall29-Jan-10 7:59
Darrall29-Jan-10 7:59 
GeneralRe: An exception thrown that I can't figure out how to fix Pin
DaveyM6929-Jan-10 13:03
professionalDaveyM6929-Jan-10 13:03 
OK, this does the same as the tutorial but it does it correctly. I have included an example of both Parent to Child (Parent reads a property in the child) and Child to Parent (Parent listens for event that the Child raises).

For a beginners tutorial to events, have a look at my Events Made Simple article[^].

Anyway, here's the code - any questions, just ask! I've included all the control creation and initialization so you can copy and paste and it should work without having to add any controls in the designer.
C#
// Case.cs
public enum Case
{
    None,
    Upper,
    Lower,
    Proper
}
C#
// FormChild.cs
using System;
using System.Drawing;
using System.Windows.Forms;

public partial class FormChild : Form
{
    #region Events

    public event EventHandler SelectedCaseChanged;

    #endregion

    #region Fields

    private Case selectedCase;

    /* Would normally be in the Designer.cs file */
    private RadioButton radioUpper;
    private RadioButton radioLower;
    private RadioButton radioProper;
    private Button buttonOK;
    private Button buttonCancel;

    #endregion

    #region Constructors

    public FormChild()
    {
        InitializeComponent();
        CustomInitialize();

        selectedCase = Case.None;
        radioUpper.Tag = Case.Upper;
        radioLower.Tag = Case.Lower;
        radioProper.Tag = Case.Proper;
    }

    #endregion

    #region Properties

    /* Public read only public property */
    public Case SelectedCase
    {
        get { return selectedCase; }
        private set
        {
            if (selectedCase != value)
            {
                selectedCase = value;
                OnSelectedCaseChanged(EventArgs.Empty);
            }
        }
    }

    #endregion

    #region Methods

    private void RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton selectedRadio = sender as RadioButton;
        if (sender != null)
            SelectedCase = (Case)selectedRadio.Tag;
    }

    protected virtual void OnSelectedCaseChanged(EventArgs e)
    {
        EventHandler eh = SelectedCaseChanged;
        if (eh != null)
            eh(this, e);
    }

    /* Creates all controls and sets properties
     * Would normally be in the Deisgner.cs file */
    private void CustomInitialize()
    {

        radioUpper = new RadioButton();
        radioLower = new RadioButton();
        radioProper = new RadioButton();
        buttonOK = new Button();
        buttonCancel = new Button();

        Text = "Child";
        Size = new Size(196, 152);
        FormBorderStyle = FormBorderStyle.FixedDialog;
        MaximizeBox = false;
        MinimizeBox = false;
        StartPosition = FormStartPosition.CenterParent;

        radioUpper.Text = "&Upper";
        radioLower.Text = "&Lower";
        radioProper.Text = "&Proper";
        radioUpper.Location = new Point(12, 12);
        radioLower.Location = new Point(12, 35);
        radioProper.Location = new Point(12, 58);

        buttonOK.Text = "OK";
        buttonCancel.Text = "Cancel";
        buttonOK.Location = new Point(93, 81);
        buttonCancel.Location = new Point(12, 81);

        Controls.AddRange(new Control[]{
                radioUpper,
                radioLower,
                radioProper,
                buttonOK,
                buttonCancel });

        CancelButton = buttonCancel;
        AcceptButton = buttonOK;
        buttonOK.DialogResult = DialogResult.OK;

        radioUpper.CheckedChanged += RadioButton_CheckedChanged;
        radioLower.CheckedChanged += RadioButton_CheckedChanged;
        radioProper.CheckedChanged += RadioButton_CheckedChanged;
    }

    #endregion
}
C#
// FormParent.cs
using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

public partial class FormParent : Form
{
    #region Fields

    FormChild formChild;

    /* Would normally be in the Designer.cs file */
    private TextBox textBox;
    private Button buttonSetCase;

    #endregion

    #region Constructors

    public FormParent()
    {
        InitializeComponent();
        CustomInitialize();
    }

    #endregion

    #region Methods

    private void buttonSetCase_Click(object sender, EventArgs e)
    {
        /* Normally you would use either read the property or
         * subscribe to the event, NOT both! */

        formChild = new FormChild();
        // Subscribe to the event in the child form.
        formChild.SelectedCaseChanged += new EventHandler(formChild_SelectedCaseChanged);
        if (formChild.ShowDialog(this) == DialogResult.OK)
            // Read property before disposing of the form.
            CaseText(formChild.SelectedCase);
        formChild.Dispose();
        formChild = null;
    }

    private void formChild_SelectedCaseChanged(object sender, EventArgs e)
    {
        FormChild childForm = sender as FormChild;
        if (childForm != null)
            CaseText(childForm.SelectedCase);
    }

    private void CaseText(Case selectedCase)
    {
        TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
        switch (selectedCase)
        {
            case Case.Upper:
                textBox.Text = textInfo.ToUpper(textBox.Text);
                break;
            case Case.Lower:
                textBox.Text = textInfo.ToLower(textBox.Text);
                break;
            case Case.Proper:
                textBox.Text = textInfo.ToTitleCase(textBox.Text);
                break;
        }
    }

    /* Creates all controls and sets properties
     * Would normally be in the Deisgner.cs file */
    private void CustomInitialize()
    {
        textBox = new TextBox();
        buttonSetCase = new Button();

        Text = "Parent";
        Size = new Size(221, 83);

        textBox.Location = new Point(12, 14);
        buttonSetCase.Location = new Point(118, 12);
        buttonSetCase.Text = "&Set Case";

        Controls.AddRange(new Control[] {
            textBox,
            buttonSetCase });

        AcceptButton = buttonSetCase;
        buttonSetCase.Click += buttonSetCase_Click;
    }

    #endregion
}


Dave

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)

GeneralRe: An exception thrown that I can't figure out how to fix Pin
Darrall29-Jan-10 15:44
Darrall29-Jan-10 15:44 
GeneralRe: An exception thrown that I can't figure out how to fix Pin
DaveyM6930-Jan-10 1:41
professionalDaveyM6930-Jan-10 1:41 
GeneralRe: An exception thrown that I can't figure out how to fix Pin
Darrall30-Jan-10 7:12
Darrall30-Jan-10 7:12 
AnswerRe: An exception thrown that I can't figure out how to fix Pin
Dan Mos28-Jan-10 14:31
Dan Mos28-Jan-10 14:31 
QuestionIterating through MatchCollection Pin
uglyeyes28-Jan-10 12:54
uglyeyes28-Jan-10 12:54 
AnswerRe: Iterating through MatchCollection Pin
Luc Pattyn28-Jan-10 15:12
sitebuilderLuc Pattyn28-Jan-10 15:12 
QuestionHow to get cookies from CookieContainer? Pin
hello_amigo28-Jan-10 10:54
hello_amigo28-Jan-10 10:54 
AnswerRe: How to get cookies from CookieContainer? Pin
Luc Pattyn28-Jan-10 12:02
sitebuilderLuc Pattyn28-Jan-10 12:02 
AnswerRe: How to get cookies from CookieContainer? Pin
Mycroft Holmes28-Jan-10 12:03
professionalMycroft Holmes28-Jan-10 12:03 
GeneralRe: How to get cookies from CookieContainer? Pin
hello_amigo28-Jan-10 13:32
hello_amigo28-Jan-10 13:32 
AnswerRe: How to get cookies from CookieContainer? Pin
Not Active28-Jan-10 13:37
mentorNot Active28-Jan-10 13:37 
AnswerRe: How to get cookies from CookieContainer? Pin
hello_amigo29-Jan-10 5:43
hello_amigo29-Jan-10 5:43 
GeneralRe: How to get cookies from CookieContainer? Pin
Lasha874-Mar-10 1:37
Lasha874-Mar-10 1:37 
QuestionUsing TaskScheduler 2.0 Pin
Bob Nona28-Jan-10 9:23
Bob Nona28-Jan-10 9:23 
AnswerRe: Using TaskScheduler 2.0 Pin
OriginalGriff28-Jan-10 10:24
mveOriginalGriff28-Jan-10 10:24 
GeneralConverting a project to target x64 platform Pin
SimulationofSai28-Jan-10 8:33
SimulationofSai28-Jan-10 8:33 
GeneralRe: Converting a project to target x64 platform [modified] Pin
harold aptroot28-Jan-10 9:07
harold aptroot28-Jan-10 9:07 

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.