Moving to the end of the text in a readonly TextBox - c#

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;

Related

label is not created in other form C# win form

So, my problem is that i have a button in one form that needs to add a label in other form, label has a text that is from main labels textbox, and label has a backcolor that is from color block selected from main form.
private void btnAdd_Click(object sender, EventArgs e)
{
PiezimesLogs log = new PiezimesLogs();//form where the label would be added
Label l = new Label();// creates new label
l.Text = piezimeTxt.Text;
l.BackColor = ColorChange.BackColor;
log.Controls.Add(l);
}
so, this is the code from button that should do everything.
log - other form in which the label should be created.
c# win form
Instead of creating a new label, you could simply add an empty label to your 'target-form'.
Afterwards, you can simply modify the labels' text in order to make it appear.
Regards,
Michael

How to Avoid LineBreaks in Winforms TextBox

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.

how to edit text in a text box that was set using Text box.text?

i cant edit the text that's in the text box. i need the textbox to show text at the start which then can be edited.
imageName shows up in the text box but i cant edit it to something else.
private void image_NameTextBox_TextChanged(object sender, EventArgs e)
{
string imageName = (dictionaryDataSet1.Tables[0].Rows[selectedIndex]["Image Name"].ToString());
image_NameTextBox.Text = imageName;
}
Looks like you defined a TextChanged-Event to your TextBox. Every time you edit your Text you are calling your handler:
private void image_NameTextBox_TextChanged(object sender, EventArgs e)
{
string imageName = (dictionaryDataSet1.Tables[0].Rows[selectedIndex]["Image Name"].ToString());
image_NameTextBox.Text = imageName;
}
The result is: You are setting the Text back to the old value.
Remove the event from your TextBox and fill your Text in a different methode.
You have your code in the TextChanged event which means everytime you try and edit the text it just sets it right back to the same thing. Move your code outside of the TextChanged event (somewhere it is still functional) and then try editing your text.

Accessing a text box inside a user control

I'm having problems binding a user control. I have a main window, which with a press of a button gets my value. However when i use my user control in the main window i cant set the text box to add my value. Instead of 'text' i want my user control text box? I have tried to declaring the text box but no luck any ideas?
private void button1_Click(object sender, RoutedEventArgs e)
{
text.Clear();
text.AppendText( tc.FTemp + "F");
}
<my:UserControl1 Height="172" HorizontalAlignment="Left" Margin="12,197,0,0" VerticalAlignment="Top" Width="151" Loaded="userControl11_Loaded" />
Two solutions:
Create a public property in your user control
Use 'Controls.Find' (the slowest, but easiest way)
public Form1()
{
InitializeComponent();
Control[] control = userControl1.Controls.Find("textbox1", true);
control.First().Text = "Found!";
}

When I double click on a word in the textbox I want to get that word("text") in a C# wpf

I am making a C# wpf application with Visual Studio 2013. I have a multilined text box with some sentences. When I double click on a word in the textbox I want to get that word("text").
How can I achieve this ?
This is the code I wrote for double click event. It gives me an empty String.
private void textBoxResult_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
MessageBox.Show(textBoxResult.SelectedText);
}
You will get the text from the SelectedText property
xaml
<TextBox Text="1. (n.) One who numbers."
MouseDoubleClick="TextBox_MouseDoubleClick"/
AcceptsReturn="True" >
code behind handler
private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
TextBox tb = sender as TextBox;
String doubleClickedWord = tb.SelectedText;
}
idea behind is that when you double click a word it gets selected and can be retrieved from SelectedText property

Categories