Logic of RichTextBox.Paste() method - c#

What is the internal logic of RichTextBox.Paste() method which pastes data from clipboard to RichTextBox.
Actually I want to add text to RichTextBox at the location where cursor is there on button click. But when I add text the added text is either selected after addition or the cursoe location is at the start of added text.
Any solution for this ?

I think you don't need to know the .NET implementation of the control. It is "black box" for you and me. But, you can do unselect and move cursor to the end of text (this 2 things disturb you, not?)
Moving cursor to position 0 (start):
richTextBox1.Select(0, 0);
Moving to the end:
richTextBox1.Select(richTextBox1.Text.Length, 0);
Select all text:
richTextBox1.Select(0, richTextBox1.Text.Length);
Unselect all and move to the end:
richTextBox1.Select(richTextBox1.Text.Length, richTextBox1.Text.Length);

Related

How to stop text in a button control from breaking the word up onto multiple lines in Winforms?

I have a Windows Forms project that sets button text based on variables. The font is quite large for readability, but it means that some words are too long to fit in the button nicely, and the last few characters break onto multiple lines.
Is there an algorithm or property I can use to resize the text inside the button to prevent this from happening?
I don't want to try to fit the text on one line, I just want to stop the text from splitting words up.
If you really want to resize the font, you could try to detect when the text has reached the bounds of the button and then reduce the font size. Here's some code to get you started, but I'm not sure how to determine the length where text will start wrapping based on the button width. Perhaps there's some other property for that which I couldn't find.
// Get some info from the button control
var graphics = button1.CreateGraphics();
var font = button1.Font;
var textWidth = graphics.MeasureString(button1.Text, font).Width;
// Not sure what the magic formula is to determine when the word wraps
if (textWidth > button1.Width - 25)
{
// Assign a new font based on the old one, but smaller
button1.Font = new Font(font.FontFamily, font.Size - 1,
font.Style, GraphicsUnit.Pixel);
}
Set the AutoSize of the button to "false":
Button1.AutoSize = "false"
white-space: -moz-pre-wrap; /* Firefox */
white-space: -o-pre-wrap; /* Opera */
white-space: pre-wrap; /* Chrome */
word-wrap: break-word; /* IE */
I am not sure, but there should not be any algorithm which can help you here (even AI algorithms). Constraint is you want to keep font size and button size same. However you can do following which could solve your problem.
Use abbreviated text. e.g. First few words(characters)/ Initials of text. Generally contacts list on android mobiles, skype, or any other communicator shows initials if avatar is not set.
Show some part of text on button and when mouse hovers then show full text by resizing the button or on tooltip.
Add some space on form which will show full text when mouse is hovered on any button on it. So a common label on form which will show full button text when mouse is hovered on any button.
If texts are fixed/static then introduce some icons on button instead of text and show full text as tooltip.
~Nilesh

Windows Forms Textbox: Selection and Caret position

After setting the SelectionStart and SelectionLength, the Caret is at the end of the selection.
How can I move the Caret to the beginning of the selection?
I now tried this: The question is not answered by the other threads
SelectionStart = selStart; // set the desired position
SelectionLength = 0;
MyLibs.WindowsDll.POINT point;
MyLibs.WindowsDll.GetCaretPos(out point); // save the position
SelectionLength = restOfWord.Length; // extend the selection
MyLibs.WindowsDll.SetCaretPos(point.X, point.Y); // restore caret position
GetCaretPos and SetCaretPos are working somehow, but not as it should.
Using the above code snippet, the caret is indeed blinking at the beginning of the selection.
But ... then I hit backspace or cursor left/right, the caret still behaves like it is still at the end of the selection.
Unfortunately, you can not do this using managed code like C#,
but to achieve this goal you may use C++, SetFocus() function,
for example:
//use SetFocus()
pEdit->SetFocus()
//place caret at the end:
pEdit->SendMessage (WM_KEYDOWN,VK_END,0);
//simulate keyup:
m_Edit->SendMessage (WM_KEYUP,VK_END,0);
//send the key code:
m_Edit->SendMessage(WM_CHAR,'A',0);
Have a look to MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646312(v=vs.85).aspx
You may use also CEdit::SetSel to accomplish that:
CEdit* e = (CEdit*)GetDlgItem(IDC_EDIT1);
e->SetFocus();
e->SetSel(0,-1); // move cursor at the end
e->SetSel(-1); // remove selection
See details in Stackoverflow question: CEdit control MFC, placing cursor to end of string after SetWindowText
UPDATED:
If you need to set position to the beginning you can reset the Caret position to start before displaying the text using:
SetSel (0,0);
You can not move the caret to the beginning while keeping the text selected using basic C#/C++ functions, however you can override basic "Caret" logic using C++ or/and Assembler.

Is it possible to get which character of text is clicked on C# form?

I am trying to get which of the * is clicked in text of label as in picture.
NOTE : Number of * is not known at first.User inputs it.So the position of mouse click is not useful.
Answer is :
X of first *'s location is known, equals = 10 .The asterisks are spaced equally,distance between two * is ~15 pixel .
When user clicks on one of asterisks , index of asterisks can be calculated as the code below
private void label4_MouseDoubleClick(object sender, MouseEventArgs e)
{
double t = (e.X - 10) / 15.0;
int indexOfClickedAsterisk=(int)Math.Round(t) + 1;
}
It depends. If you are willing to display the text in a TextBox instead of Label (which is what it looks like you're using now), then you can use the GetCharIndexFromPosition method. Just make sure you specify the Point in client coordinates (comes for free if you are handling mouse clicks in the Label control itself).
Note that you can set the TextBox as ReadOnly, assuming you don't want the user actually modifying the text. They will still be able to select the text though.
If you need an actual Label control (e.g. you don't even want selectable text and the grayed-out appearance of a disabled TextBox isn't appropriate), then you'll have to write your own (or find one online that someone else wrote already), as the built-in one doesn't have that functionality. It would not be too difficult. You can use the TextRenderer methods to determine where each character is laid out when drawn and then use that information to correlate character positions with mouse clicks.

How to detect a tag applied to a text in Gtk# TextView's?

I am programming a very simple text editor component for a personal project. It supports bold, italic, underline, righ, center, left and fill justification... so far so god. Using TextControl.Buffer.ApplyTag( tag, start, end ), you press the button once, and the selected text portion becomes bold or whatever. If you press the button again, the bold format should disappear.
The problem is to detect tags and then remove them, so the format disappears. I know I can use TextControl.Buffer.RemoveAllTags( start, end ); in order to remove all tags, and this.TextControl.Buffer.RemoveTag( tag, start, end ); in order to remove a specific tag. But I am looking for a function such as:
TextTag[] GetTags(TextIter start, TextIter end);
... so I can detect which tags have been applied to a specific selection, but I am not finding that function in the documentation of Gtk.TextView nor anywhere else. Does anybody know about this?
You want the TextIter.Tags read-only property. It gives you a list of tags the apply to a single point, not a range. A range is more complicated, since a tag might apply to only half of a range. You'll probably have to write that yourself if you want it.

How Do I scroll to a string in a Rich Text Box

I ahve a RTB with sufficent text that scrolling is needed
user enters a string and I highlight all occurrences using a combination of Find and Select which is great but now I want the ability for a user to press Next and the next higlighted instance should be visible say 2at /3rd of the bounding rectangle ( I would even settle for at the top of the bound.
How do I scroll to an index basically ( I am caching the indices as I find and markup )
oh also this is C# Winforms .NET 2.0
Set the selection start to the next location, and then use ScrollToCaret to scroll to that location in the rich text box.
rText1.SelectionStart = i
rText1.ScrollToCaret()
private void myrichTextBox_TextChanged(object sender, EventArgs e)
{
myrichTextBox.SelectionStart = myrichTextBox.Text.Length; //Set the current caret position at the end
myrichTextBox.ScrollToCaret(); //Now scroll it automatically
}

Categories

Resources