在C#中将事件从Winform传输到另一个

时间:2014-11-23 02:13:42

标签: c# winforms events event-handling

如何在一个表单中单击一个Button(以生成颜色)并在另一个表单中更改RichTextBox中的文本颜色?提前致谢。 (新手试图了解C#) 一些代码:

1.WinForm

 public delegate void ColorWindowEvent(Object sender, SecondrWindowEventArgs e);

    public partial class ColorWindow : Form
    {
        public event ColorWindowEvent myEventHandler;

        public ColorWindow ()
        {
            InitializeComponent();
        }

        public void MyEvent(Object sender, ColorWindowEventArgs e)
        {
            string s = "";
            myEventHandler(this, new SecondWindowEventArgs(s)); 
        }

        private void btnRed_Click(object sender, EventArgs e)
        {
            Color c = Color.Red;
            string s = c.ToString();
            this.Close();
        }

        private void btnBlue_Click(object sender, EventArgs e)
        {
            Color c = Color.Blue;
            string str = c.ToString();
            this.Close();

        }

        private void btnGreen_Click(object sender, EventArgs e)
        {
            Color c = Color.Green;
            string s = c.ToString();
            this.Close();
        }
    }
    public class SecondWindowEventArgs : EventArgs
    {
        private string s;
        public SecondWindowEventArgs(string _s)
        {
            s = _s;

        }
        #region
        public string S
        {
            get;
            set;
        }
        #endregion
    }

2.WinForm

public delegate void SecondWindowEvent(Object sender, FirstWindowEventArgs e);

public partial class SecondWindow : Form
{
    public event SecondWindowEvent myEventHandler;
    private string s;

    public SecondWindow(String _s)
    {
        s = _s;
        InitializeComponent();
    }

    public void MyEvent(Object sender, FirstWindowEventArgs e)
    {
        string str = rtf2.Text;
        if (str != null)
        {
            myEventHandler(this, new FirstWindowEventArgs(str));
        }
    }
    private void btnQuit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void rtf2_TextChanged(object sender, EventArgs e)
    {
        if (myEventHandler != null)
        {
         myEventHandler(this, new FirstWindowEventArgs(rtf2.Text.Substring(rtf2.Text.Length - 1)));
         rtf2.ForeColor = Color.FromName(e.ToString());

        }
    }

    private void btnClearText_Click(object sender, EventArgs e)
    {
        rtf2.Text = " ";
    }

    private void rtf2_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Escape)
        {
            FargeVindu fargeVindu = new FargeVindu();
            fargeVindu.minEventHandler += new FargeVinduEvent(fargeVindu_minEventHandler);
            fargeVindu.Show();
        }
        else if (e.KeyData == Keys.Delete)
        {

        }

    }
    protected void ColorWindow_myEventHandler(object sender, SecondWindowEventArgs e)
    {
        rtf2.ForeColor = Color.FromName(s);
    }

    Random random = new Random();
    private void SecondWindow_Load(object sender, EventArgs e)
    {
        lblText.ForeColor = Color.FromArgb(random.Next(255),
    random.Next(255), random.Next(255));
    }
    public Color getColor
    {
        get;
        set;
    }

}

3.WinForm

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

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClick_Click(object sender, EventArgs e)
        {
            string str = " ";
            SecondWindow secondWindow = new SecondWindow (str);

            secondWindow.myEventHandler += new SecondWindowEvent(secondWindow_myEventHandler);
            secondWindow.Show();
        }

        protected void secondWindow_myEventHandler(object sender, FirstWindowEventArgs e)
        {
            rtf1.AppendText(String.Format(e.Tekst));
        }

        public void btnClearText_Click(object sender, EventArgs e)
        {
            rtf1.Text = " ";
        }
    }

评论澄清

我希望单击按钮后,Form关闭时更改颜色。这就是我试过的:

private void btnRed_Click(object sender, EventArgs e) 
{
    Color c = Color.Red; 
    string s = c.ToString(); 
    this.Close(); 
} 

1 个答案:

答案 0 :(得分:1)

要回答您的问题,请在您的颜色表单上创建一个由您的按钮设置的属性,然后您可以在表单从ShowDialog语句返回后关闭该表单。

<强> Form1中

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

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        if(frm2.ShowDialog() == DialogResult.OK)
        {
           //this.BackColor=frm2.getColor;  helps if I read the question more closely
           richTextBox1.SelectionColor = frm2.getColor;
        }
    }
}

<强>窗体2

public partial class Form2 : Form
{
    public Color getColor { get; set; }
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        getColor = Color.Red;
        DialogResult = DialogResult.OK;
    }
}

在使用您发布的代码并添加了一些丢失的处理程序后,看起来您的颜色文本的格式为Color [Red] ColorFromName不知道如何解析它以便您需要获取使用String.Split这样的实际颜色名称。

protected void ColorWindow_myEventHandler(object sender, SecondWindowEventArgs e)
{
    rtf2.ForeColor = Color.FromName(e.S.Split(new string[]{"[","]"},StringSplitOptions.None)[1]);
}

我还注意到,每次文本更改时都会设置rtf2.ForeColor,我删除了它,现在可以更改RichText框的ForeColor。如果您只是通过实际的颜色对象而不是将其更改为字符串并返回,我会更容易/更清洁恕我直言。

这是修改后的TextChanged方法注释,注释掉它不属于那里的rtf2.ForeColor语句。

private void rtf2_TextChanged(object sender, EventArgs e)
{
    if (myEventHandler != null)
    {
     myEventHandler(this, new FirstWindowEventArgs(rtf2.Text.Substring(rtf2.Text.Length -1)));
    // rtf2.ForeColor = Color.FromName(e.ToString());

    }
}