How to copy, paste, cut in FastColoredTextBox? - c#

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.

Related

How do I check the contents of a TextBox?

I have a textbox. In that textbox I write Human. Then I click a button, and if the word is human, then on a richtextbox, the word human will appear.
Here's the code I've tried.
private void button3_Click(object sender, EventArgs e)
{
if (textBox4.Text)
{
richTextBox1.Text = "human";
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
string value = textBox4.Text;
}
I tried making the textbox into a string so I could use it in the if statement, but it didn't work, so instead I used texbox4.text, but it is still wrong.
You could simply do with this piece of code,
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text =="human")
{
richTextBox1.Text = textBox1.Text;
}
}

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 = "";

C# checkedlistbox mouse enter/leave error

I want my checkedlistbox to expand to a certain size when the mouse enters and then go back to a its original size after mouse leaves. Below is the code is have. However, I receive an error when i have another program selected and my mouse goes over the checkedlistbox while the application is not active.
Any suggestions on how to fix?
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
Search.ActiveForm.Height = 552;
checkedListBox1.Height = 130;
}
private void checkedListBox1_MouseLeave(object sender, EventArgs e)
{
Search.ActiveForm.Height = 452;
checkedListBox1.Height = 34;}
Error Code - Object Reference not set to an instance of an object.
Try this
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
checkedListBox1.Size = new Size(Width,Height);
}
This of course would work so that no exception is thrown, but I hope it's also what you want:
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
if(Search.ActiveForm == null) return;
Search.ActiveForm.Height = 552;
checkedListBox1.Height = 130;
}
private void checkedListBox1_MouseLeave(object sender, EventArgs e)
{
if(Search.ActiveForm == null) return;
Search.ActiveForm.Height = 452;
checkedListBox1.Height = 34;
}

Search functions on an RTF Editor

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");

Tooltip not showing for nested control

I have the following piece of code that works great in all but one instance.
private void tbxLastName_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
GetRemainingChars(sender);
}
public void GetRemainingChars(object sender)
{
var control = sender as TextEdit;
var maxChars = control.Properties.MaxLength;
tipCharacterCounter.Show(control.Text.Length + "/" + maxChars, this, control.Location.X, control.Location.Y - control.Height);
}
I just repeat this process from any textbox. Unfortunately, I have one control that is more complicated and I cannot get this to work. The Event portion looks like this -->
private void memDirectionsToAddress_Popup(object sender, EventArgs e)
{
MemoExPopupForm popupForm = (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow as MemoExPopupForm;
MemoEdit meDirections = popupForm.Controls[2] as MemoEdit;
meDirections.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(meDirections_EditValueChanging);
}
void meDirections_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
GetRemainingChars(sender);
}
What I don't understand is if I replace the tipCharacterCounter portion with, say updating a label, it works fine. It's like the ToolTip is hidden or something but I have tried feeding Show() different points to no avail.
Ideas?
Which version of DXPerience are you using? I've tried the following code using DXperience 10.1.5 and it works fine here:
private void memoExEdit1_Popup(object sender, EventArgs e) {
MemoExPopupForm popupForm = (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow as MemoExPopupForm;
MemoEdit meDirections = popupForm.Controls[2] as MemoEdit;
meDirections.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(meDirections_EditValueChanging);
}
void meDirections_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e) {
GetRemainingChars(sender);
}
public void GetRemainingChars(object sender) {
TextEdit control = sender as TextEdit;
int maxChars = control.Properties.MaxLength;
tipCharacterCounter.ShowHint(control.Text.Length + "/" + maxChars, control, ToolTipLocation.RightBottom);
}

Categories

Resources