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

C#

 
Questioncombining 2 codes Pin
djsproject13-Jan-10 1:35
djsproject13-Jan-10 1:35 
AnswerRe: combining 2 codes Pin
0x3c013-Jan-10 2:25
0x3c013-Jan-10 2:25 
GeneralRe: combining 2 codes Pin
djsproject16-Jan-10 8:09
djsproject16-Jan-10 8:09 
QuestionSet the parent textbox value from child form Pin
pavanig912-Jan-10 23:37
pavanig912-Jan-10 23:37 
AnswerRe: Set the parent textbox value from child form Pin
Harvey Saayman12-Jan-10 23:42
Harvey Saayman12-Jan-10 23:42 
GeneralRe: Set the parent textbox value from child form Pin
pavanig913-Jan-10 0:52
pavanig913-Jan-10 0:52 
GeneralRe: Set the parent textbox value from child form Pin
Harvey Saayman13-Jan-10 0:55
Harvey Saayman13-Jan-10 0:55 
AnswerRe: Set the parent textbox value from child form Pin
DaveyM6913-Jan-10 0:22
professionalDaveyM6913-Jan-10 0:22 
This is the purpose of events, the same method that is used to communicate between all controls and their parents. Harvey has already linked to his article - there are many around including mine here[^].

This is a ready made demo app that shows child to parent communication passing data in the event args.
// FormParent.cs
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ChildToParentDemo
{
    public partial class FormParent : Form
    {
        private Button buttonShowChild;

        public FormParent()
        {
            InitializeComponent();
            buttonShowChild = new Button();
            buttonShowChild.Text = "&Show Child";
            buttonShowChild.Location = new Point(12, 12);
            Controls.Add(buttonShowChild);
            AcceptButton = buttonShowChild;
            buttonShowChild.Click += buttonShowChild_Click;
        }

        private void buttonShowChild_Click(object sender, EventArgs e)
        {
            FormChild formChild = new FormChild();
            formChild.StringData += formChild_StringData;
            formChild.ShowDialog(this);
            formChild.Dispose();
        }

        void formChild_StringData(object sender, StringDataEventArgs e)
        {
            // Got data in e here!
            MessageBox.Show(e.StringData);
        }
    }
}
// FormChild.cs
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ChildToParentDemo
{
    public partial class FormChild : Form
    {
        public event EventHandler<StringDataEventArgs> StringData;

        private Button buttonBroadcastData;
        private TextBox textBoxStringData;

        public FormChild()
        {
            InitializeComponent();
            textBoxStringData = new TextBox();
            textBoxStringData.Location = new Point(12, 12);
            buttonBroadcastData = new Button();
            buttonBroadcastData.Text = "&Broadcast Data";
            buttonBroadcastData.Location = new Point(124, 12);
            Controls.AddRange(new Control[] 
                { textBoxStringData, buttonBroadcastData });
            AcceptButton = buttonBroadcastData;
            buttonBroadcastData.Click += buttonBroadcastData_Click;
        }

        private void buttonBroadcastData_Click(object sender, EventArgs e)
        {
            OnStringData(new StringDataEventArgs(textBoxStringData.Text));
        }

        protected virtual void OnStringData(StringDataEventArgs e)
        {
            EventHandler<StringDataEventArgs> eh = StringData;
            if (eh != null)
                eh(this, e);
        }
    }
}
// StringDataEventArgs.cs
using System;

namespace ChildToParentDemo
{
    public class StringDataEventArgs : EventArgs
    {
        private string stringData;

        public StringDataEventArgs(string stringData)
        {
            this.stringData = stringData;
        }

        public string StringData
        {
            get { return stringData; }
        }
    }
}


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)

QuestionDataGridView: SelectRow if EditMode=EditOnEnter Pin
Nigel Mackay12-Jan-10 23:33
Nigel Mackay12-Jan-10 23:33 
AnswerRe: DataGridView: SelectRow if EditMode=EditOnEnter Pin
Herman<T>.Instance13-Jan-10 7:45
Herman<T>.Instance13-Jan-10 7:45 
GeneralRe: DataGridView: SelectRow if EditMode=EditOnEnter Pin
Nigel Mackay13-Jan-10 18:25
Nigel Mackay13-Jan-10 18:25 
QuestionVirtualMode DataGridView - user presses Esc while editing Pin
Nigel Mackay12-Jan-10 23:31
Nigel Mackay12-Jan-10 23:31 
Questionpdf to text Pin
prem212-Jan-10 23:13
prem212-Jan-10 23:13 
AnswerRe: pdf to text Pin
SeMartens12-Jan-10 23:15
SeMartens12-Jan-10 23:15 
QuestionHow To directly Print Using Microsoft.Reporting.WinForms.ReportViewer Pin
Thaer Hamael12-Jan-10 22:23
Thaer Hamael12-Jan-10 22:23 
AnswerRe: How To directly Print Using Microsoft.Reporting.WinForms.ReportViewer Pin
Eddy Vluggen13-Jan-10 1:12
professionalEddy Vluggen13-Jan-10 1:12 
GeneralRe: How To directly Print Using Microsoft.Reporting.WinForms.ReportViewer Pin
Thaer Hamael13-Jan-10 1:38
Thaer Hamael13-Jan-10 1:38 
GeneralRe: How To directly Print Using Microsoft.Reporting.WinForms.ReportViewer Pin
Eddy Vluggen13-Jan-10 2:16
professionalEddy Vluggen13-Jan-10 2:16 
QuestionSystem.Runtime.InteropServices.COMException (0x80040154) Pin
Priya Prk12-Jan-10 20:48
Priya Prk12-Jan-10 20:48 
AnswerRe: System.Runtime.InteropServices.COMException (0x80040154) Pin
OriginalGriff12-Jan-10 23:09
mveOriginalGriff12-Jan-10 23:09 
AnswerRe: System.Runtime.InteropServices.COMException (0x80040154) Pin
Priya Prk13-Jan-10 8:39
Priya Prk13-Jan-10 8:39 
GeneralRe: System.Runtime.InteropServices.COMException (0x80040154) Pin
OriginalGriff14-Jan-10 1:49
mveOriginalGriff14-Jan-10 1:49 
GeneralRe: System.Runtime.InteropServices.COMException (0x80040154) Pin
Priya Prk14-Jan-10 21:34
Priya Prk14-Jan-10 21:34 
QuestionShowing Multiple records Pin
sjs4u12-Jan-10 18:53
sjs4u12-Jan-10 18:53 
AnswerRe: Showing Multiple records Pin
carlecomm13-Jan-10 19:21
carlecomm13-Jan-10 19:21 

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.