C# 为什么在onchangetext事件中键入时它什么都不做?
当事件TextChanged中的代码在事件TextBoxURL_KeyDown中时,它也可以很好地使用我想要的逻辑。但是后来我不得不按enter键,所以我把它移到了TextChanged事件中,但是现在当我在TextBoxUrl中键入一个有效的时,什么也没有发生 代码:C# 为什么在onchangetext事件中键入时它什么都不做?,c#,.net,winforms,C#,.net,Winforms,当事件TextChanged中的代码在事件TextBoxURL_KeyDown中时,它也可以很好地使用我想要的逻辑。但是后来我不得不按enter键,所以我把它移到了TextChanged事件中,但是现在当我在TextBoxUrl中键入一个有效的时,什么也没有发生 代码: TextChanged在文本框的出口处激发。您可能正在寻找KeyDown或KeyUp,或KeyPress-可能是KeyUp,因为这将允许您在添加新字符后捕获文本框中的内容。该定义看起来有误……您没有实现事件处理程序,而是在掩盖一
TextChanged在文本框的出口处激发。您可能正在寻找KeyDown或KeyUp,或KeyPress-可能是KeyUp,因为这将允许您在添加新字符后捕获文本框中的内容。该定义看起来有误……您没有实现事件处理程序,而是在掩盖一个事件处理程序。去掉新的关键字。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DownloadMultipleFiles
{
public partial class AddNewDownloads : Form
{
public AddNewDownloads()
{
InitializeComponent();
foreach (Control ctrl in this.Controls)
{
if ((ctrl as TextBox) != null)
{
(ctrl as TextBox).TextChanged += TextChanged;
}
}
this.TextBoxURL.KeyDown += new KeyEventHandler(TextBoxURL_KeyDown); //add keyDown event
if (TextBoxFilename.Text == "" || TextBoxBrowse.Text == "")
{
TextBoxFilename.Enabled = false;
TextBoxBrowse.Enabled = false;
btnOK.Enabled = false;
}
}
private void AddNewDownloads_Load(object sender, EventArgs e)
{
}
private void btnBrowse_Click(object sender, EventArgs e)
{
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOK_Click(object sender, EventArgs e)
{
}
private void TextBoxURL_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) //validate the URL when you press the Enter key
{
}
}
private bool CheckValidUrl() // validate from clipboard
{
string isUrl = Clipboard.GetText();
Uri uriResult;
bool result = Uri.TryCreate(isUrl, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
return result;
}
private bool WriteValidUrl() // validate manual input
{
string pattern2 = @"^http://www.[a-z].com$";
Match match = Regex.Match(TextBoxURL.Text, pattern2);
if (match.Success)
return true;
else
return false;
}
private new void TextChanged(object sender, EventArgs e)
{
if (CheckValidUrl() == true || WriteValidUrl() == true)
{
TextBoxFilename.Enabled = true;
TextBoxBrowse.Enabled = true;
btnOK.Enabled = true;
}
else
{
TextBoxFilename.Enabled = false;
TextBoxBrowse.Enabled = false;
btnOK.Enabled = false;
}
if (TextBoxURL.Text == "")
{
TextBoxFilename.Enabled = false;
TextBoxBrowse.Enabled = false;
btnOK.Enabled = false;
}
}
}
}