使用 ListBox 的 Items 作为源替换 TextBox 中的单词
Replace words in a TextBox using Items of a ListBox as the source
我想更改此文本框中的文本:
使用此 ListBox 作为要更改的词:
应使用此处插入的文本更改单词:
我已经将单词拆分到 text[]
数组中。
现在,例如,当我 select 单词 who
并单击 Replace
按钮将 who
替换为 hey
时:字符串 who are you
应该在 hey are you
.
中更改
但似乎我的代码不能正常工作,它一直显示原始文本而不是修改后的文本:
private void btnReplace_Click(object sender, EventArgs e)
{
string replace = txtReplace.Text;
string tempReplace = "";
txtList = listBox1.SelectedItems.ToString();
txtText.Text = "";
for (int i = 0; i < text.Length; i++)
{
if (text[i].ToLower() == txtList.ToLower())
{
text[i] = replace;
}
tempReplace += text[i] + " ";
}
txtText.Text = tempReplace;
}
我的代码有什么问题?
由于您的控件 绑定 在一起,在共同努力执行相同的任务时,您实际上可以将它们绑定到一个公共数据源并使用数据更改通知来更新同时控制所有这些。
▶ 源TextBox中列出单词的ListBox和修改这些部分的TextBox可以使用相同的数据源来创建Binding。
▶ 提供单词列表的 TextBox 可以在数据更改时更新,使用 Parse event of the Binding 用于 connect
将文本部分编辑到数据源的 TextBox。
这只是其中一种可能的方法,可以使用这些工具生成更复杂的绑定。
数据源可以是一个List<strings>
,其中包含源文本中的所有单词。但是一个简单的List<T>
单独做不了多少,所以你可以把它附加到一个BindingSource上:这个class可以提供数据变化通知(所以绑定到这个数据源的所有控件都是通知 当数据改变时)。
您可以在所有控件准备好完全支持数据绑定(Form.Load
事件可以做到)后设置绑定,并在源文本是时创建数据源(List<string>
)更换。在可视示例中,我为此使用了 Button,但它应该是源 TextBox 中的文本被另一个字符串替换的情况。 不要为此使用它的 TextChanged
事件。
private List<string> parts = null;
private BindingSource partsSource = null;
private void SomeForm_Load(object sender, EventArgs e)
{
partsSource = new BindingSource();
partsSource.DataSource = typeof(List<string>);
lstParts.DataSource = partsSource;
var txtBinding = new Binding("Text", partsSource, "", false, DataSourceUpdateMode.OnPropertyChanged);
txtBinding.Parse += (o, args) => {
if (parts.Count == 0) return;
parts[lstParts.SelectedIndex] = args.Value.ToString();
txtSource.Text = string.Join(" ", parts);
};
txtReplace.DataBindings.Add(txtBinding);
}
private void btnBind_Click(object sender, EventArgs e)
{
parts = new List<string>(txtSource.Text.Split());
partsSource.DataSource = parts;
}
视觉效果:
→提供源文本的TextBox命名为txtSource
.
→ 按钮是 btnBind
.
→ 列表框被命名为 lstParts
.
→ 用于编辑部件的文本框命名为 txtReplace
.
我想更改此文本框中的文本:
使用此 ListBox 作为要更改的词:
应使用此处插入的文本更改单词:
我已经将单词拆分到 text[]
数组中。
现在,例如,当我 select 单词 who
并单击 Replace
按钮将 who
替换为 hey
时:字符串 who are you
应该在 hey are you
.
中更改
但似乎我的代码不能正常工作,它一直显示原始文本而不是修改后的文本:
private void btnReplace_Click(object sender, EventArgs e)
{
string replace = txtReplace.Text;
string tempReplace = "";
txtList = listBox1.SelectedItems.ToString();
txtText.Text = "";
for (int i = 0; i < text.Length; i++)
{
if (text[i].ToLower() == txtList.ToLower())
{
text[i] = replace;
}
tempReplace += text[i] + " ";
}
txtText.Text = tempReplace;
}
我的代码有什么问题?
由于您的控件 绑定 在一起,在共同努力执行相同的任务时,您实际上可以将它们绑定到一个公共数据源并使用数据更改通知来更新同时控制所有这些。
▶ 源TextBox中列出单词的ListBox和修改这些部分的TextBox可以使用相同的数据源来创建Binding。
▶ 提供单词列表的 TextBox 可以在数据更改时更新,使用 Parse event of the Binding 用于 connect
将文本部分编辑到数据源的 TextBox。
这只是其中一种可能的方法,可以使用这些工具生成更复杂的绑定。
数据源可以是一个List<strings>
,其中包含源文本中的所有单词。但是一个简单的List<T>
单独做不了多少,所以你可以把它附加到一个BindingSource上:这个class可以提供数据变化通知(所以绑定到这个数据源的所有控件都是通知 当数据改变时)。
您可以在所有控件准备好完全支持数据绑定(Form.Load
事件可以做到)后设置绑定,并在源文本是时创建数据源(List<string>
)更换。在可视示例中,我为此使用了 Button,但它应该是源 TextBox 中的文本被另一个字符串替换的情况。 不要为此使用它的 TextChanged
事件。
private List<string> parts = null;
private BindingSource partsSource = null;
private void SomeForm_Load(object sender, EventArgs e)
{
partsSource = new BindingSource();
partsSource.DataSource = typeof(List<string>);
lstParts.DataSource = partsSource;
var txtBinding = new Binding("Text", partsSource, "", false, DataSourceUpdateMode.OnPropertyChanged);
txtBinding.Parse += (o, args) => {
if (parts.Count == 0) return;
parts[lstParts.SelectedIndex] = args.Value.ToString();
txtSource.Text = string.Join(" ", parts);
};
txtReplace.DataBindings.Add(txtBinding);
}
private void btnBind_Click(object sender, EventArgs e)
{
parts = new List<string>(txtSource.Text.Split());
partsSource.DataSource = parts;
}
视觉效果:
→提供源文本的TextBox命名为txtSource
.
→ 按钮是 btnBind
.
→ 列表框被命名为 lstParts
.
→ 用于编辑部件的文本框命名为 txtReplace
.