Click here to Skip to main content
15,888,113 members
Home / Discussions / C#
   

C#

 
GeneralRe: accessing god damn controls on other forms Pin
harold aptroot26-Sep-09 2:19
harold aptroot26-Sep-09 2:19 
AnswerRe: accessing god damn controls on other forms Pin
OriginalGriff26-Sep-09 2:10
mveOriginalGriff26-Sep-09 2:10 
GeneralRe: accessing god damn controls on other forms Pin
teknolog12326-Sep-09 2:20
teknolog12326-Sep-09 2:20 
GeneralRe: accessing god damn controls on other forms Pin
OriginalGriff26-Sep-09 2:23
mveOriginalGriff26-Sep-09 2:23 
AnswerRe: accessing god damn controls on other forms Pin
Alan N26-Sep-09 2:25
Alan N26-Sep-09 2:25 
GeneralRe: accessing god damn controls on other forms Pin
teknolog12326-Sep-09 2:41
teknolog12326-Sep-09 2:41 
GeneralRe: accessing god damn controls on other forms Pin
Alan N26-Sep-09 3:48
Alan N26-Sep-09 3:48 
AnswerRe: accessing god damn controls on other forms Pin
DaveyM6926-Sep-09 6:51
professionalDaveyM6926-Sep-09 6:51 
The solution to this is to raise an event in the control that the form subscribes to. This is how all the 'built in' controls work too. Example:
C#
using System;
using System.Drawing;
using System.Windows.Forms;

public partial class FormMain : Form
{
    // These will probably already be in the .Designer.cs file
    MyControl myControl;
    Button button;

    public FormMain()
    {
        InitializeComponent();
        // Creating instance of control, this will probably be done for you already in InitializeComponent.
        myControl = new MyControl();
        // Event subscription
        myControl.SetTextRequest += myControl_SetTextRequest;
        button = new Button();
        Controls.Add(button);
        button.Click += button_Click;
    }

    void button_Click(object sender, EventArgs e)
    {
        myControl.PerformSetTextRequest("Button");
    }

    void myControl_SetTextRequest(object sender, EventArgs<string> e)
    {
        // Respond to event
        button.Text = e.Value;
    }
}

public class MyControl : Control
{
    public event EventHandler<EventArgs<string>> SetTextRequest;

    protected virtual void OnSetTestRequest(EventArgs<string> e)
    {
        EventHandler<EventArgs<string>> eh = SetTextRequest;
        if (eh != null)
            eh(this, e);
    }

    // This method is for convenience only, event should be raised as required
    public void PerformSetTextRequest(string text)
    {
        OnSetTestRequest(new EventArgs<string>(text));
    }
}

// Custom event args - create your own class that does what you need!
public class EventArgs<T> : EventArgs
{
    public EventArgs(T value)
    {
        Value = value;
    }

    public T Value
    {
        get;
        private set;
    }
}


Dave

Generic BackgroundWorker - My latest article!
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: accessing god damn controls on other forms Pin
teknolog12326-Sep-09 9:55
teknolog12326-Sep-09 9:55 
GeneralRe: accessing god damn controls on other forms Pin
DaveyM6926-Sep-09 11:09
professionalDaveyM6926-Sep-09 11:09 
GeneralRe: accessing god damn controls on other forms Pin
teknolog12326-Sep-09 12:54
teknolog12326-Sep-09 12:54 
GeneralC# Final project Pin
Saba Minhas26-Sep-09 1:19
Saba Minhas26-Sep-09 1:19 
GeneralRe: C# Final project Pin
nagendrathecoder26-Sep-09 1:35
nagendrathecoder26-Sep-09 1:35 
GeneralRe: C# Final project Pin
Saba Minhas26-Sep-09 1:46
Saba Minhas26-Sep-09 1:46 
GeneralRe: C# Final project Pin
Abhijit Jana26-Sep-09 3:03
professionalAbhijit Jana26-Sep-09 3:03 
GeneralRe: C# Final project Pin
Saba Minhas28-Sep-09 8:31
Saba Minhas28-Sep-09 8:31 
GeneralRe: C# Final project Pin
OriginalGriff26-Sep-09 2:17
mveOriginalGriff26-Sep-09 2:17 
GeneralRe: C# Final project Pin
Saba Minhas30-Sep-09 6:46
Saba Minhas30-Sep-09 6:46 
QuestionHosting EXE Applications in a WinForm project Pin
tamir90125-Sep-09 23:55
tamir90125-Sep-09 23:55 
AnswerRe: Hosting EXE Applications in a WinForm project PinPopular
Christian Graus25-Sep-09 23:59
protectorChristian Graus25-Sep-09 23:59 
Questionbuiltin text editor in c# Pin
H.R25-Sep-09 23:40
H.R25-Sep-09 23:40 
AnswerRe: builtin text editor in c# Pin
Christian Graus25-Sep-09 23:55
protectorChristian Graus25-Sep-09 23:55 
QuestionPda and Laptop communication help Pin
hande5425-Sep-09 22:44
hande5425-Sep-09 22:44 
Question[Message Deleted] Pin
amaankhan25-Sep-09 17:21
amaankhan25-Sep-09 17:21 
AnswerRe: help me.... Problem in Updating......... see the code Pin
Christian Graus25-Sep-09 17:26
protectorChristian Graus25-Sep-09 17:26 

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.