.net 刷新文本框文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17382585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh TextBox text?
提问by ElektroStudios
I will explain the situation:
我来解释一下情况:
My form have a listview, a textbox, and a label.
The Textbox is to write a directory path to add that directory to the listview.
With the event OnTextchanged of the textbox I check if the listview contains the textbox text before adding it, if yes then the label says "Directory is already in the listview" if not then the label says otherwise.
Now, after adding the directory path from the textbox to the listview, like I don't modified the textbox text after adding the text to the listview then the OnTextChanged event of the textbox is not processed so the label is still saying that the directory IS NOTinside the listview (because the OnTextchanged event is not processed).
我的表单有一个列表视图、一个文本框和一个标签。
Textbox就是写一个目录路径把那个目录添加到listview中。
通过文本框的 OnTextchanged 事件,我在添加之前检查列表视图是否包含文本框文本,如果是,则标签显示“目录已在列表视图中”,如果没有,则标签另有说明。
现在,在将文本框的目录路径添加到列表视图后,就像我在将文本添加到列表视图后没有修改文本框文本一样,文本框的 OnTextChanged 事件没有被处理,所以标签仍然说目录是NOT列表视图内(因为不处理OnTextchanged事件)。
Then to solve this little issue I refresh the text of the textbox after adding the text to the listviewx, I did it with a little snippet I've made for this:
然后为了解决这个小问题,我在将文本添加到 listviewx 后刷新了文本框的文本,我使用了我为此制作的一个小片段:
Private Sub Refresh_Textbox_Text(ByVal TextBox As TextBox)
Dim TempText As String = TextBox.Text
TextBox.Clear()
TextBox.Text = TempText
End Sub
But I think that's a noob way to do it, I wonder if exists a native method to do refresh in the way I need the text of a textbox like seen in that snippet, I've tried with some methods as "refresh", "invalidate", etc... but none does the same.
但我认为这是一种 noob 的方法,我想知道是否存在一种本地方法来刷新我需要文本框文本的方式,就像在那个片段中看到的那样,我尝试了一些方法作为“刷新”,“无效”等......但没有一样。
回答by Mark Hall
There really is not a Native way to raise the TextChangedevent without actually changing the Text, the Updatedand RefreshMethods deal mainly with refreshing the drawing of the Control. But if this is something that you need to add to additional TextBoxes you can try making an Extension method, or you can add a Handler to your TextBox's Invalidated Event .
在TextChanged不实际更改Text、Updated和Refresh方法 的情况下,确实没有一种本机方式来引发事件,主要处理刷新控件的绘制。但是,如果这是您需要添加到其他Extension methodTextBox 的内容,您可以尝试制作,或者您可以将 Handler 添加到您的 TextBox 的 Invalidated Event 。
Using the Invalidated event
使用 Invalidated 事件
void textBox1_Invalidated(object sender, InvalidateEventArgs e)
{
textBox1_TextChanged(sender, new EventArgs());
}
Extension Method
扩展方法
I will admit that this is probably overkill....
我承认这可能有点矫枉过正......
Quick and dirty example:
快速而肮脏的例子:
using System;
using System.Windows.Forms;
using ExtensionMethods;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.RefreshCurrent();
}
}
}
namespace ExtensionMethods
{
public static class MyExtensions
{
public static void RefreshCurrent( this TextBox tb)
{
string temp = tb.Text;
tb.Text = "";
tb.Text = temp;
}
}
}
回答by 06needhamt
Try inserting Me.Refresh()like so
尝试Me.Refresh()像这样插入
Private Sub Refresh_Textbox_Text(ByVal TextBox As TextBox)
Dim TempText As String = TextBox.Text
TextBox.Clear()
TextBox.Text = TempText
Me.Refresh()
End Sub
Hope this helps
希望这可以帮助
回答by David -
I don't understand the 4th step completely, but have a look to the following code
我完全不明白第4步,但看看下面的代码
Dim ListItems1 As New List(Of String)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Button1.Enabled = False
Label1.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListItems1.Add(TextBox1.Text)
ListView1.Items.Add(TextBox1.Text)
Label1.Text = ""
TextBox1.Text = ""
TextBox1.Focus()
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length = 0 Then
Button1.Enabled = False
Else
If ListItems1.Contains(TextBox1.Text) Then
Label1.Text = "Directory is already in the listview"
Button1.Enabled = False
Else
Label1.Text = "Directory is not in the listview"
Button1.Enabled = True
End If
End If
End Sub
if you don't want a button to exist, try the following
如果您不希望按钮存在,请尝试以下操作
Dim ListItems1 As New List(Of String)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Label1.Text = ""
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length = 0 Then
Label1.Text = ""
Else
If ListItems1.Contains(TextBox1.Text) Then
Label1.Text = "Directory is already in the listview"
Else
Label1.Text = "Directory is not in the listview"
End If
End If
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 13 And ListItems1.Contains(TextBox1.Text) = False And TextBox1.Text.Length > 0 Then
ListItems1.Add(TextBox1.Text)
ListView1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
Label1.Text = ""
End If
End Sub

