Why text box text not showing from starting position in c#? - 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.

Related

Combobox dynamic dropdown height C# Forms

In a project I want a combobox to filter the rows according to the text written there. (overriten class of combobox). I am performing it simply as below:
private void Cmb1_TextUpdate(object sender, EventArgs e)
{
if (isFound(Cmb1.Text)) //if text is not found in dataTable then false
dataTable.DefaultView.RowFilter = string.Format("Name LIKE '%{0}%'", Cmb1.Text);
}
It filters the results. But for example if there is 1 element left after filtering dropdown height decreases to fit the text. It is okey until now. But after I delete filter text items come back but dropdown height is not changing (still in a height value for 1 item).
To fix it I need to unfocus and focus again so it refreshes.
I tried to write this.DropDownHeight = value; at the end of Cmb1_TextUpdate and also override the OnDropDown event
I tried enter link description here subject already but no luck (I think refresh works there but for me not):
NOT: It works when I add these:
DroppedDown = false; DroppedDown = true;
but slows down working of combobox

How can I diplay whole text in RichTextBox using preseted font

in c# I'm trying to display text (from txt file) in RichTextBox and I want display whole text using the preseted font (Arial). When I type the text "test ěščřžýáíé" it displays ok but when I try to set the Text programmaticaly
RichTextBox.Text="test ěščřžýáíé";
the words/characters are displayed with different fonts (the word "test" is really displayed in Arial). What do I do wrong?
Thanks for any help
Pavel
How are you setting the font? I assume you are using the Win Forms RichTextBox. I have managed to produce a very simple application which does what you have described as what you need:
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Font = new System.Drawing.Font(FontFamily.GenericSansSerif, 12.0f);
richTextBox1.Text = "test ěščřžýáíé";
}
With a Button button1 and a RichTextBox richTextBox1. When the button is clicked the text appears.

How to set the position of a label in C#?

I'm trying to improvise a chat for my web site. I have 2 TextBoxes (one for the subject of the question and other for the content of the question). After a certain button is clicked I update a Label with the results.
The position of the Label is not where I want it to be. How can I adjust the position of the Label?
You need to set the Bounds of the label to change it's position & size (assuming your referring to WinForms). Alternatively you can set the Location property directly.
You can also use Location property to set the location of your label on your form.
private void button_Click(object sender, RoutedEventArgs e)
{
textblock_name.Margin = new Thickness(338, 57, 0, 0);
}
you can use above code to change position dynamically

Scroll to the end of a Single-Line Textbox in 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();

How do I highlight a selection made programmatically in a Winforms TextBox

I've not gone into much research here, but the intuitive thing is not working:
private void SerachButton1_Click(object sender, EventArgs e)
{
String serchTerm = searchTerm1.Text;
String text = usualTextBox.Text;
Int32 index = text.IndexOf(serchTerm);
if (index >= 0)
{
usualTextBox.Select(index, serchTerm.Length);
}
}
SelectedText, SelectionLength and SelectionStart properties are as I expect them after Select is called but there's no visible selection.
What am I doing wrong here?
Edit: I've also tried RichTextBox. When I set background and text colors for the selection it shows up, but it won't unselect automatically when you manually select another part of text or just click on a position in text. Are these two types of selection inherently different and if you select programmatically you have to also deselect programmatically?
You need to set usualTextBox.HideSelection to false so that the selection remains visible when the focus is not in the TextBox.

Categories