新手请教!!!

xh_hb2005 2006-10-31 09:09:24
请教关于附值,如何把Form1中TextBox值附给Form2中TabControl中的(TabPages属性中)成员Text值!

(谢谢!!)
...全文
94 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xh_hb2005 2006-10-31
  • 打赏
  • 举报
回复
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}

这里的代码该如何写?
xh_hb2005 2006-10-31
  • 打赏
  • 举报
回复
谢谢 !有时间定当一种种研究!
oolongTea 2006-10-31
  • 打赏
  • 举报
回复
ls讲得很全面,mark

给个可运行的程序你
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();
}
}
}
lovvver 2006-10-31
  • 打赏
  • 举报
回复
窗体间传值的问题
给你一个类似的例子,与窗体传值基本一样,也有这几种方法.

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();
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2006-10-31 09:09
社区公告

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