Scroll to the end of a Single-Line Textbox in C# - c#

In regards to single-line textboxes (Multiline property is set to false), is it possible to scroll to the end of the line when the text length exceeds the horizontal size of the box?
I have tried various solutions that work for multi-line boxes, but none of them have worked thus far.
Very similar questions have been asked by several individuals in the past, but it has always regarded Multi-Line textboxes. The questions/solutions that I have come across on SO are the following:
Scroll to bottom of C# TextBox
How do I automatically scroll to the bottom of a multiline text box?
Right now I have the following code (which seemingly does not work):
PathText.Text = "";
PathText.AppendText(BrowseDialog.SelectedPath);
PathText.SelectionStart = PathText.TextLength;
PathText.ScrollToCaret();
PathText.Refresh();
PathText is the textbox in use, and BrowseDialog is a FileDialog.
Any suggestions are greatly appreciated.

You could do something like this:
PathText.Focus();
PathText.Select(PathText.Text.Length, 0);

textBox1.Select(textBox1.Text.Length, 0);
// call focus
textBox1.Focus();
OR
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox1.Focus();

Related

How do I avoid having my Rich Text Box, "scroll bar," freeze up?

My issue is in the .NET framework using C# to create a simple form application that contains a rich text box (RTB) control.
Briefly the issue I am experiencing is that when trying to clear the contents (.Text) of the RTB, the scroll bar doesn't go away. I would like to know if there is anything inherently wrong with the way I am using the RTB. I apologize, but the site will not allow me to post images yet. So if there is a misunderstanding regarding what "doesn't go away" means, please ask!
So first, I write data to the box using the following code snippet:
// append the new message
this.rtb_receive_0.Text += message;
this.rtb_receive_0.SelectionStart = this.rtb_receive_0.Text.Length;
this.rtb_receive_0.ScrollToCaret();
Later on, I clear the RTB contents (RTB.Text) with the following code:
this.rtb_receive_0.Text = String.Empty;
this.rtb_receive_0.Refresh();
In the above code I have attempted to fix my problem with the, "Refresh," method. However it does not seem to be doing the job.
When I clear the RTB contents, the scroll bar does not go away... I noticed that if I grab another window and drag it over the top of the application, that the frozen scroll bar disappears. Also, I can minimize the application, then maximize it again and the bar will disappear. There has to be a way to prevent this frozen scroll bar from happening in the first place though.
Per the answer, here was the fix to stop the bar from freezing up:
this.rtb_receive_0.Text = String.Empty;
this.rtb_receive_0.Clear();
this.rtb_receive_0.ScrollBars = RichTextBoxScrollBars.None;
this.rtb_receive_0.ScrollBars = RichTextBoxScrollBars.Vertical;
this.rtb_receive_0.Refresh();
Have you tried simply just programatically setting the Scrollbars property on the RTB?
myRichTextBox.ScrollBars = RichTextBoxScrollBars.None;
Edit: I think I misinterpreted what you needed. Searching around, I found this similar post on another forum: http://www.vbforums.com/showthread.php?793671-RESOLVED-RichTextBox-Visual-Bug
This user is setting the value of an RTB based on a selection in a list view. When a new value is set and does not require a scrollbar it doesn't re-draw and still shows the bar.
It seems like adding myRichTextBox.Clear(); myRichTextBox.Refresh(); should help. In this case that user is also programatically setting the ScrollBars property as well.
Also, are you able to determine how many lines of text can fit in the RichTextBox before a scrollbar is needed? I suppose that might vary based on system settings on the machine, but you might just be able to programatically check myrtb.Scrollbars = (myrtb.Lines.Length > X) ? Vertical : None; (excuse the psuedo code syntax)
What helped for me was just calling the refresh() method twice. Very ugly, but it does the job.
Hmm, after more thorough testing this ugly fix proved to be not so much of a fix afterall. It helps, but still some glitches.
refresh();
update();
seems like a better solution.
I was having this same problem. I solved it by calling the Invalidate() method which forces the control to repaint.
Me.RichTextBox.Clear()
'Call Invalidate in order to force the RichTextBox to repaint. I do this so that any
'Visible Scroll bars are removed after clearing the Text
Me.RichTextBox.Invalidate()
I tried with Refresh(); Update(); Invalidate();,but it didn't worked for me.
I solved this problem using following three lines :-
RitchTextBox.Clear(); //Clearing text in RichTextBox
RitchTextBox.ScrollBars = RichTextBoxScrollBars.None; //Remove scroll
RitchTextBox.ScrollBars = RichTextBoxScrollBars.Vertical; //Again add scroll
Try those above three lines. It will work 100%.

Any Characters - Masked Text Box [duplicate]

This question already has answers here:
Masked TextBox Input Align Left
(2 answers)
Closed 9 years ago.
I'm writing an app in C# with Visual Studio 2012, and I'm needing to format some input text using MaskedTextBox. The user will type in a folder path to the text box, but since the folder path is relative to another path, I need it to start with ".\", but I do not care how long the path is.
Right now, I have the mask set for the box to \.\\CCCCCCCCCCCCCCCCCC. This works fine except for the fact that when the user clicks into the box, it places the cursor where they click instead of the beginning of the box.
Is there a way to set the mask to still put in the ".\" but not to set any limit on the characters that come after it?
Or is there a way I'm overlooking?
EDIT: More info
So I've tried a couple recommended things, but they don't seem to work. The answer linked here doesn't work well. While I can set it to go to that selection point when I click on the box, it will go there every time you need to click on the box. So you can't select the whole box or edit part of what you typed, which is even worse for usability.
I also tried the method suggested by Adelmo. I made an even handler like so:
public Form1()
{
InitializeComponent();
refreshList();
this.textBoxPrintFolder.GotFocus += new EventHandler(textBoxPrintFolder_GotFocus);
}
private void textBoxPrintFolder_GotFocus(object sender, EventArgs e)
{
this.textBoxPrintFolder.Select(2, 0);
}
This works when tabbing to the box, but apparently clicking on the box doesn't go into the GotFocus event.
I've also tried using the MouseEnter event. While it does work, it takes a few seconds before it will move. Not ideal.
Any help would be greatly appreciated.
Maybe using onFocus event:
You can control cursor position (and selection) by TextBox.SelectionStart and TextBox.SelectionLength properties.
Example if you want move cursor to before 3th character set SelectionStart = 2 and SelectionLength = 0.
http://social.msdn.microsoft.com/Forums/en-US/04362a62-8cbf-4d86-a1bc-2aba8e4978ca/cursor-position-in-textbox
Hope it help you

Why text box text not showing from starting position in c#?

I am really confused about this. Please consider below scenario.
Scenario:
I have a C# Winform app with few Textbox controls. Now when I input data in these text boxes, for example, "THIS IS MY SAMPLE TEXTBOX", which overlaps the Textbox's visible area and displaying as "AMPLE TEXTBOX". But I want the text to be displayed from the starting position like "THIS IS MY S" and then if needed, overlaps. How can I do this? I have tried below but no luck. Please help. Thanks.
(sender as TextBox).TextAlign = HorizontalAlignment.Left;
Edit
I am also using AutoCompleteMode.Suggest so that when I press any key, corresponding list will be displayed similar to dropdownlist. But the first item of this list is selected by default, which I do not want. Can you please suggest in this also. Thanks.
Final Solution
I am using this to solve the issue
(sender as TextBox).TextAlign = HorizontalAlignment.Left;
(sender as TextBox).Select(0, 0);
Thanks to #Har Har.
I found the solution, To position the cursor at the beginning of the contents of a TextBox control, call the Select method and specify the selection start position of 0, and a selection length of 0.
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "Hello this is a sample application";
textBox1.Select(0, 0);
}
It will show the cursor position at 0 index, It's working.

how to get and set current cursor position of WPF textbox

I want to get the current cursor position from a WPF TextBox. If a TextBox contains text abhishek and cursor is blinking after abhi then i want that index, so that later after clearing the TextBox programmatically and assigning some other or same text programmatically I want to make the cursor blink just after 4 characters.
I have tried get cursor position like this,
_tempFuncName = txtFunctionName.Text;
_cursorPosition = txtFunctionName.SelectionStart;
_selectionLength = txtFunctionName.SelectionLength;
And set back at some later stage from other event like this,
txtFunctionName.Text = _tempFuncName;
txtFunctionName.SelectionStart = _cursorPosition;
txtFunctionName.SelectionLength = _selectionLength;
Here underscore variables are page level variables.
This code is not working. Is there some other approach?
You can play with caretindex property of a text box
//You can set this property on some event
NumberOfDigits.CaretIndex = textbox.Text.Length;
You just need to add one line to set focus on textbox otherwise everything is working fine.
txtFunctionName.Text = _tempFuncName;
txtFunctionName.SelectionStart = _cursorPosition;
txtFunctionName.SelectionLength = _selectionLength ;
txtFunctionName.Focus();
txtFunctionName.Text = _tempFuncName;
txtFunctionName.SelectionStart = _cursorPosition;
txtFunctionName.SelectionLength = _selectionLength ;
these statements are sufficient enough to do the req thing. i was making mistake in choosing event to write code. Thanks everyone.
For me, setting the focus only didn't help, but scrolling to the caret did.
txt_logArea.Select(txt_logArea.Text.Length, 0);
txt_logArea.ScrollToCaret();

C# dynamically add text to tabcontrol

Having a slight problem on C#, still quite new to the language but hoping you can help. I have a program which dynamically creates tab forms and then I'm trying to add controls to the tabform (text boxes and labels), but no matter what I try it just doesn't seem to want to work. Here's the code I'm currently using (just to get one textbox in each form):
int i = dogresults;
while (i > 0)
{
i--;
DataRow dogrow = ds1.Tables["confirmdogs"].Rows[i];
string dogname = dogrow.ItemArray.GetValue(3).ToString();
TabPage newpage = new TabPage(dogname);
tcNewCustomer.TabPages.Add(dogname);
TextBox tb1 = new TextBox();
tb1.Location = new Point(20, 10);
newpage.Controls.Add(tb1);
tb1.Name = "txtDogNo" + i;
}
Any help would be greatly appreciated!
EDIT: Doh! Got it!
You're not adding the new TabPage you're creating. This line:
tcNewCustomer.TabPages.Add(dogname);
should be like this:
tcNewCustomer.TabPages.Add(newpage);
(A small test app shows the tab pages being created without any textboxes with the first version, but with the second version working fine.)
That looks okay at a glance (although I haven't tried it - a short but complete demo program would help). When you say it "just doesn't seem to want to work" - what exactly is happening?
Have you tried moving the location down a bit? I know some controls are odd in terms of where their logical "top" is (i.e. it's not the first visible pixel).
What about setting the text in the textbox? Currently you're just setting the name...
Although I'd still expect you to see a border on the box + background colour assuming that differs from the tabpage background.

Categories