C# 将工具控件连接到另一个窗体(void)
我已经使用以下代码将一个工具控件连接到另一个1: ---Form1.csC# 将工具控件连接到另一个窗体(void),c#,winforms,visual-studio-2010,C#,Winforms,Visual Studio 2010,我已经使用以下代码将一个工具控件连接到另一个1: ---Form1.cs public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frm
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public string LabelText
{
get { return Lbl.Text; }
set { Lbl.Text = value; }
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.mainForm.LabelText = txtMessage.Text;
if (timer1.Enabled == true)
{
int line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
int column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
txtMessage.Text = "line: " + line.ToString() + " , column: " + column.ToString();
}
}
}
——Form2.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public string LabelText
{
get { return Lbl.Text; }
set { Lbl.Text = value; }
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.mainForm.LabelText = txtMessage.Text;
if (timer1.Enabled == true)
{
int line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
int column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
txtMessage.Text = "line: " + line.ToString() + " , column: " + column.ToString();
}
}
}
*****产量为**
表单2中的标签文本已连接到表单1
所以它已经被修复了
现在我的问题是,有没有一种方法可以用同样的方法来处理空函数
我的意思是,例如:
在Form1中,我得到了1个按钮,其中包含一个控件:
richTextBox1.Copy();
然后此控件将用于Form2上的richTextBox1。
(将复制Form2上richtextbox中的选定文本)
可能吗?真的需要帮助。提前多谢 这里有一些东西可以让你开始:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm2 = new Form2(this);
}
}
并确保richTextBox1
已声明为public
以及:
这里要做的是:使用对发送方Form1
实例的引用初始化Form2
实例,并使用该引用访问RichTextBox
编辑:
也许(!)这就是你想要的:
mainForm.richTextBox1.Copy();
您需要将Form2的声明移到类级别: --格式1
Form2 frm = null;
private void button1_Click(object sender, EventArgs e)
{
frm = new Form2(this);
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
if (frm != null)
{
frm.CopyRichTextBox();
}
}
--格式2
public void CopyRichTextBox()
{
this.richTextBox1.Copy();
}
不,先生,我想要的是我想要转移控制form2sir上richtextbox的按钮(richTextBox1.Copy();)的控制权请阅读上面的斜体文字这是我想要做的:(谢谢。我已经阅读了上面的斜体文本。这就是代码的作用。表单1中的按钮2允许您从表单2中的RichTextBox复制所选文本。我将在一分钟后发布结果如果(frm!=null){frm.RichTextBoxSelectedText.copy();}?我不需要将Lbl.Text作为RichTextBox的文本。我只希望RichTextBox有一个copy命令。但是它的错误是没有重载方法“copy”需要0个参数。不,先生,我想要的是我想转移按钮的控制权(richTextBox1.copy();)它控制form2sir上的richtextbox请阅读上面的斜体文本这就是我想做的:( thanks@Elegiac你写的不是英语不正确,就是你称之为“控制”的东西实际上是另一种东西。我根据你可能要找的东西编辑了我的答案。我写的那行是Form2,但会将Form1的richTextBox1的文本复制到剪贴板上。@Elegiac这很奇怪。我不明白为什么。@Elegiac您是否在使用Silverlight?(我刚刚搜索并发现它不包含
Copy
方法。)@Elegiac我们想提供帮助,但我们不理解您的要求。我得到了1个按钮,其中有一个控件:richTextBox1.Copy();意思是什么?您是说按钮的事件处理程序中有该代码吗?(带有:private void timer1_Tick…的代码称为事件处理程序,而不是控件。按钮是控件,RichTextBox也是。)