社区
C#
帖子详情
如何修改主Form的属性?
sangengyi
2004-07-14 01:58:51
Form1为主Form,在Form2的button1中修改Form1的背景色,代码为:
Form1 f1 = new Form1( );
f1.BackColor = Color.Red;
为什麽没反应?是不是f1与主程序窗口引用的实例不是同一个对象造成的?
代码应该怎麽写?谢谢。
...全文
135
9
打赏
收藏
如何修改主Form的属性?
Form1为主Form,在Form2的button1中修改Form1的背景色,代码为: Form1 f1 = new Form1( ); f1.BackColor = Color.Red; 为什麽没反应?是不是f1与主程序窗口引用的实例不是同一个对象造成的? 代码应该怎麽写?谢谢。
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
9 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
chNET
2004-07-14
打赏
举报
回复
用这个,上面的有垃圾代码:
主Form:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication4
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(112, 96);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
}
}
-------------------------------------------------------
Form2:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication4
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
Form form;
public Form2(Form f)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
form = f;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(112, 136);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
form.BackColor = Color.Red;
}
}
}
北京的雾霾天
2004-07-14
打赏
举报
回复
你怎么能找主窗体的实例呢,找到才能对那个实例操作.技巧?我想可能就是设一些属性值使得你在你的窗体代码里能够访问到主窗体的属性,比如你可以在你的窗体加载的时候把主窗体的实例引用传到你的窗体中供本窗体使用,这样你是可以操作主窗体的.
chNET
2004-07-14
打赏
举报
回复
主Form:
---------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(128, 128);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(360, 104);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(608, 357);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
}
}
-----------------------------------
Form2:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication3
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
Form form;
public Form2(Form f)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
form = f;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 112);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
form.BackColor = Color.Red;
}
}
}
sangengyi
2004-07-14
打赏
举报
回复
To: CMIC
f1.BackColor = Color.Red;
-----------------------------
就是因为不行,并不是MDI.
在子窗口修改主窗口的属性。不知怎麽做?
sangengyi
2004-07-14
打赏
举报
回复
老大,需要什麽技巧?说说。。。
CMIC
2004-07-14
打赏
举报
回复
f1.BackColor = Color.Red;
可以改变颜色,但如果是mdi不行,解决办法:
如何修改MDI窗口的主背景
http://dev.csdn.net/develop/article/22/22701.shtm
gkwww
2004-07-14
打赏
举报
回复
附 -> 除
gkwww
2004-07-14
打赏
举报
回复
不同的实例了.附非用静态窗体实例.
Mycro
2004-07-14
打赏
举报
回复
Form1 f1 = new Form1( );
f1.BackColor = Color.Red;
你的这个Form1 f1 ,跟不就不是主Form了,
你又新New出来一个,两回事了!!
再子Form中改主Form的属性,
需要技巧的,没那么简单。。。
form
标签的action
属性
怎么用?
form
标签action
属性
的用法介绍(附实例)
本篇文章
主
要的介绍了关于HTML中
form
标签action
属性
的用法介绍和实例,还有关于
form
标签的action
属性
的定义和语法介绍,最后徐还有关于
form
标签的action
属性
的作用解释。现在让我们一起来看吧。 打造全网web前端...
form
表单的
属性
form
表单HTML标签、
属性
、
属性
值: (单行文本输入框) (密码输入框) (单选框) (多选框) (提交按钮) (重置按钮) 或者按钮(空按钮) (下拉框选) (多行文本框) (1) 是表单的标签,所有的需要写在...
FORM
表单的几大
属性
FORM
表单的几大
属性
问题引入 在做一个活动支付项目的时候,提交订单(
form
表单)跳转支付页面发现没有反应。经过断点排查发现断点也进了,不报错。但是返回的就是空白页,让人很纠结。 进过反复测试,才发现是...
使用JavaScript
修改
form
的action
属性
进行传参,?后面的参数被覆盖掉
使用JavaScript
修改
form
的action
属性
进行传参,?后面的参数被覆盖掉 使用JavaScript
修改
form
的action
属性
进行传参,?后面的参数被覆盖掉,上网百度说是method要是post。 因为
form
用get方式提交,就是拼接的search...
c#
form
窗体
属性
讲解
form
窗体
属性
开始学习c#,网上搜索有很多
form
窗体
属性
,进行简单整理,以便自己以后学习和查找。 一、
Form
常用
属性
(1)Name
属性
:用来获取或设置窗体的名称。 (2)WindowState
属性
:用来获取或设置窗体的窗口...
C#
111,094
社区成员
642,554
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章