How to Avoid LineBreaks in Winforms TextBox - c#

Im learning c# at the moment, and I would like to add text to a TextBox.
I am appending Text with TextBox.AppendText() method:
public delegate void WriteToLogEventHandler(object sender, EventArgs e);
private void WriteToLog(object sender, EventArgs e)
{
if (InvokeRequired)
Invoke(new WriteToLogEventHandler(WriteToLog), new object[] { sender, e });
else
{
textBox_Messages.AppendText((e as WriteToLogEventArgs).Message);
}
}
But I don't want the Lines to automatic line break, I want the TextBox to scroll horizontal. I already tried the TextBox Scrollbar Property "vertical", "horizontal" and "both"
but the Textbox looks always like this:
Is there a way to automatically make the textbox horizontal scrollable
excuse my english
Thanks Locke...

Try setting the WordWrap property to false along with having both scroll bars.

TextBox.WordWrap = false
From MSDN:
true if the multiline text box control wraps words; false if the text
box control automatically scrolls horizontally when the user types
past the right edge of the control. The default is true.

Related

How to append the string at a cursor position in a rich text box C#?

I want to add string in rich text box at that position where the cursor is blinking. I also want to change the color of text that I entered. For example If there is pre-entered text which color is black, then I entered a new string then this string should have blue color. And it should be for all the time i entered the string. And the strings I entered, they should remain blue. thanks
The current cursor position is also know as the SelectionStart. Simply set the SelectionStart=0 and then set all SelectionXXX properties you want to:
richtTextBox1.SelectionLength=0;
richtTextBox1.SelectionColor = Color.Blue;
Now all you type will be blue.
No more and no less is needed. No need to subscribe to the TextChanged event.
Note that if you move the cursor position to a different spot that spot will have a color of its own and when you enter new text there it will have that color.
If you want to apply the new color (Blue) to other spots you need to code the SelectionChanged event maybe like this:
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
// you should check if there is no text selected
// or else any selection will be colored immediately:
if ( richTextBox1.1.SelectionLength == 0)
richTextBox1.1.SelectionColor = yourNewColor;
}
And as usual, if you want to color a portion that is already there with a new color you need to select that portion and again set the color. Best use a color palette for this! Using a few colored Labels you might write:
private void colorLabel_Click(object sender, EventArgs e)
{
Color yourNewColor= ((sender) as Label).BackColor;
richTextBox1.1.SelectionColor = yourNewColor;
}
If you want to you can use the image of a richer palette in a Panel's BackgroundImage and code its MouseClick event:
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
Color c = ((Bitmap)panel1.BackgroundImage).GetPixel(e.X, e.Y);
if (e.Button.HasFlag(MouseButtons.Left))
{
richTextBox1.SelectionColor = c;
}
else // pick new BackColor:
{
richTextBox1.SelectionBackColor = c;
}
}
So you need to subscribe to the event where text is changing from the textbox, this will give you the text that has been entered up to the cursor.
Then it is a simple matter of just appending text to that as statements, where you can apply different formatting, as described here:
http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/

Moving to the end of the text in a readonly TextBox

I currently have a TextBox in my WPF application that is readonly:
<TextBox x:Name="TextBox_CurrentDirectory" IsReadOnly="True"></TextBox>
And text gets updated in the Code Behind:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var app = Application.Current as App;
TextBox_CurrentDirectory.Text = app.ActiveDirectory;
//Show the end of the text here
}
Is there a way for me to show the end of the text programmatically? If the text in the TextBox is longer than the TextBox, it only shows the start and gets cut off. I'd like to be able to show the end of the text.
I tried using
TextBox_CurrentDirectory.CaretIndex = TextBox_CurrentDirectory.Text.Length;
but nothing happens.
You need to give your TextBox Focus before setting the CaretIndex.
TextBox_CurrentDirectory.Text = app.ActiveDirectory;
TextBox_CurrentDirectory.Focus();
TextBox_CurrentDirectory.CaretIndex = TextBox_CurrentDirectory.Text.Length;

How to make textbox text disappear when I click on it

I managed to create textboxes dynamically in C#. When a textbox has text, can I make it disappear when I click on it?
I need to put a word inside textboxes that is the result of a select in Oracle.
Assign a value to the Text property of the TextBox. You could subscribe then to the GotFocus event, and set the text value to an empty string.
// Holds a value determining if this is the first time the box has been clicked
// So that the text value is not always wiped out.
bool hasBeenClicked = false;
private void TextBox_Focus(object sender, RoutedEventArgs e)
{
if (!hasBeenClicked)
{
TextBox box = sender as TextBox;
box.Text = String.Empty;
hasBeenClicked = true;
}
}
javascript is good for the delete.
onclick="$(this).val('');"
alternatively you can use HTML5 placeholder

C# Winforms - When are scrollbars for a Control initialized?

Here is the situation:
I am trying to control flowLayoutControl's scroll bar from devexpress controls VerticalScroll.
Now - flowLayoutControl with autoscroll = true. I added a new verticalscroll control and dock it to Right. So now the Devexpress Vertical Scroll control is right on top of FlowLayout scrollbar.
Also the FlowLayoutPanel vertical scroll does not hide when following code is run:
spotWinFlowLayout1.VerticalScroll.Visible = false
I have setup the following event handlers:
private void spotWinFlowLayout1_Resize(object sender, EventArgs e)
{
SetupVerticalScrollBar();
}
private void SetupVerticalScrollBar()
{
vScrollBar1.Minimum = spotWinFlowLayout1.VerticalScroll.Minimum;
vScrollBar1.Maximum = spotWinFlowLayout1.VerticalScroll.Maximum;
vScrollBar1.LargeChange = spotWinFlowLayout1.VerticalScroll.LargeChange;
vScrollBar1.SmallChange = spotWinFlowLayout1.VerticalScroll.SmallChange;
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
spotWinFlowLayout1.VerticalScroll.Value = e.NewValue;
}
Everything is working fine except when on form Load there is already a scrollbar on flowLayoutControl,
spotWinFlowLayout1.VerticalScroll.XXX properties not set yet. So both the scrollbars are out of Sync. But as soon as I resize the form both get Sync.
So when is scrollbar for the FlowLayoutPanel initialized?
So when is scrollbar for the
FlowLayoutPanel initialized?
This might sound like a smartalec answer: "When the control is drawn or placed on the form" which is the reason when you adjust the size of the form they are in sync.( they are being drawn again )
The solution is to manually add the scrollbars yourself.

C# ComboBox in DropDownList style, how do I set the text?

I want to use a ComboBox with the DropDownList style (the one that makes it look like a button so you can't enter a value) to insert a value into a text box. I want the combobox to have a text label called 'Wildcards' and as I select a wildcard from the list the selected value is inserted in to a text box and the combobox text remains 'Wildcard'. My first problem is I can't seem to set a text value when the combobox is in DropDownList style. Using the properties pallet doesn't work the text value is simply cleared when you click off, adding comboBox.Text = "Wildcards"; to form_load doesn't work either. Can anyone help?
The code you specify:
comboBox.Text = "Wildcards";
...should work. The only reason it would not is that the text you specify is not an item within the comboBox's item list. When using the DropDownList style, you can only set Text to values that actually appear in the list.
If it is the case that you are trying to set the text to Wildcards and that item does not appear in the list, and an alternative solution is not acceptable, you may have to be a bit dirty with the code and add an item temporarily that is removed when the drop-down list is expanded.
For example, if you have a form containing a combobox named "comboBox1" with some items and a button named "button1" you could do something like this:
private void button1_Click(object sender, EventArgs e)
{
if (!comboBox1.Items.Contains("Wildcards"))
{
comboBox1.Items.Add("Wildcards");
}
comboBox1.Text = "Wildcards";
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
if (comboBox1.Items.Contains("Wildcards"))
comboBox1.Items.Remove("Wildcards");
}
That's pretty quick and dirty but by capturing the DropDownClosed event too you could clean it up a bit, adding the "Wildcards" item back as needed.
You can select one of items on formload or in form constructor:
public MyForm()
{
InitializeComponent();
comboBox.SelectedIndex = 0;
}
or
private void MyForm_Load(object sender, EventArgs e)
{
comboBox.SelectedIndex = 0;
}
Try this
comboBox1.SelectedValue = "Wildcards";
This may be a possible solution:
comboBox1.SelectedValue = comboBox1.Items.FindByText("Wildcards").Value;

Categories