C#在textBox和richTextBox之间传输数据 [英] C# Data Transfer between textBox and richTextBox

查看:68
本文介绍了C#在textBox和richTextBox之间传输数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在form1中有两个RichTextBox,一旦在form1 richTextBox1中选择特定文本并按一个快捷键,然后将form1 richTextBox1选中的文本值传输到form2 textBoxe1。



我的问题是我不能将form2 textbox1值传递给form1 richTextBox2,同时保留form1。



此外,上面的过程应该继续作为print1到form1中的每一行richtextbox2。



等待更好的源代码解决方案。



I have twoRichTextBoxes in form1, once select specific texts in form1 richTextBox1 and press a shortcut key, then form1 richTextBox1 selected text values transfer to the form2 textBoxe1.

My problem is I cannot transfer form2 textbox1 value to the form1 richTextBox2 while keeping form1.

Also, above process should need to continue as print value to each line in the form1 richtextbox2.

Waiting for a better solution with source code.

Form1
//This is not working properly
public string TextConvert
        {
            get { return richTextBox2.Text; }
            set { richTextBox2.Text = value; }
        }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.Space))
            {
                Box form = new Box();
                form.TextExport = richTextBox1.SelectedText;
                form.ShowDialog();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }







Form2
//This is not working properly
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
                if (keyData == (Keys.Enter))
                {

                    MessageBox.Show("Hi");
                    MainWindow form1 = new MainWindow();
                    form1.TextConvert = textBox1.Text;
                    this.Close();
                }
            return base.ProcessCmdKey(ref msg, keyData);
        }
public string TextExport
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

推荐答案

你可以试试这个。这将允许你从form2获得texbox值。



in form1



you could try this. this would allow you to get the texboxvalue from form2.

in form1

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.Space))
            {
                Box form = new Box();
                form.TextExport = richTextBox1.SelectedText;
                form.ShowDialog();
                
                richTextBox1.Text = richTextBox1.Text + "\n" + form.texvalue; //this will get the value of texvalue from form2.

            }
            return base.ProcessCmdKey(ref msg, keyData);
        }





in form2





in form2

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
                if (keyData == (Keys.Enter))
                {
 
                    MessageBox.Show("Hi");
                    MainWindow form1 = new MainWindow();
                    texvalue = textBox1.Text; //put the value of the textbox to textvalue
                    this.Close();
                }
            return base.ProcessCmdKey(ref msg, keyData);
        }
public string TextExport
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }
public string texvalue = String.Empty; //we will access this from form1





对不起我使用的条款我不擅长他们。

希望这次能帮到你。



sorry for the terms i use i''m not good at them.
hope this will help you this time.


这段代码用你传递的那个代码覆盖richtextbox的当前值

来自form2。





this code overwrites the current value of the richtextbox
with the one you are passing from form2.


public string TextConvert
{
      get { return richTextBox2.Text; }
      set { richTextBox2.Text = value; }
}





您可以尝试用此替换它



you could try replacing that with this

public string TextConvert
{
      get { return richTextBox2.Text; }
      set { richTextBox2.Text = richTextBox2.Text+"\n"+value; }
}


这篇关于C#在textBox和richTextBox之间传输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆