Autor Beitrag
vreden123
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95
Erhaltene Danke: 2



BeitragVerfasst: Fr 18.03.11 00:24 
Hallo,

ich möchte einen String von der MainForm zur Subform senden.
Umgekehrt habe ich das bereits hinbekommen.
Die SubForm ist als UserControll in der MainForm eingebunden.
Sonmit ist kein Objektverweiß erforderlich.

Der Code sieht so aus:
MainForm:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace kommunikation2formen
{
    using Controls;
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           personUserControll1.UpdateText += UpdateLabelText;
            
        }

        void UpdateLabelText(object sender, PersonUserControll.TextEventArgs e)
        {
            //statusLabel.Text = e.Text;
            MessageBox.Show(e.Text);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}




SubForm:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace kommunikation2formen.Controls
{
    public partial class PersonUserControll : UserControl
    {
        public PersonUserControll()
        {
            InitializeComponent();
        }

        public class TextEventArgs : EventArgs
        {
            public TextEventArgs(string text)
            {
                Text = text;
            }

            public string Text { get; set; }
        }

        public event EventHandler<TextEventArgs> UpdateText;

        protected void OnUpdateText(string text)
        {
            OnUpdateText(new TextEventArgs(text));
        }

        protected virtual void OnUpdateText(TextEventArgs e)
        {
            EventHandler<TextEventArgs> ev = UpdateText;
            if (ev != null)
                ev(this, e);
        }

        private void PersonUserControll_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OnUpdateText("dsfdsfdfds");
        }
    }
}


Wenn in der Subform auf dem Button geklickt wird, erscheint von der MainForm eine MessageBox.
Ich möchte dies gerne umgekehrt haben. Also sprich in der MainForm wird auf einen Button geklickt und die SubForm arbeitet mit den Daten.(bsp.: MessageBox)

Wenn ich den "Sende" und "Empfangscode" vertausche ist ein Objektverweiß erforderlich, weil die MainForm ja nicht als UserControll in der SubForm eingebunden ist.
Wie kann ich am besten einen String von der MainForm zu der SubForm bekommen?
vreden123 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95
Erhaltene Danke: 2



BeitragVerfasst: Mi 23.03.11 18:02 
Ich muss noch dazu sagen das die Subform bereits geöffnet ist.
Also es muss ein String zur Subform gesendet werden und die soll das dann als Message.Box ausgeben werden.
flyhigh83
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 67

Win 7
C# (VS2010)
BeitragVerfasst: Mo 04.04.11 15:57 
Hi,

ich habe das Problem verstanden aber was mir noch fehlt ist. Dein Parent Form kennt das SubForm nicht, weil es von ihm aus nicht geöffnet wurde?
Sonst könntest du ja beim öffnen einfach in den Construktor den String mitübergeben.
bakachan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 503
Erhaltene Danke: 34

W7 (x64) Ultimate
C# / VB.NET (VS2010 Ultimate)
BeitragVerfasst: Mo 04.04.11 16:03 
Wie wäre es mit einer public-Funktion an deinem SubForm der als Parameter ein String übergeben wird?
Die MainForm hat doch das SubForm als personUserControll1 oder ist das irgendein Container?
flyhigh83
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 67

Win 7
C# (VS2010)
BeitragVerfasst: Mo 15.08.11 17:51 
ahrg.