Search a word in rich edit control of devexpress - c#

I want to search a word in rich edit control. I will write a word in a text box and then click on a button named search button, and then the searched word will be highlighted in the rich edit control.
How can I solve this problem?
In rich text box we use RichTextBox.TextLength, but rich edit control doesn't support this.
private void button2_Click(object sender, EventArgs e) //Search button
{
int index = 0;
while(index <= richEditControl1.Text.LastIndexOf(textBox1.Text))
{
richEditControl1.Find(textBox1.Text, index, richEditControl1.TextLength, RichTextBoxFinds.None);
richEditControl1.SelectionBackColor = Color.IndianRed;
index = richEditControl1.Text.IndexOf(textBox1.Text, index) + 1;
}
}

Try with StartSearch method. Something like this.
private void SearchRTF(string mytext) {
var result = myRichEditControl.Document.StartSearch(mytext);
if (result.FindNext()) {
var section = myRichEditControl.Document.BeginUpdateCharacters(result.CurrentResult);
section.ForeColor = System.Drawing.Color.White;
section.BackColor = System.Drawing.Color.Blue;
myRichEditControl.Document.EndUpdateCharacters(section);
}
}

Related

Need help deleting rows/lines in richtextbox c# win forms

So essentially, I have a button, when I press the button, the button should delete the most recent row of text embedded into the rich text box. Press it again, it should delete the next line above that etc.
I tried a using .substring, which it worked, but required me to double click the button instead of a single click..
Furthermore, when I press the add text button it should replace the deleted line. Currently, the delete button button1_click just removes all lines, but all I want the it to do is remove a line of text from the richtextbox.
Edit:
Huge apologies before, I forgot to add the code as mentioned in replies, apologies.
public partial class Form1 : Form
{
string[] texts = new string[10];
int i = 0;
public Form1()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (TextBox.Text == "")
{
texts[i] = texts[i];
}
else
{
texts[i] += TextBox.Text + "\r\n";
richText.Text += texts[i];
++i;
TextBox.Text = "";
}
}
private void richText_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//i dont know need to do research or im too dumb
richText.Text = "";
for (int i = 0; i < texts.Length; i++)
{
texts[i] = "";
}
i = 0;
//https://stackoverflow.com/questions/22587505/delete-the-last-line-of-rich-text-box
//works, but double click instead of one click
}
}
}
To clarify, the left button adds text from the textbox and the right button is basically the function aforementioned.
enter image description here
Thanks to anyone who helps!

How to change color of text in windows form when you click a button?

When I click a button with a word e.g "cat" I want this word in richTextBox1 change to color red. Of course I made it wrong, but I would like to learn how to change it.
private void btn1(object sender, EventArgs e)
{
Button button = sender as Button;
string wordToColor = button.Text;
ChangeColorOfText(richTextBox1, wordToColor);
}
private void ChangeColorOfText(RichTextBox richTextBox1, string word)
{
ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Red = richTextBox1.SelectionColor;
int index;
do
{
index = richTextBox1.Find(word);
if (index >= 0)
{
richTextBox1.Select(index, word.Length);
richTextBox1.SelectionColor = ColorDialog.Red;
}
}
while (index >= 0);
}
I had few things that may be a solution for you:
It was a problem for me but it's not a must:
private void button1_Click(object sender, EventArgs e)
{
//Your code for finding and selecting your text
if (!String.IsNullOrEmpty(myRichTextBox.Text))
{
//Your code for selecting your text
myRichTextBox.SelectionStart = 0;
myRichTextBox.SelectionLength = myRichTextBox.Text.Length;
}
myRichTextBox.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
myRichTextBox.SelectionColor = Color.Red;
}
I think your problem was not setting the font before you set the color
(When I added the line myRichTextBox.SelectionFont = new Font("Verdana", 12, FontStyle.Bold); everything worked)
The Second thing I did in the code was to use
myRichTextBox.SelectionColor = Color.Red;
insted of
richTextBox1.SelectionColor = ColorDialog.Red;
it's easier and more efficient.
At the line
private void ChangeColorOfText(RichTextBox richTextBox1, string word)
you dont have to add the RichTextBox richTextBox1 element. (worked fine for me without it).
I deleted ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Red = richTextBox1.SelectionColor;
and changed richTextBox1.SelectionColor = ColorDialog.Red; torichTextBox1.SelectionColor = Color.Red;
I thought it will work but just when I write the same word in the text box that is in the button and click on the button the form freezes.

ASP.NET dynamic creation of text box using asp:Panel and access the text box using FindControl

I am using ASP Panel to dynamically create text boxes on button click. The code i am using is as below,
<asp:TextBox ID="text_number" runat="server"></asp:TextBox>
<asp:Button ID="button1" runat="server" OnClick="button1_Click"></asp:Button>
<asp:Panel ID="mypanel" runat="server"></asp:Panel>
<asp:Button ID="button2" runat="server" OnClick="button2_Click"></asp:Button>
protected void button1_Click(object sender, EventArgs e)
{
int n = Convert.ToInt32(text_number.Text);
for (int i = 0; i < n; i++)
{
TextBox MyTextBox = new TextBox();
MyTextBox.ID = "newtext_" + (i + 1);
mypanel.Controls.Add(MyTextBox);
MyTextBox.CssClass = "textblock";
Literal lit = new Literal();
lit.Text = "<br />";
mypanel.Controls.Add(lit);
}
}
After i click on button1 the text boxes are created and then i enter values to the text boxes and click on button2. When clicking button2 all values from text boxes should be read and stored in list behind C#. The code i am using is as below,
protected void button2_Click(object sender, EventArgs e)
{
int n = Convert.ToInt32(text_number.Text);
for (int i = 0; i < n; i++)
{
TextBox control = (TextBox) FindControl("newtext_" + (i+1));
mylist.Add(control.Text);
}
}
But whenever i click on the button2, all the text boxes i added in the panel disappear from the web page and also i get the null object reference error in the FindControl. I can understand that all the text box controls within the panel gets cleared when pressing the button2 and this is the reason they disappear in web page and also the reason for getting null object error. what is the problem here? is there any other way to dynamically create 'n' text boxes on button click and then fetch the values from them on second button click without any issues like this?
Because you are creating Controls dynamically, you need to recerate them on every Page Load otherwise the Controls and their content will be lost.
What you need to do is store the number of TextBoxes into a Session or ViewState and recreate them.
protected void Page_Load(object sender, EventArgs e)
{
//check if the viewstate exists and if so, build the controls
if (ViewState["boxCount"] != null)
{
buildControls(Convert.ToInt32(ViewState["boxCount"]));
}
}
protected void button1_Click(object sender, EventArgs e)
{
//adding controls moved outside the button click in it's own method
try
{
buildControls(Convert.ToInt32(text_number.Text));
}
catch
{
}
}
protected void button2_Click(object sender, EventArgs e)
{
//check if the viewstate exists and if so, build the controls
if (ViewState["boxCount"] != null)
{
int n = Convert.ToInt32(ViewState["boxCount"]);
//loop al controls count stored in the viewstate
for (int i = 0; i < n; i++)
{
TextBox control = FindControl("newtext_" + i) as TextBox;
mylist.Add(control.Text);
}
}
}
private void buildControls(int n)
{
//clear the panel of old controls
mypanel.Controls.Clear();
for (int i = 0; i < n; i++)
{
TextBox MyTextBox = new TextBox();
MyTextBox.ID = "newtext_" + i;
MyTextBox.CssClass = "textblock";
mypanel.Controls.Add(MyTextBox);
Literal lit = new Literal();
lit.Text = "<br />";
mypanel.Controls.Add(lit);
}
//save the control count into a viewstate
ViewState["boxCount"] = n;
}

Can't count words from richtextbox to label?

I'm not sure what's wrong here, but i'm trying to count words in a richtext box, and display that with a label.
I put a richtextbox in a tab control so I can have a tabbed text box. Which seems to make this a lot harder then it should
also this isn't the whole program, I took the parts relating to the richtextbox and word counter
Any help is appreciated :)
public RichTab()
{
InitializeComponent();
TabPage tp = new TabPage("Document");
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
tp.Controls.Add(rtb);
tabControl1.TabPages.Add(tp);
WordCount();
}
public RichTextBox RTTB()
{
RichTextBox rtb = null;
TabPage tp = tabControl1.SelectedTab;
if (tp != null)
{
rtb = tp.Controls[0] as RichTextBox;
}
return rtb;
}
private void WordCount()
{
MatchCollection wordColl = Regex.Matches(RTTB().Text, #"[\W]+");
label2.Text = wordColl.Count.ToString();
}
I would probably just wire up the TextChanged event of the RichTextBox, and count the words there:
rtb.TextChanged += rtb_TextChanged;
Then count the words (using Giorgio Minardi's regex):
private void rtb_TextChanged(object sender, EventArgs e) {
label2.Text = Regex.Matches(((RichTextBox)sender).Text, #"[\S]+").Count.ToString();
}
What's the actual issue ?
Here's a simple routine to count words:
[Test]
public void CountWords()
{
const string sample = "How you doing today ?";
MatchCollection collection = Regex.Matches(sample, #"[\S]+");
var numberOfWords = collection.Count;
//numberOfWords is 5
Assert.IsTrue(numberOfWords == 5);
}

C#: How do you edit items and subitems in a listview?

How do you edit items and subitems in a listview? Let's say I have a listview with 3 columns,and subitems,
Car Brand | Car Name | Car Year
Ford | Mustang | 2000
Dodge | Charger | 2007
How would I Add items like that to listview and how would I edit let's say the Car Name on which ever row by index[] if I needed to edit at runtime at firing of an event?
If you're looking for "in-place" editing of a ListView's contents (specifically the subitems of a ListView in details view mode), you'll need to implement this yourself, or use a third-party control.
By default, the best you can achieve with a "standard" ListView is to set it's LabelEdit property to true to allow the user to edit the text of the first column of the ListView (assuming you want to allow a free-format text edit).
Some examples (including full source-code) of customized ListView's that allow "in-place" editing of sub-items are:
C# Editable ListView
In-place editing of ListView subitems
I use a hidden textbox to edit all the listview items/subitems. The only problem is that the textbox needs to disappear as soon as any event takes place outside the textbox and the listview doesn't trigger the scroll event so if you scroll the listview the textbox will still be visible.
To bypass this problem I created the Scroll event with this overrided listview.
Here is my code, I constantly reuse it so it might be help for someone:
ListViewItem.ListViewSubItem SelectedLSI;
private void listView2_MouseUp(object sender, MouseEventArgs e)
{
ListViewHitTestInfo i = listView2.HitTest(e.X, e.Y);
SelectedLSI = i.SubItem;
if (SelectedLSI == null)
return;
int border = 0;
switch (listView2.BorderStyle)
{
case BorderStyle.FixedSingle:
border = 1;
break;
case BorderStyle.Fixed3D:
border = 2;
break;
}
int CellWidth = SelectedLSI.Bounds.Width;
int CellHeight = SelectedLSI.Bounds.Height;
int CellLeft = border + listView2.Left + i.SubItem.Bounds.Left;
int CellTop =listView2.Top + i.SubItem.Bounds.Top;
// First Column
if (i.SubItem == i.Item.SubItems[0])
CellWidth = listView2.Columns[0].Width;
TxtEdit.Location = new Point(CellLeft, CellTop);
TxtEdit.Size = new Size(CellWidth, CellHeight);
TxtEdit.Visible = true;
TxtEdit.BringToFront();
TxtEdit.Text = i.SubItem.Text;
TxtEdit.Select();
TxtEdit.SelectAll();
}
private void listView2_MouseDown(object sender, MouseEventArgs e)
{
HideTextEditor();
}
private void listView2_Scroll(object sender, EventArgs e)
{
HideTextEditor();
}
private void TxtEdit_Leave(object sender, EventArgs e)
{
HideTextEditor();
}
private void TxtEdit_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
HideTextEditor();
}
private void HideTextEditor()
{
TxtEdit.Visible = false;
if (SelectedLSI != null)
SelectedLSI.Text = TxtEdit.Text;
SelectedLSI = null;
TxtEdit.Text = "";
}
Click the items in the list view.
Add a button that will edit the selected items.
Add the code
try
{
LSTDEDUCTION.SelectedItems[0].SubItems[1].Text = txtcarName.Text;
LSTDEDUCTION.SelectedItems[0].SubItems[0].Text = txtcarBrand.Text;
LSTDEDUCTION.SelectedItems[0].SubItems[2].Text = txtCarName.Text;
}
catch{}
Sorry, don't have enough rep, or would have commented on CraigTP's answer.
I found the solution from the 1st link - C# Editable ListView, quite easy to use. The general idea is to:
identify the SubItem that was selected and overlay a TextBox with the SubItem's text over the SubItem
give this TextBox focus
change SubItem's text to that of TextBox's when TextBox loses focus
What a workaround for a seemingly simple operation :-|
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
li = listView1.GetItemAt(e.X, e.Y);
X = e.X;
Y = e.Y;
}
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
int nStart = X;
int spos = 0;
int epos = listView1.Columns[1].Width;
for (int i = 0; i < listView1.Columns.Count; i++)
{
if (nStart > spos && nStart < epos)
{
subItemSelected = i;
break;
}
spos = epos;
epos += listView1.Columns[i].Width;
}
li.SubItems[subItemSelected].Text = "9";
}

Categories

Resources