How to remove a specified characters in labels or controls? - c#

I want to change texts every time that user click on a button. So, if the user clicked on specified button the "✓ " characters will be added to that button's text. also, if the user clicked on another button, That characters will be removed from the specified button's text.
I tried the first code below to add the "✓ " characters and it worked well without problems. Then I tried to remove that characters by replacing + into -
and I got problems. I need now a code to remove that characters from main text and thanks for helping me.
English_Language.Text = "✓ " + English_Language.Text;
//-----------
English_Language.Text = English_Language.Text - "✓ ";

In order to remove a character from a string you cannot just subtract. That is undefined, if there is more than one, what will happen? So you must use one of the methods on the string class in to get the result you want. If you only have one of those characters, or you want them all gone then you can use Replace, otherwise, you can use SubString as follows.
English_Language.Text = English_Language.Text.SubString(1);
Using SubString in this way will skip the first character seeing as that's where you are placing the character.

Related

How can I know the index of duplicated text from a selectedtext? [duplicate]

Ok, I'm trying to do something a little specific here. I want to get the location of the selected text in a textbox.
To elaborate- I can use location to select text. If I have a textBox1 I could do:
textBox1.SelectionStart = 1;
textBox1.SelectionLength = 4;
That would start at the second letter and select 4 letters.
What I want to do is the opposite: when the user selects text, I want to find out what the start is and what the length is (or what the start is and what the end is. Either will work).
I thought about just searching the string for the selectedtext (textBox1.SelectedText). The problem comes if it is a common word or a string that is used multiple times. For instance.
This is a cat. This is a cat. This is a cat.
If they select the second sentence, using SelectedText to search the string for that specific sentence does me no good. It could be either of the 3.
So, my question is: When the user clicks a button, how do I determine the exact elements that are selected by the user, so that I can later manipulate those specific elements? Important to note the later part- I likely will not only want to manipulate the text when the button is pressed. I will also want to manipulate it later, at a time when the text may no longer be highlighted. This means I'll want to store SOMETHING to tell me what specific parts of the sentence I'm dealing with. If that solution isn't viable, is there a solution you can think of where, in the above "this is a cat" example, the user could select the second sentence, hit a button, and then later I know which sentence was selected when he hit that button?
According to the documentation, SelectionStart and SelectionLength can be both set and read. Just use those.
You dont even need to know the position of selected text to manipulate them, to edit the text that you have selected in the text you can simple set the SelectedText property to the new edited value.
// if textBox1.text = "Hello World World"; with first "World" selected
textBox1.SelectedText = textBox1.SelectedText.Replace("World", "Raj");
// then it becomes "Hello Raj World"

horizontal tab escape not working in string.Format [duplicate]

This should be very simple.
I have a Label control on my Form and I am trying to put a tab character between text
Label.Text = "Is there a\ttab";
The output is "Is there atab";
What am I doing wrong?
Tab is actually a non-printing character—or rather, a control character. What it does is entirely dependent on the application. What exactly do you expect? 8 spaces? 4 spaces? As many spaces as needed to get to a multiple of 8 columns? Indentation of the following text by one cm?
To put it short: The Label control doesn't support tabs. Actually, Label just uses normal graphics routines for rendering its text and how should they know what you intend to do with your tab character?
If you need to display that character as a number of spaces, then you should replace it by that number of spaces.
I wanted to add tabs ("\t") to a dropdown list of items. The items have a ToString method that gives about 3 words concatenated together. They did not line up. For example:
1-I 45
123-AB 511
123456-MMM 611
A long list like this is hard to read. So I used string.Format like this:
string.Format("{0,6}-{1,-4} {2}",id,name,num);
The number after the comma will right align/pad if positive and left align/pad if negative.
Then I changed my font in the Combobox to be monospaced, like Courier New, and you get something like this:
1-I 45
123-AB 511
123456-MMM 611
That is much easier for a user to read.
Nothing, windows forms labels are very limited in functionality and don't support the \t character.
A (slightly awkward) alternative might be:
label1.Text = "test\ting\t123".Replace("\t"," ");
Old thread, but since none of the answers seemed to work for me, I will go ahead and throw in my 2 cents. I could not get a "\t" or even use manual spaces to add spacing to the label. What I ended up doing was using alt code alt-255 5 times. This worked like a charm. Gotta love total hacks...
Right, to insert a tab, just add the spaces desired.
If you want to offset the next by a specified length, you could try
int offset_text = 20;
label1.Text = "Is there a".PadRight(offset_text)+"Tab";
label2.Text = "More Text".PadRight(offset_text)+"Too";
Just use a literal string and you should be good to go...
label1.Text = #"Test for Tab";
Where that big space is where I actually hit tab three times...hope this helps
I had the same problem. A textbox does instead a label accept Tabs. So if you change the label in a textbox, the pro
Just click in the arrow at the right of the Text property of the label (click in the Text property content and the drop-down-arrow will show up). A box for text-editing will open, and in that box you can use Enter, Tab, and so on.

Fill word Content Control using C# issue

I have word contains content control like
I want to write many lines on it so in C# I tried many ways :
using EnviromentNewLine .
\n
Example:
for (int i = 0; i < items.Count; i++)
{
items[i] = string.Format("{0}{1}{2}{3}", (i + 1).ToString(CultureInfo.InvariantCulture), ". ", items[i], "<br />");
}
but I still get the word content control on one line , any idea how to fix it
The actual problem is, that the plain text content control does not support multiple paragraphs, it is only possibly to insert manual line breaks (which you normally do by pressing Shift+Return). Within the plain text content control, Word automatically replaces Return by Shift+Return as you can see when switching on the formatting symbols.
You basically have two options to solve your issue:
Change the content control to a rich text content control. That way you will be able to insert arbitrary content, also new line characters (i.e. multiple actual Word paragraphs)
Insert a vertical tab, i.e. ASCII character 11 or \B instead of a new line. This will insert the manual line break required by the plain text content control.
Try
\r
MSDN 2.4.4.4 Character literals

textboxcell on datagridview allow only one char by default

I have a datagridview with textboxcell. When I enter the cell and type a string it only shows the last char of the string. It replaces the last char with the new char.
In order to write a complete string I need to click on other cell of any other part in the form, and then double click the textbox cell again and I can write a whole string.
Does anyone have an idea why is that?
I recently ran into this behavior due to executing datagridview1.CommitEdit(DataGridViewDataErrorContexts.Commit) in the grid's CurrentCellDirtyStateChanged event handler. Any time I typed, it would only display one character, and I'd have to click out of the cell and back in to add additional text beyond the 1st character.
In my case, I'd added this code a while back to solve a problem that no longer existed, and had forgotten to remove it. Removing it resolved this behavior.

Winforms Label Text property not displaying \t tab character

This should be very simple.
I have a Label control on my Form and I am trying to put a tab character between text
Label.Text = "Is there a\ttab";
The output is "Is there atab";
What am I doing wrong?
Tab is actually a non-printing character—or rather, a control character. What it does is entirely dependent on the application. What exactly do you expect? 8 spaces? 4 spaces? As many spaces as needed to get to a multiple of 8 columns? Indentation of the following text by one cm?
To put it short: The Label control doesn't support tabs. Actually, Label just uses normal graphics routines for rendering its text and how should they know what you intend to do with your tab character?
If you need to display that character as a number of spaces, then you should replace it by that number of spaces.
I wanted to add tabs ("\t") to a dropdown list of items. The items have a ToString method that gives about 3 words concatenated together. They did not line up. For example:
1-I 45
123-AB 511
123456-MMM 611
A long list like this is hard to read. So I used string.Format like this:
string.Format("{0,6}-{1,-4} {2}",id,name,num);
The number after the comma will right align/pad if positive and left align/pad if negative.
Then I changed my font in the Combobox to be monospaced, like Courier New, and you get something like this:
1-I 45
123-AB 511
123456-MMM 611
That is much easier for a user to read.
Nothing, windows forms labels are very limited in functionality and don't support the \t character.
A (slightly awkward) alternative might be:
label1.Text = "test\ting\t123".Replace("\t"," ");
Old thread, but since none of the answers seemed to work for me, I will go ahead and throw in my 2 cents. I could not get a "\t" or even use manual spaces to add spacing to the label. What I ended up doing was using alt code alt-255 5 times. This worked like a charm. Gotta love total hacks...
Right, to insert a tab, just add the spaces desired.
If you want to offset the next by a specified length, you could try
int offset_text = 20;
label1.Text = "Is there a".PadRight(offset_text)+"Tab";
label2.Text = "More Text".PadRight(offset_text)+"Too";
Just use a literal string and you should be good to go...
label1.Text = #"Test for Tab";
Where that big space is where I actually hit tab three times...hope this helps
I had the same problem. A textbox does instead a label accept Tabs. So if you change the label in a textbox, the pro
Just click in the arrow at the right of the Text property of the label (click in the Text property content and the drop-down-arrow will show up). A box for text-editing will open, and in that box you can use Enter, Tab, and so on.

Categories

Resources