Add find/replace functionality to a text box?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 3911 Views - Last Post: 18 June 2016 - 10:09 AM Rate Topic: -----

#1 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Add find/replace functionality to a text box?

Posted 26 May 2016 - 11:02 AM

Hi

Is it possible to add find/replace functionality to a text box in C# WPF MVVM and if so how?

I've tried googling but can't find anything helpful.

Thank You
Is This A Good Question/Topic? 1
  • +

Replies To: Add find/replace functionality to a text box?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,193
  • Joined: 12-June 08

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 11:06 AM

I would imagine the textbox value is still a string datatype, right? The string namespace still has a 'replace' function in it.
Was This Post Helpful? 0
  • +
  • -

#3 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 11:08 AM

But that wouldn't highlight or scroll to the text in the text box
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,193
  • Joined: 12-June 08

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 11:41 AM

.. okay so that wasn't stated. There are options to highlight words, scroll, etc. Not in one nice package, but nothing a bit of elbow grease won't solve when putting together the parts.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 11:45 AM

Find within a TextBox or RichTextBox? I found plenty of examples searching "wpf richtextbox find", for example. A similar search for "wpf textbox find" will probably discover results, although it is perhaps more usual to use an RTB.
Was This Post Helpful? 1
  • +
  • -

#6 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 12:04 PM

What's the difference between a textbox and a richtextbox?

I've never come across a richtextbox before.

And sorry for not stating that but I want something like the find + replace functionality in visual studio so quickly find word or phrase and go directly to it where it is highlighted and preview it before you replace it and even find and replace multiple instances.

I can already imagine that this is going to be very difficult but I've had a request to add it as a feature to my app so I want to do my best to add it.

This post has been edited by danbywinby: 26 May 2016 - 12:07 PM

Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 12:13 PM

A RichTextBox holds "Rich Text", a fairly simple text format that encodes formatting information in the text, like font and color.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 12:20 PM

Difference between Textbox and RichTextbox wpf?

Essentially, a RichTextBox is a more complex object, allowing rich content, than a TextBox. (An important technical difference is that an RTB doesn't support DataBinding.)

View PostCurtis Rutland, on 26 May 2016 - 07:13 PM, said:

A RichTextBox holds "Rich Text", a fairly simple text format that encodes formatting information in the text, like font and color.

The RTB can include images and tables as well. For example,
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            // Add some text
            Paragraph para = new Paragraph();
            para.Inlines.Add(new Run("Image "));
            fld.Blocks.Add(para);

            // Add an image
            para = new Paragraph();    
            BitmapImage bitmap = new BitmapImage(new Uri(<Uri to your image>));
            Image image = new Image();
            image.Source = bitmap;
            Figure figure = new Figure();
            figure.Width = new FigureLength(200);
            BlockUIContainer container = new BlockUIContainer(image);
            figure.Blocks.Add(container);
            para.Inlines.Add(figure);
            fld.Blocks.Add(para);

        }

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 12:46 PM

Let me help set expectations here: If you want to do Notepad style find and replace, this is actually relatively easy to do and is not very complex. If you want to do Visual Studio (or most other programmer targeted text editor) find and replace, this is will be a much more complex task and will require you to maintain multiple data structures so that undo will work correctly.

My first question to the stakeholder would be: "If you want the VS style find and replace, what value are you getting from it that the Notepad style find and replace is not getting you, other than the really nice UI experience?"
Was This Post Helpful? 0
  • +
  • -

#10 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 01:48 PM

Well if I can't bind data with a richtextbox then that cuts that idea out because currently I make text display in the text box using data binding.

What would this simple notepad like find and replace allow me to do exactly and what would its limitations be?
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 04:00 PM

Give it a try. Notepad comes with every Windows installation.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 05:24 PM

Danbywinby: Google "WPF Text Editor Tutorial" - There are a hundred of them. Making text editor in WPF, complete with font selection, coloring, find and replace... Is like a staple tutorial for WPF noobies. Everyone writes one.

This is the first one that came up when I did a search and its looks pretty decent.
http://www.wpf-tutor...ch-text-editor/
Was This Post Helpful? 0
  • +
  • -

#13 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: Add find/replace functionality to a text box?

Posted 26 May 2016 - 10:46 PM

I can't use a richtextbox if it does support binding and that tutorial doesn't do find and replace.
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: Add find/replace functionality to a text box?

Posted 27 May 2016 - 12:07 AM

So what has your searching uncovered?

TextBox Class

The TextBox has a Select() method, strings have a Replace() method.

how to highlight/select text in a wpf textbox without focus?

etc., etc.
Was This Post Helpful? 0
  • +
  • -

#15 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: Add find/replace functionality to a text box?

Posted 27 May 2016 - 05:40 AM

The only stuff I have found online relates to a richtextbox which I can't use because I need to be able to use databinding because I'm using Mvvm.

Functionality like notepad should be fine because I tried it out myself earlier and it will highlight the text that it has found and it can go to the next or previous occurrence and it will let you replace each occurrence one by one but still give you an option for replace all.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3