Search functions on an RTF Editor - c#

I'm creating an RTF Editor and I need help with the search functions. I have already created the find and replace code but I cant figure out how to code the find next and replace all code. Any help will be much appreciated. The following is the code that I have already. ( I am using Visual studio 2010 c# )
private void buttonFind_Click(object sender, EventArgs e)
{
RichTextBox frm1TB = ((Form1)this.Owner).rTB;
int foundAt;
foundAt = frm1TB.Text.IndexOf(txtSearch.Text);
if (foundAt == -1)
{
MessageBox.Show("Not Found");
}
else
{
frm1TB.SelectionStart = foundAt;
frm1TB.SelectionLength = txtSearch.TextLength;
frm1TB.Focus();
btnFindnext.Enabled = true;
btnReplaceall.Enabled = true;
btnReplace.Enabled = true;
}
}
private void buttonfindNext_Click(object sender, EventArgs e)
{
}
private void buttonreplace_Click(object sender, EventArgs e)
{
RichTextBox frm1TB = ((Form1)this.Owner).rTB;
btnFind_Click(sender,e);
frm1TB.SelectedText = txtReplace.Text;
}
private void buttonreplaceAll_Click(object sender, EventArgs e)
{
}

you can use the this overload of indexOf, define the startIndex as the index of the last result you've found + the length of the search string. now the indexOf will give you the location of the string in txtSearch.Text in the RTF box after the last occurrence.
to replace all just Replace

I think you can just to this:
frm1TB.Rtf = frm1TB.Rtf.Replace("replace what", "with this");

Related

How to copy, paste, cut in FastColoredTextBox?

I need three functions: Copy, Paste, Cut ,
For a FastColoredTextBox.. so far with my methods, the job is done but afterwards,
the cursor's position get changed and I got no clue on how to keep it where it
was before.
Here's my methods:
private void OnMouseMenuCut(object sender, EventArgs e)
{
var sPoint = rtbScript.SelectionStart;
var ePoint = rtbScript.SelectionLength;
var text = rtbScript.SelectedText;
rtbScript.Text = rtbScript.Text.Remove(sPoint, ePoint);
Clipboard.SetText(text.Replace("\n", "\r\n"));
rtbScript.Text = rtbScript.Text.Insert(sPoint, text);
}
private void OnMouseMenuCopy(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(rtbScript.SelectedText)) return;
Clipboard.SetText(rtbScript.SelectedText.Replace("\n", "\r\n"));
}
private void OnMouseMenuPaste(object sender, EventArgs e)
{
if (!Clipboard.ContainsText()) return;
var index = rtbScript.SelectionStart;
rtbScript.Text = rtbScript.Text.Insert(index, Clipboard.GetText());
}
Also, If there's a better way to do those functions, please post..
Thanks!
For a RichTextBox your code has more issues than loosing the Cursor position, It also looses all formatting! Here are versions that should work better:
private void OnMouseMenuCut(object sender, EventArgs e)
{
var sPoint = rtbScript.SelectionStart;
var text = rtbScript.SelectedText;
rtbScript.Cut();
Clipboard.SetText(text.Replace("\n", "\r\n"));
rtbScript.SelectionStart = sPoint;
}
private void OnMouseMenuCopy(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(rtbScript.SelectedText)) return;
Clipboard.SetText(rtbScript.SelectedText.Replace("\n", "\r\n"));
}
private void OnMouseMenuPaste(object sender, EventArgs e)
{
if (!Clipboard.ContainsText()) return;
var index = rtbScript.SelectionStart;
rtbScript.SelectedText = Clipboard.GetText();
rtbScript.SelectionStart = index + Clipboard.GetText().Length;
}
Note: You must never change the Text property of a RTB or else you will mess up the formating!
Since you wrote that this also works with your FastColoredTextBox I have undeleted the solution..
In the current version of FCTB, these methods already exist inside the FCTB.cs file. They just need to be linked up.

How to get value from radiobutton to linkbox

I'm trying to get the value of radio button selection to my listbox, but the listbox always gets the same value even though selection was different. Please help, I am a newbie to coding, I couldn't find any answers on the net either..
Here are the codes I've written:
void rdbtnOne_CheckedChanged(object sender, EventArgs e)
{
if (rdbtnOne.Checked == true)
{
rdbtnOne.Text = "Men";
}
else
{
rdbtnOne.Text = "Women";
}
}
void btnOne_Click(object sender, EventArgs e)
{
lstOne.Items.Add(i + rdbtnOne.Text);
i++;
}
Ok, I have found the solution, FINALLY. The reason that my code did not work in the first place was because I tried to give value by equaling rdbtnOne.Text directly. Instead I created another value to equal it. All right here is how it worked for me:
string MenOrWomen;
void rdbtnTwo_CheckedChanged(object sender, EventArgs e)
{
if (rdbtnTwo.Checked.Equals(true))
{
MenOrWomen = "Women";
}
else
{
MenOrWomen = "Men";
}
}
void rdbtnOne_CheckedChanged(object sender, EventArgs e)
{
if (rdbtnOne.Checked.Equals(true))
{
MenOrWomen = "Men";
}
else
{
MenOrWomen = "Women";
}
}
int i = 1;
void btnOne_Click(object sender, EventArgs e)
{
lstOne.Items.Add(i + MenOrWomen);
i++;
}

How to delete selected text from textbox in c#(SWF)

I try to do a notepad in c#,
I have some problems at delete function,
I want to delete selected text...
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a;
a = textBox1.SelectionLength;
textBox1.Text.Remove(textBox1.SelectionStart,a);
}
what is wrong?
Remove will return the truncated string, so you just need to reassign to the TextBox:
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a = textBox1.SelectionLength;
textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart,a);
}
Use SelectedText like this :
textbox1.SelectedText = "";

the total price in the datagrid when I passed it round that price

my English is not good because I'm Spanish so I'm using a translator if you do not understand something ask me.
my problem is that I'm doing a program with two windows in main window I have a datagrid in the second window I pass information with textbox, the problem is that by passing the total price by multiplying the amount I get a dessert for the price of a dessert, the total price in the datagrid when I passed it round that price, if the price happened 1.20 the program will change to 1.
gives me no error so I'll have to spend the entire program code sorry.
This is the second window
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (Application.Current.Properties["seleccionado"] == null)
{
textBox1.IsEnabled = false;
Postresinfo = new TabladePostre();
}
else
{
Postresinfo = (TabladePostre) (Application.Current.Properties["seleccionado"]);
textBox1.IsEnabled=false;
textBox1.Text = Convert.ToString(Postresinfo.refPostre);
textBox2.Text = Postresinfo.NombrePostre;
textBox3.Text = Convert.ToString(Postresinfo.cantidad);
textBox4.Text = Convert.ToString(Postresinfo.precio);
textBox5.Text = Convert.ToString(Postresinfo.preciototal);
}
LinqdePostresDataContext BasedeDatos;
string filename = "";
private void button1_Click(object sender, RoutedEventArgs e)
{
BasedeDatos(LinqdePostresDataContext)Application.Current.Properties["basedeDatos"];
Postresinfo.NombrePostre = textBox2.Text;
Postresinfo.cantidad = Convert.ToInt32(textBox3.Text);
Postresinfo.precio = Convert.ToDecimal(textBox4.Text);
Postresinfo.preciototal = Convert.ToDecimal(textBox5.Text);
Postresinfo.imagen = filename;
if (Application.Current.Properties["seleccionado"] != null)
{
Postresinfo.refPostre=Convert.ToInt32(textBox1.Text);
}
else
{
BasedeDatos.TabladePostres.InsertOnSubmit(Postresinfo);
}
BasedeDatos.SubmitChanges();
this.Close();
}
decimal precio = 0;
private void button2_Click(object sender, RoutedEventArgs e)
{
precio = Convert.ToDecimal(textBox4.Text);
textBox5.Text = Convert.ToString(precio * Convert.ToDecimal(textBox3.Text));
}
private void button9_Click(object sender, RoutedEventArgs e)
{
// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".jpg"; // Default file extension
dlg.Filter = "Text documents (.jpg)|*.jpg"; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
filename = dlg.FileName;
ImageSourceConverter conversor = new ImageSourceConverter();
image1.Source = (ImageSource)conversor.ConvertFromString(filename);
}
}
this is the main window:
LinqdePostresDataContext BasedeDatos = new LinqdePostresDataContext();
private void activar(object sender, RoutedEventArgs e)
{
Cargartabla();
}
private void Cargartabla()
{
var postre = (from n in BasedeDatos.TabladePostres
select n);
dataGrid1.ItemsSource = postre;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Application.Current.Properties["seleccionado"] = null;
Ventana2 Ventana2 = new Ventana2();
Ventana2.Show();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
BasedeDatos.TabladePostres.DeleteOnSubmit((TabladePostre)dataGrid1.SelectedItem);
BasedeDatos.SubmitChanges();
Cargartabla();
}
private void Activar2(object sender, EventArgs e)
{
Cargartabla();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
Application.Current.Properties["seleccionado((TabladePostre)dataGrid1.SelectedItem);
Application.Current.Properties["basedeDatos"] = BasedeDatos;
Ventana2 ventana2 = new Ventana2();
ventana2.Show();
}
If you need to know also I have a database with int price, int quantity in the total price in decimal.
thanks for reply, I tried both options have given me but does not work, these are the faults:
Postresinfo.refPostre = float.Parse (textBox1.Text) gives me no errors, the program runs normally and nothing changes
float.TryParse (textBox1.Text, out Postresinfo.refPostre) has these faults:
Error 1 A property, indexer or dynamic member access May not be passed as an out or ref parameter.
Error 2 The best overloaded method match for 'float.TryParse (string, out float)' has some invalid arguments
Error 3 Argument 2: can not convert from 'out int' to 'float out'
I tried the other code and nothing changes, the program runs normally
shane now, I tried this code:
Convert.ToDecimal (textBox1.Text);
but it changes nothing and runs normally.
I've also tried the other code, but nothing changes and the program runs normally
the fault is not textbox1, I think it's because it's in that TextBox5 textbox where I enter the price in decimal and passes it to the datagrid and is the rounded
I'll also attach the column fails me:
<DataGridTextColumn Binding="{Binding Path=preciototal}" Header="Precio Total"/>
thanks.
you got a lot of code here and i'm not sure what is the problem. i understand that you get round value but it'll be helpful to mention the name of the textBox that it happens in it.
i did saw this code:
Postresinfo.refPostre=Convert.ToInt32(textBox1.Text);
in button1_Click which will cause rounding if the value in textBox1.Text is float.
you should do
Postresinfo.refPostre=float.Parse(textBox1.Text);
or
float.TryParse(textBox1.Text, out Postresinfo.refPostre);
because it's a textbox and you might get a value which isn't a number.
if you decide to use Parse then you should do
try
{
Postresinfo.refPostre=float.Parse(textBox1.Text);
}
catch
{
// Show a message or write to log or simething
}
Your problem is that you are converting a price like "1.20" to an int with Convert.ToInt32(textBox1.Text);.
Try change Postresinfo.refPostre to a decimal and use Convert.ToDecimal(textBox1.Text); so use something like this as a FormatException could be thrown:
try
{
Convert.ToDecimal(textBox1.Text);
}
catch (Exception)
{
//Deal with Error
}

make first alphabet after space to Upper in text on textBox1_TextChanged event

I have a query regarding with TextBox.
When I type in the text box the words automatically changes. for example: "my name is kumar" to "My Name Is Kumar" and should be done on textBox1_TextChanged event.
currently i am doing this on Leave event
private void textBox1_Leave(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Substring(0, 1).ToUpper() + textBox1.Text.Substring(1);
}
please help me to accomplished it. lots of thanks in advance. :)
Use TextInfo.ToTitleCase Method
private void textBox1_Leave(object sender, EventArgs e)
{
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
textBox1.Text = myTI.ToTitleCase(textBox1.Text)
}
As a followup to the previous answer, if you add the following lines to the remainder of the body you'll ensure the correct behavior is maintained:
textBox1.SelectionStart = textBox1.TextLength;
textBox1.SelectionLength = 0;
So the full solution would be:
private void textBox1_Leave(object sender, EventArgs e)
{
//Original from JW's answer
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
textBox1.Text = myTI.ToTitleCase(textBox1.Text);
//New lines to ensure the cursor is always at the end of the typed string.
textBox1.SelectionStart = textBox1.TextLength;
textBox1.SelectionLength = 0;
}
This should solve your problem:
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
textBox1.Text = myTI.ToTitleCase(textBox1.Text);
textBox1.SelectionStart = textBox1.Text.Length;
}
I would use a Regex here, since it's simpler to implement and I don't think your TextBox will hold large strings. Since you want the string to be auto-corrected as you write, you need the TextChanged event instead of the Leave one:
private void textBox1_TextChanged(object sender, EventArgs e)
{
Regex regex = new Regex(" [a-z]");
foreach (Match match in regex.Matches(textBox1.Text))
textBox1.Text = regex.Replace(textBox1.Text, match.Value.ToUpper());
}

Categories

Resources