
C# Jak se dá najít a nahradit text v richTextBox1?
Jak se dá najít a nahradit text v richTextBox1?
Replace nafunguje
private void btnNahradit_Click(object sender, EventArgs e)
{
int start = 0;
int konec = richTextBox1.Text.LastIndexOf(cbVyhledat.Text);
while (start < konec)
{
richTextBox1.Find(cbVyhledat.Text, start, richTextBox1.TextLength, RichTextBoxFinds.MatchCase);
richTextBox1.SelectionBackColor = Color.Yellow;
start = richTextBox1.Text.IndexOf(cbVyhledat.Text, start) + 1;
//Replace nefunguje
richTextBox1.Text = richTextBox1.Text.Replace(cbVyhledat.Text,
cbNahradit.Text);
}
}
////////////////////////////////////////////////////////////
Anebo jak udělat tady z toho cyklus ať to nahradí vše?
///////////////////////////////////////////////////////////
mujString = richTextBox1.Text;
najit = cbVyhledat.Text.Trim();
nahradit = cbNahradit.Text.Trim();
location = richTextBox1.Text.IndexOf(najit);
if (umisteni == -1)
{
MessageBox.Show("String není", "Zpráva");
}
else
{
while (???????)
{
richTextBox1.Text = mujString.Remove(location, najit.Length).Insert
(location, nahradit);
// Alternativa
/*richTextBox1.Text = mujString.Substring(0, location) + nahradit +
mujString.Substring(location + najit.Length);*/
}
}