新手请教!!! 请教关于附值,如何把Form1中TextBox值附给Form2中TabControl中的(TabPages属性中)成员Text值!(谢谢!!) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 窗体间传值的问题给你一个类似的例子,与窗体传值基本一样,也有这几种方法.CON-如何在子窗体(被调用者)中实现对其父窗体(调用者)的刷新呢?网络上有几种方法,先总结如下:调用窗体(父):Form1,被调用窗体(子):Form2方法1: 所有权法//Form1://需要有一个公共的刷新方法public void Refresh_Method(){ //...}//在调用Form2时,要把Form2的所有者设为Form1Form2 f2 = new Form2() ;f2.Owner = this;f2.ShowDialog() ;//Form2://在需要对其调用者(父)刷新时Form1 f1 ;f1 = (Form1)this.Owner;f1.Refresh_Method() ;方法2:自身传递法//Form1://需要有一个公共的刷新方法public void Refresh_Method(){ //...}Form2 f2 = new Form2() ;f2.ShowDialog(this) ;//Form2:private Form1 p_f1;public Form2(Form1 f1){ InitializeComponent(); p_f1 = f1;}//刷新时p_f1.Refresh_Method() ;方法3:属性法//Form1://需要有一个公共的刷新方法public void Refresh_Method(){ //...}//调用时Form2 f2 = new Form2() ;f2.P_F1 = this;f2.Show() ;//Form2:private Form1 p_f1;public Form1 P_F1{ get{return p_f1;} set{p_f1 = value;}}//刷新时p_f1.Refresh_Method() ;方法4:委托法//声明一个委托public delegate void DisplayUpdate();//Form1://需要有一个公共的刷新方法public void Refresh_Method(){ //...}//调用时Form2 f2 = new Form2() ;f2.ShowUpdate += new DisplayUpdate(Refresh_Method) ;f2.Show() ;//Form2://声明事件public event DisplayUpdate ShowUpdate;//刷新时,放在需要执行刷新的事件里ShowUpdate(); ls讲得很全面,给个可运行的程序你using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace test{ public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TabControl tabControl1; private System.Windows.Forms.TabPage tabPage1; private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage1 = new System.Windows.Forms.TabPage(); this.button1 = new System.Windows.Forms.Button(); this.tabControl1.SuspendLayout(); this.SuspendLayout(); this.tabControl1.Controls.Add(this.tabPage1); this.tabControl1.Location = new System.Drawing.Point(24, 40); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.TabIndex = 0; this.tabPage1.Location = new System.Drawing.Point(4, 21); this.tabPage1.Name = "tabPage1"; this.tabPage1.Size = new System.Drawing.Size(192, 75); this.tabPage1.TabIndex = 0; this.tabPage1.Text = "tabPage1"; this.button1.Location = new System.Drawing.Point(80, 184); this.button1.Name = "button1"; this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Controls.Add(this.tabControl1); this.Name = "Form1"; this.Text = "Form1"; this.tabControl1.ResumeLayout(false); this.ResumeLayout(false); } static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { Form2 f2 = new Form2(); f2.testClick += new test.Form2.OnTestClick(f2_testClick); f2.ShowDialog(); } private void f2_testClick(string s) { tabPage1.Text = s; } }}using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;namespace test{ public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; public delegate void OnTestClick(string s); public event OnTestClick testClick; private System.ComponentModel.Container components = null; public Form2() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); this.textBox1.Location = new System.Drawing.Point(24, 64); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 0; this.textBox1.Text = "textBox1"; this.button1.Location = new System.Drawing.Point(24, 128); this.button1.Name = "button1"; this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Name = "Form2"; this.Text = "Form2"; this.ResumeLayout(false); } private void button1_Click(object sender, System.EventArgs e) { testClick(textBox1.Text); Dispose(); } }} //Form1://需要有一个公共的刷新方法public void Refresh_Method(){ //...}这里的代码该如何写? 帮一下忙 窗体背景颜色透明的问题? windows服务的Onshutdown方法 如何实现在线直播??谢谢请教! 网站在asp.net2010运行时错误,但是在2008又没有错误 如何重写菜单? C#中关于DataGrid与SQL数据库绑定并显示数据的问题,请赐教! 关于form窗口最小化的问题 Developer.Express.NET无法安装 使用水晶报表推模型的疑问? 一行很简单的代码,我却不懂!请随便指教一下吧! C#中进行平台调用编程问题
给你一个类似的例子,与窗体传值基本一样,也有这几种方法.CON-如何在子窗体(被调用者)中实现对其父窗体(调用者)的刷新呢?
网络上有几种方法,先总结如下:
调用窗体(父):Form1,被调用窗体(子):Form2
方法1: 所有权法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//在调用Form2时,要把Form2的所有者设为Form1
Form2 f2 = new Form2() ;
f2.Owner = this;
f2.ShowDialog() ;
//Form2:
//在需要对其调用者(父)刷新时
Form1 f1 ;
f1 = (Form1)this.Owner;
f1.Refresh_Method() ;
方法2:自身传递法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
Form2 f2 = new Form2() ;
f2.ShowDialog(this) ;
//Form2:
private Form1 p_f1;
public Form2(Form1 f1)
{
InitializeComponent();
p_f1 = f1;
}
//刷新时
p_f1.Refresh_Method() ;
方法3:属性法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//调用时
Form2 f2 = new Form2() ;
f2.P_F1 = this;
f2.Show() ;//Form2:
private Form1 p_f1;
public Form1 P_F1
{
get{return p_f1;}
set{p_f1 = value;}
}
//刷新时
p_f1.Refresh_Method() ;
方法4:委托法
//声明一个委托
public delegate void DisplayUpdate();
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//调用时
Form2 f2 = new Form2() ;
f2.ShowUpdate += new DisplayUpdate(Refresh_Method) ;
f2.Show() ;
//Form2:
//声明事件
public event DisplayUpdate ShowUpdate;
//刷新时,放在需要执行刷新的事件里
ShowUpdate();
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace test
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.button1 = new System.Windows.Forms.Button();
this.tabControl1.SuspendLayout();
this.SuspendLayout();
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Location = new System.Drawing.Point(24, 40);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.TabIndex = 0;
this.tabPage1.Location = new System.Drawing.Point(4, 21);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(192, 75);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.button1.Location = new System.Drawing.Point(80, 184);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.tabControl1.ResumeLayout(false);
this.ResumeLayout(false); }
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
Form2 f2 = new Form2();
f2.testClick += new test.Form2.OnTestClick(f2_testClick);
f2.ShowDialog();
} private void f2_testClick(string s)
{
tabPage1.Text = s;
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace test
{
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
public delegate void OnTestClick(string s);
public event OnTestClick testClick;
private System.ComponentModel.Container components = null; public Form2()
{
InitializeComponent();
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.textBox1.Location = new System.Drawing.Point(24, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.button1.Location = new System.Drawing.Point(24, 128);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
private void button1_Click(object sender, System.EventArgs e)
{
testClick(textBox1.Text);
Dispose();
}
}
}
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}这里的代码该如何写?