-1

In the top of form1

textBox1.Text = "TextToSearch{}";

First I want to force the user to be able to type only inside the TextToSearch{} between the two { } and if there are more then one:

TextToSearch{},TextToSearch{} then the user will be able to type only in between the two { } in both places. In the rest of the TextBox area he will not be able to type.

I want to use later this TextToSearch{} as separator between multiple texts searching. For example:

TextToSearch{hello}

It will search for the word hello

And:

TextToSearch{hello},TextToSearch{hi}

Now it should search for hello and hi not hellohi but separate hello and hi. So I also need to parse this texts to string array. Before I used just , to separate.

string[] values = textBox1.Text.Split(',');

hello,hi 

It was easy. But now the texts are in TextToSearch{} between the { } and also separate this by , for example:

TextToSearch{hello},TextToSearch{hi}

So I need to take out the hello and hi and put them in the values array.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398

2 Answers2

1

Instead of trying to get such functionality from the TextBox - which can not simply be achieved - I recommend you to focus on the main problem which you should solve:

Let the user search some phrases which each phrase may be a single word or multiple word.

Option 1 - As an option you can use , to separate search phrases.

string input = this.textBox1.Text;
var parts = input.Split(',').ToList();
parts.ForEach(x => MessageBox.Show(x));

input: Split,string,with,white spaces,or,double quotes
parts: Split string with white spaces or double quotes

Option 2 - As another option you can ask the users to separate words by space. Also if they want to have some words together as a search phrase, they can put those words between "". To achieve this, you can use multiple methods. For example:

//using System.Text.RegularExpressions;

string input = this.textBox1.Text;
var parts = Regex.Matches(input, @"[\""].+?[\""]|[^ ]+")
                 .Cast<Match>()
                 .Select(x => x.Value.Trim('"'))
                 .ToList();
parts.ForEach(x => MessageBox.Show(x));

input: Split string with "white spaces" or "double quotes"
parts: Split string with white spaces or double quotes

If none of above options satisfies your requirement, you can use multiple TextBox controls for multiple parts.

Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Reza using ',' to separate i did it already the problem is that what if the user entering a text that contain inside in some places ',' comma ? Thats why i wanted to use this TextToSearch{}. Second you wrote before that if i realy need parts...Is'nt it logic to give theu ser the option to make multiple searching when he make a searching inside a file content ? – דניאל רשת Jul 06 '16 at 09:16
  • 1
    It's completely reasonable for users to have the option to search a single word or a phrase containing multiple words. I shared these options to let you have the feature in your application. Both options are simply applicable. If the user needs to include a special character (`,` in first approach or `"` in second) you can use escape pattern for it, for example you can ask them to use `,,` instead of a `,` in phrase and simply replace it with a special string line `{comma}` or a new generated guid, then split and at last put `,,` instead of the special string. – Reza Aghaei Jul 06 '16 at 11:08
  • 1
    Also for the last option, you can simply ask them to use multiple dynamic textboxes, or even a single textbox and a button, then add the text of textbox to a list after click on button. This way they can write a phrase or a word and add it to the list and then write next phrase or word and so on. at last they can ask you to search using the list, the can also simply remove items from the list by selecting them pressing a remove button. I recommend you to not waste your type any more for such problem, pick one of the solutions and create rest of the application. Hope you find the answer useful. – Reza Aghaei Jul 06 '16 at 11:15
0

Try to use MaskedTextBox as follows.

Plase MaskedTextBox on Form, set its properties:

maskedTextBox.Mask = @"TextToSe\arch{C}"; // C - any non-control character
maskedTextBox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;

Set TextChanged event hanlder:

private void MaskedTextBox_TextChanged(object sender, EventArgs e)
{
    int count = maskedTextBox.Text.Length + 1;

    maskedTextBox.Mask = @"TextToSe\arch{" + new string('C', count) + "}";
}

User-entered text can be obtained from the Text property:

string textToSearch = maskedTextBox.Text;

Imho, it is quite convenient. But only for a single search string.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49