私はこのフォームを持っています:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GatherLinks
{
public partial class ChangeLink : Form
{
public ChangeLink()
{
InitializeComponent();
}
public string getText()
{
return textBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
DialogResult = DialogResult.OK;
}
else
{
}
}
private void ChangeLink_Load(object sender, EventArgs e)
{
this.AcceptButton = button1;
}
}
}
そしてForm1のこのコード:
public void KeysValuesUpdate()
{
DialogResult dr = DialogResult.None;
using (var w = new StreamWriter(keywords_path_file))
{
crawlLocaly1 = new CrawlLocaly(this);
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
if (FormIsClosing != true)
{
dr = crawlLocaly1.ShowDialog(this);
}
if (dr == DialogResult.OK)
{
if (LocalyKeyWords.ContainsKey(mainUrl))
{
LocalyKeyWords[mainUrl].Clear();
LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
}
else
{
LocalyKeyWords[mainUrl] = new List<string>();
LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
}
Write(w);
ClearListBox();
}
if (dr == DialogResult.Cancel)
{
Write(w);
}
if (dr == DialogResult.None)
{
Write(w);
}
}
}
このKeysValuesUpdate()関数は次のように呼び出されます。
private void button2_Click(object sender, EventArgs e)
{
cl = new ChangeLink();
cl.StartPosition = FormStartPosition.CenterParent;
DialogResult dr = cl.ShowDialog(this);
if (dr == DialogResult.Cancel)
{
cl.Close();
}
else if (dr == DialogResult.OK)
{
label4.Text = cl.getText();
mainUrl = cl.getText();
if (!LocalyKeyWords.ContainsKey(mainUrl))
{
newUrl = true;
KeysValuesUpdate();
}
else
{
newUrl = false;
KeysValuesUpdate();
}
OptionsDB.set_changeWebSite(cl.getText());
cl.Close();
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
}
button2をクリックすると、テキストボックスのある新しいフォームが開き、その中にテキストを入力できます。次に、内部のテキストがすでに存在するかどうかを確認し、newUrlがfalseまたはtrueです。次に、新しいフォームで[OK]をクリックすると、入力したテキストが[含まれている/存在している]かどうかがチェックされます。
ユーザーが入力中にテキストボックスに何かを入力したときに、それがContain / Existキーの場合は、テキストボックスのテキストを赤で色付けします。ユーザーが入力し続け、テキストがContain/Existではない場合は黒に戻します。ただし、テキストボックスContain / Existのテキストがすでに赤で色付けされている場合は毎回、テキストが他のテキスト内にある場合ではなく、一致する場合に限ります。
これは黒です:例:Danny hello all
ただし、テキストボックスのみを入力すると、helloという単語が赤になります。helloの後に入力を続けると、テキストを削除してhelloという単語だけを保持すると、テキストボックス内のすべてのテキストが黒になります。再び赤。
そして、それは上記のコードに従って、テキストボックスにテキストを入力するときにリアルタイムである必要があります。
textBox1 textchangedイベントで更新されたコードを含む新しいフォーム:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GatherLinks
{
public partial class ChangeLink : Form
{
Form1 f1;
public ChangeLink(Form1 f)
{
InitializeComponent();
f1 = f;
}
public string getText()
{
return textBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
DialogResult = DialogResult.OK;
}
else
{
}
}
private void ChangeLink_Load(object sender, EventArgs e)
{
this.AcceptButton = button1;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (f1.mainUrl.Contains(textBox1.Text))
{
textBox1.ForeColor = Color.Red;
}
else
textBox1.ForeColor = Color.Black;
}
}
}