C#通过一个窗口的事件改变另一个窗口的背景色

pos老吴 2010-01-17 09:22:29
C#通过一个窗口的事件改变另一个窗口的背景色。
同时打开form1,from2;from2中建button1.点击button1时form1的背景色改变!
学习中请教下
...全文
237 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
pos老吴 2010-01-17
  • 打赏
  • 举报
回复
谢谢了,老了,跟不上躺了,写c谢习惯了,无奈!
绝代坏坏 2010-01-17
  • 打赏
  • 举报
回复
看来楼主真是刚学c# ,看看button按钮在不在form2里,没有的话就仍一个进去,然后双击button添加事件。
pos老吴 2010-01-17
  • 打赏
  • 举报
回复
'begcolor.Form1.Dispose(bool)': no suitable method found to override (CS0115) - C:\Documents and Settings\user\My Documents\SharpDevelop Projects\begcolor\Form1.Designer.cs:22,27
pos老吴 2010-01-17
  • 打赏
  • 举报
回复
The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) (CS0234) - C:\Documents and Settings\user\My Documents\SharpDevelop Projects\begcolor\Form1.cs:8,14运行错误
绝代坏坏 2010-01-17
  • 打赏
  • 举报
回复
我用的是framwork3.5 调试通过。你如果用的是2.0 把 using System.Linq;
去掉即可
pos老吴 2010-01-17
  • 打赏
  • 举报
回复
运行错误
绝代坏坏 2010-01-17
  • 打赏
  • 举报
回复
把事件写到form1里,在form2里定制即可。
pos老吴 2010-01-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 juedaihuaihuai 的回复:]
C# codeusing 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 Windows?-
[/Quote]
如果反过来来呢,在form1中建button改变form2的背景色呢??
绝代坏坏 2010-01-17
  • 打赏
  • 举报
回复

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}

void Form1_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.EventChangeColor += new Form2.ChangeBackColor(f2_EventChangeColor);
f2.Show();
}

void f2_EventChangeColor(Color c)
{
this.BackColor = c;
}


}
}






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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public delegate void ChangeBackColor(Color c);
public event ChangeBackColor EventChangeColor;
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Color newColor = Color.AliceBlue;
EventChangeColor(newColor);
}
}
}

ouc_ajax 2010-01-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
public partial class Form1 : Form
    {
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show(this);
        }
    }

    public partial class Form2 : Form
    {
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm1 = (Form1)this.Owner;

        }
    }

[/Quote]

如梦 无处不在!
Up!
qp7711 2010-01-17
  • 打赏
  • 举报
回复
不懂
yeaicc 2010-01-17
  • 打赏
  • 举报
回复
鄙视2喽,1楼正解。
nimingxin1987 2010-01-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 nixiang12 的回复:]
我是在 form1 里面 show form2
button1 是在form1里面
[/Quote]
Form2 frm2 = new Form2();
你这句话直接就是重新实例一个新的f2了
nimingxin1987 2010-01-17
  • 打赏
  • 举报
回复
请无视2楼,直接看1楼的正确答案就行了
nixiang12 2010-01-17
  • 打赏
  • 举报
回复
我是在 form1 里面 show form2
button1 是在form1里面
nixiang12 2010-01-17
  • 打赏
  • 举报
回复

Form2 frm2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
frm2.BackColor = Color.Red;
}

这样就行了 把实例化在外面
wuyq11 2010-01-17
  • 打赏
  • 举报
回复
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);
}
}

public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;

}
}

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧