Accused to be not detailed enough, now I'll try to be detailed and descriptive as possible.
So, I have web application with textbox. Next to it I made label for preview, so when I click Submit, it sends text to remote application, when I press preview, it sends text to label for preview.
Remote application have specific text formatting, so When I type A text becomes red, B, text becomes green, and so on. When I wan't to change color, I type this sign for new color, I can't close color (like in HTML).
For new line, I also have a sign, it's #r
I also have sign for double width.
Main problem is to do following things:
Count characters and spaces, and break line on last space before 30th sign (replace space with #r (new row sign), so space wouldn't be taken to next row).
None of the #x(x is some letter) should be counted, as they just do text formatting.
While counting characters, when it comes to %Y (sign for double letter's width), it should count double those letters and spaces until it comes to #Z or #W, where normal letters with starts again, and from that point, count again one by one letter.
If anything is not clear enough, please ask.
You can access individual characters in a string that you get from the TextBox, and iterate over them:
StringBuilder newString = new StringBuilder();
for (int index = 0; index < textBox.Text.Length; index++)
{
char myChar = textBox.Text[index];
... apply your logic as need one char at a time...
... you might need extra variables outside of the loop to
... keep track of your last space before 30th character, etc.
newString.Append(...);
}
string result = newString.ToString();
Related
I am currently struggling with finding a proper solution for following problem:
Imagine you have an MS Word (2019) document containing a table consisting of two columns 'A' and 'B'. Text in column B shall always be aligned vertically with the last text line of the text in column 'A'. OK, easy task if the row is on one page only: simply set vertical text alignment of 'B' to 'bottom' and you're done.
Now the BUT: If the the text in 'A' gets too long, Word shall break the cell across pages (hat's a mandatory requirement!). But even if the long text in column 'A' now ends on page 2, the short text in 'B' will be placed on bottom of page 1!
A possible workaround would be to fill up the 'B' with multiple LFs and/or LF+CRs, but I hope someone can explain how a proper solution could look like?!
Thanks in advance!
Desired text position
Wrong text position if row is split across 2 pages
I have a WinForms DataGrid. One column takes formulas from user (like "1+55-var+4"). The content of this column could be very long so I enabled text wrapping in that column.
myGridView.Columns(5).DefaultCellStyle.WrapMode = DataGridViewTriState.True
myGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
myGridView.Columns(5).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
That behaviour works perfectly for common text columns because the wrapping recognizes white spaces for line breaking.
But my fomula content does not have white spaces. I want that line breaks happen on special character (that means +, -, /, *) or if the column space overlaps.
Has anyone an idea? Thanks.
Assuming you don't need the text to wrap while the text box is being edited, the most straightforward solution would be to add and remove return characters yourself every time the TextBox loses focus or is resized. (Like G3nt_M3caj said) However, depending on the font figuring out exactly when and where to put returns might get messy.
Alternatively, everytime the TextBox loses focus find all of the special characters in the text and append & prepend the character with a Zero Width Space (U+200B). And everytime the textbox gains focus find and remove all Zero Width Spaces. You then don't need to even worry about the TextBox resizing.
If your making a digital button calculator, you additionally need to add the Zero Width Space before and after every special character like so:
private void ButtonClick(object sender, EventArgs e)
{ string key = ((Button)sender).Text; //A string containing the character that has been clicked.
if ( '0' <= key[0] && key[0] <= '9' ) textBox1.AppendText(key);
else textBox1.AppendText('\u200B'+ key + '\u200B');
}
On a final note, whatever you do make sure the text with Zero Width Spaces cannot be copied. It would be a nightmare to debug code that somehow got a Zero Width character in it.
EDIT: I just realized you never said you were using a TextBox inside your DataGrid. Well I think it will work either way.
I'm creating a flat file reader (simple hex editor if you will) in c# using RichTextBoxes. One RTB shows the hex values and another shows the ASCII values.
My plan is to show one 'record' per line. So, if there are 10 records I want to look at with a length of 1000, there will be 10 lines of 1000 characters per row in the ASCII and the hex side will have a length of 3000.
I dynamically set the rtb.RightMargin property to the length of one record.
The problem I'm running into is when the records are incredibly long, over 3500 chars for the ascii side making the hex side very large, I find that the text starts to disappear in the middle and end of the records when the right margin becomes too large. So for example:
hexRtb.RightMargin = 7500 //This is because it's triple the size of the ascii text.
In the hex rtb, it'll show the first parts of the text until I start scrolling towards the middle where all text stops showing completely. If I manage to click on these empty parts of the record, text will show up, but then disappear again after scrolling away.
I cannot figure out what's going on. This only seems to happen when the RightMargin is set to an incredibly large number. Smaller numbers, all text will shows without a problem.
Anyone ever encountered something like this?
Here's a code sample if it helps.
int asciiRecordLength = mHexReader.RecordSize;
int hexRecordLength = mHexReader.RecordSize * HexByte; //This is to convert the ascii record length to a hex record length
asciiTextBox.RightMargin = TextRenderer.MeasureText(mHexReader.GetAsciiValues().Substring(0, asciiRecordLength), asciiTextBox.Font).Width;
hexTextBox.RightMargin = TextRenderer.MeasureText(mHexReader.GetHexValues().Substring(0, hexRecordLength), hexTextBox.Font).Width;
//Populate text boxes
hexTextBox.Text += mHexReader.GetHexValues(); //This gets all of the records to be read
asciiTextBox.Text += mHexReader.GetAsciiValues();
For those who've run into the same problem I have, I found a workaround to this problem.
Instead of using RichTextBox, I found another control you can download called ScintillaNET. So far, I've been able to switch over most of my code to using this control without any problems. Some of the functions are slightly different, such as, instead of hextTextBox.Clear() it's hexTextBox.ResetText(), or hexTextBox.SelectionStart is now hexTextBox.Selection.Start.
Minor differences but this control fixes the issue with the pixels disappearing when the record length becomes very large.
The code can be downloaded from: http://scintillanet.codeplex.com/
Hope this helps anyone else who has run into the same problem I have.
In Visual Studio 2010, I have multiple lines of text to be commented:
A
B
C
Using CTRL+E+C to comment out multiple lines, I get
//A
//B
//C
I would like to have a space (or indent) between // and A, hence // A instead of //A.
However, after I group tab a block of text and indent it, CTRL+E+C no longer comments out the selected text.
How to group comment and get the following:
// A
// B
// C
Pressing Ctrl+K+C or Ctrl+E+C After selecting the lines you want to comment will not give space after slashes. you can use multiline select to provide space as suggested by Habib
Perhaps, you can use /* before the lines you want to comment and after */ in that case you might not need to provide spaces.
/*
First Line to Comment
Second Line to Comment
Third Line to Comment
*/
One way to do it would be:
Select the text, Press CTRL + K, C to comment (CTRL+E+C )
Move the cursor to the first line after the delimiter // and before the Code text.
Press Alt + Shift and use arrow keys to make selection. (Remember to make line selection(using down, up arrow keys), not the text selection - See Box Selection and Multi line editing)
Once the selection is done, press space bar to enter a single space.
Notice the vertical blue line in the below image( that will appear once the selection is made, then you can insert any number of characters in between them)
I couldn't find a direct way to do that. The interesting thing is that it is mentioned in the C# Coding Conventions (C# Programming Guide) under Commenting Conventions.
Insert one space between the comment delimiter (//) and the comment
text
But the default implementation of commenting in visual studio doesn't insert any space
I was able to achieve the desired result by using Alt + Shift + up/down and then typing the desired comment characters and additional character.
You can customize every short cut operation according to your habbit.
Just go to Tools > Options > Environment > Keyboard > Find the action you want to set key board short-cut and change according to keyboard habbit.
Might just be for Visual Studio '15, if you right-click on source code, there's an option for insert comment
This puts summary tags around your comment section, but it does give the indentation that you want.
I am custom drawing a text box and now I am implementing the part where the user can click on a line and have the cursor move to where he clicked.
I know how to get the row of text he clicked on because the character height is constant across all fonts, but not the column, because I'm not sure how to say "get me all the text that can be drawn before this amount of pixels," and because character width is not consistent unless you're using a fixed-width font, which is not a guarantee.
So I have the point from which I'm drawing the string (0) then I have the point that the user clicked. How do I get the string index of the character they clicked on?
Extra info: I am drawing the text by storing the lines in a List then iterating the list and using Graphics.DrawString on each line.
There is no simple method to find the character at a pixel.
However you can find the pixels that a string will fill. Use the Graphics.MeasureCharacterRanges method. You can perform a binary search on your string until you find the string where MeasureCharacterRanges returns your cursor position.
Note: You might see the Graphics.MeasureString method and be tempted to use that. DON'T! That method doesn't return accurate measurements. I can't remember why, but you will do your head in if you try!