C# winform 在一个窗体中如何设置另一个窗体的TextBox的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# winform 在一个窗体中如何设置另一个窗体的TextBox的值相关的知识,希望对你有一定的参考价值。
参考技术A 方法有很多,下拉给你列几个:首先介绍最粗暴的方法,修改控件的访问修饰符。(不建议使用此法)
public System.Windows.Forms.TextBox textBox1;
在调用时就能直接访问
Form1 frm = new Form1();
frm.textBox1.Text = "方法1";
frm.Show();
方法2是通过构造函数/指定公开方法传入,然后为对应控件赋值。
public Form2(string text)
InitializeComponent();
this.textBox1.Text = text;
调用时
Form2 frm = new Form2("方法2");
frm.Show();
方法3是通过公开属性来设置,此法甚好。
public string Text3
get return this.textBox1.Text;
set this.textBox1.Text = value;
调用如下
Form3 frm = new Form3();
frm.Text3 = "方法3";
frm.Show();
等等,还有一些其他方法,这不一一介绍了。
winform 屏幕右上角悬浮窗demo
当窗体最小化时,显示右悬浮窗!!!
1新建右上角小窗口
(1)去除边框 (2)最前显示(设置背景图案)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace TopMost 10 { 11 public partial class frmTopMost : Form 12 { 13 private Point ptMouseCurrentPos, ptMouseNewPos, ptFormPos, ptFormNewPos; 14 private bool blnMouseDown = false; 15 private frmMain pParent; 16 //Add ContextMenuStrip . 17 private ContextMenuStrip docMenu; 18 19 public frmTopMost(frmMain pMain) 20 { 21 InitializeComponent(); 22 pParent=pMain; 23 } 24 25 private void frmTopMost_Load(object sender, EventArgs e) 26 { 27 this.FormBorderStyle = FormBorderStyle.None; 28 this.TopMost = true; 29 this.Show(); 30 this.Top = 100; 31 this.Left = Screen.PrimaryScreen.Bounds.Width - 200; 32 this.Width = 35; 33 this.Height = 35; 34 35 docMenu = new ContextMenuStrip(); 36 37 ToolStripMenuItem OpenLable = new ToolStripMenuItem(); 38 OpenLable.Text = "Open"; 39 OpenLable.Click+=new EventHandler(OpenLable_Click); 40 ToolStripMenuItem ExitLable = new ToolStripMenuItem(); 41 ExitLable.Text = "Exit"; 42 ExitLable.Click+=new EventHandler(ExitLable_Click); 43 44 docMenu.Items.AddRange(new ToolStripMenuItem[] { OpenLable,ExitLable}); 45 this.ContextMenuStrip = docMenu; 46 } 47 private void frmTopMost_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 48 { 49 if (blnMouseDown) 50 { 51 //Get the current postion of the mouse in the screen. 52 ptMouseNewPos = Control.MousePosition; 53 54 //Set window postion. 55 ptFormNewPos.X = ptMouseNewPos.X - ptMouseCurrentPos.X + ptFormPos.X; 56 ptFormNewPos.Y = ptMouseNewPos.Y - ptMouseCurrentPos.Y + ptFormPos.Y; 57 58 //Save window postion. 59 Location = ptFormNewPos; 60 ptFormPos = ptFormNewPos; 61 62 //Save mouse pontion. 63 ptMouseCurrentPos = ptMouseNewPos; 64 } 65 } 66 67 private void frmTopMost_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e) 68 { 69 if (e.Button == MouseButtons.Left) 70 { 71 blnMouseDown = true; 72 73 //Save window postion and mouse postion. 74 ptMouseCurrentPos = Control.MousePosition; 75 ptFormPos = Location; 76 } 77 } 78 79 private void frmTopMost_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 80 { 81 if (e.Button == MouseButtons.Left) 82 { 83 blnMouseDown = false; 84 } 85 } 86 //Restore parent from. 87 private void frmTopMost_DoubleClick(object sender, System.EventArgs e) 88 { 89 SwitchToMain(); 90 } 91 private void SwitchToMain() 92 { 93 pParent.RestoreWindow(); 94 this.Hide(); 95 } 96 97 #region //Context events. 98 private void OpenLable_Click(object sender, System.EventArgs e) 99 { 100 SwitchToMain(); 101 } 102 private void ExitLable_Click(object sender, System.EventArgs e) 103 { 104 Application.Exit(); 105 } 106 #endregion 107 108 private void frmTopMost_MouseEnter(object sender, System.EventArgs e) 109 { 110 this.toolTip1.Active = true; 111 this.toolTip1.AutoPopDelay = 500000; 112 this.toolTip1.InitialDelay = 500; 113 this.toolTip1.ReshowDelay = 100; 114 this.toolTip1.ShowAlways = true; 115 } 116 private void frmTopMost_MouseLeave(object sender, System.EventArgs e) 117 { 118 this.toolTip1.Active = false; 119 } 120 121 } 122 }
2、主窗体
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace TopMost 10 { 11 public partial class frmMain : Form 12 { 13 public frmMain() 14 { 15 InitializeComponent(); 16 } 17 18 private FormWindowState fwsPrevious; 19 private frmTopMost myTopMost; 20 private void frmMain_Load(object sender, EventArgs e) 21 { 22 //save the windowState; 23 fwsPrevious = this.WindowState; 24 25 //Create top most window; 26 myTopMost = new frmTopMost(this); 27 } 28 29 private void frmMain_SizeChanged(object sender,EventArgs e) 30 { 31 if (this.WindowState == FormWindowState.Minimized) 32 { 33 //window minimized. 34 //show the most window. 35 myTopMost.Show(); 36 this.ShowInTaskbar = false; 37 } 38 else if(this.WindowState!=fwsPrevious) 39 { 40 fwsPrevious = WindowState; 41 } 42 } 43 //Restore window. 44 public void RestoreWindow() 45 { 46 this.WindowState = fwsPrevious; 47 this.ShowInTaskbar = true; 48 } 49 } 50 }
完!!
以上是关于C# winform 在一个窗体中如何设置另一个窗体的TextBox的值的主要内容,如果未能解决你的问题,请参考以下文章