将textbox1中的值复制到textbox2 [英] copy a value in textbox1 to textbox2

查看:133
本文介绍了将textbox1中的值复制到textbox2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我想在textbox1上写一个数字数据并在textbox2上观看

我该怎么办?另一个问题是我想写。只有一次,而不是更多。例如,如果我写了12.54.318.10210.121,它就不能接受两个点!

hi i want to write a numeric data on textbox1 and watch it on textbox2
what do i do? another question is i want to write "." just once, not more. for example, if i wrote "12.54.318.10210.121", it can't accept two dot!

推荐答案

那么,首先要接受你需要使用的数字正则表达式,我应该说的最佳选择,可能 [0-9] +(\。[0-9] [0-9]?)?将满足需要你。检查一下是否有效?也可以按照以下链接执行此操作:

文本框应该只接受数字和点 [ ^ ]

如何限制TextBox在C#中的keypress事件中只接受十进制数中的一个点 [ ^ ]



要查看textbox1文本到textbox2,您只需使用 textbox2.Text = Textbox1.Text; 如果满足您的需求,请在textbox1 keypress事件中。

也可以看到:

如何使用keystokes在文本框2中显示textbox1值[ ^ ]

textbox1的值应该是在每个按键上显示在textbox2中。使用代理 [ ^ ]
Well, first for accepting numeric numbers you need to use regex, the best option i should say and possibly [0-9]+(\.[0-9][0-9]?)? would meet the need of you.Check if that works? Also follow these links to do that:
Text box should only accept only numbers and dot[^]
How to restrict the TextBox to accept only one dot in decimal number in keypress event in C#[^]

And to see the textbox1 text to textbox2 you can simply use textbox2.Text = Textbox1.Text; inside textbox1 keypress event if that meet your needs.
Also can see:
How can you display textbox1 value in textbox2 using keystokes[^]
Value of textbox1 should show in textbox2 on every key pressing.using delegates[^]


private char minus = '-';
private char dot = '.';

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if
    (
        // is it a digit ?
        Char.IsDigit(e.KeyChar)
        // or is it a Control key ?
        || Char.IsControl(e.KeyChar)
        // is it a period ?
        || e.KeyChar == dot
            // and not a period following another period
            && textBox1.Text.LastIndexOf(dot) != textBox1.Text.Length - 1
    )
    {
        // we're done
        return;
    }
    else
    {
        // is it a minus character, and will it appear at the start of the text ?
        if (e.KeyChar == minus && textBox1.Text.Length == 0)
        {
            // we're done
            return;
        }
    }

    // ignore the key-stroke
    e.Handled = true;
}

首先,我建议任何想要控制的人使用NumericUpDownControl或MaskedTextBox控件输入数字数据。为什么重新发明轮子?



但是,如果您有特殊需求,或者只是想要使用常规TextBox的挑战,并编写自己的过滤代码,那么...继续阅读:)



此处OP需要:



1.限制文本进入TextBox所以:



a。只允许使用数字,但以下情况除外:



b。减号:我们假设在数字开头只有合法



c。一个或多个句点,但连续不超过一个句号。



2.让第二个TextBox始终更新,并在第一个TextBox中成功输入任何文本。



我们可以通过将TextChanged EventHandler绑定到输入文本的TextBox1来快速处理需求#2:

First, I'd recommend to anyone wanting to have a Control to input numeric data into that they consider using a NumericUpDownControl, or a MaskedTextBox Control. Why re-invent the wheel ?

But, if you have special needs, or just want the challenge of using a regular TextBox, and writing your own filtering code, then ... keep reading :)

The OP here wants:

1. to constrain the text entered into a TextBox so:

a. only numbers are allowed, except:

b. a minus-sign: we'll assume that's only "legal" at the start of the number

c. one or more periods, but never more than one period in a row.

2. to have a second TextBox always updated with whatever Text is successfully entered in the first TextBox.

We can take care of requirement #2 quickly by just binding a TextChanged EventHandler to TextBox1, where the text is entered:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    textBox2.Text = textBox1.Text;
}

因此,现在TextBox1中出现的任何内容都出现在TextBox2中。而且,懒惰,我们在这里忽略了有人出现并在TextBox2中输入文本的可能性,然后想知道为什么它在TextBox1中按下键时消失。



KeyPress EventHandler允许我们通过将每个键击调用的KeyPressEventArgs参数(e.Handled)的Handled Property设置为'true来取消用户键输入。



请记住,如果我们在KeyPress EventHandler中什么都不做,那么该字符将被发送到它的目标容器Control,它将在那里呈现/显示。 imho这个名字'Handled在这里有点误导:我们真的在说e.Handled = true;我们希望消费键击,而不是传递它。值得注意的是KeyPress EventHandler在 KeyDown EventHandler之后被称为



KeyPressEventArgs结构为我们提供当前的KeyChar值击键(e.KeyChar)。我们可以通过使用Char对象的方便的内置函数来区分KeyChars的类型,例如Char.IsDigit(),Char.IsNumber(),Char.IsControl()等。



使用KeyDown和KeyUp EventHandlers,我们获得了一个KeyCode属性,这非常有用,因为我们可以直接比较KeyCodes,如:if(e.KeyCode == Keys.Subtract || e.KeyCode == Keys.OemMinus)...使用方便的Key Enumeration。但是,使用KeyCodes,你没有Char对象提供的有用运算符。

So, now whatever appears in TextBox1, appears in TextBox2. And, being lazy, we'll ignore here the possibility that someone comes along and enters text in TextBox2, and then wonders why it vanishes the moment a key is pressed in TextBox1.

The KeyPress EventHandler allows us to cancel user key entry by setting the 'Handled Property of the KeyPressEventArgs argument (e.Handled) called with each key-stroke to 'true.

Keep in mind that if we do nothing in the KeyPress EventHandler, the character will be "sent to" its target container Control where it will be rendered/displayed. imho the name 'Handled is kind mis-leading here: we're really saying by e.Handled = true; that we wish to consume the key-stroke, and not pass it along. It's worthwhile to note that the KeyPress EventHandler is called after the KeyDown EventHandler.

The KeyPressEventArgs structure provides us with a KeyChar value for the current key-stroke (e.KeyChar). We can discriminate types of KeyChars by using the handy built in functions of the Char object, such as Char.IsDigit(), Char.IsNumber(), Char.IsControl(), etc.

With the KeyDown and KeyUp EventHandlers we get a KeyCode property handed to us, which is quite useful, because we can do direct comparisons of KeyCodes like: if (e.KeyCode == Keys.Subtract || e.KeyCode == Keys.OemMinus) ... using the handy Keys Enumeration. But, with KeyCodes you do not have the useful operators that the Char object provides.


这个男女同校比其他人更好:

this coed is better than others:
if 
(!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar!= '.' && e.KeyChar!='-')
            {
                e.Handled = true;
            }
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                    e.Handled = true;
            }
            if (e.KeyChar == '-' && (sender as TextBox).Text.IndexOf('-') > -1)
            {
                e.Handled = true;
            }
            textBox2.Text = textBox1.Text;


这篇关于将textbox1中的值复制到textbox2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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