I've been tasked with creating a feedback dialogue box to work in conjunction which offers the user simple text based feedback on what operations are occurring. E.g. master switch turned on, hob 1 turned on. I got the text response to display in a multiline textbox, but can only get it to write on the first line therefore overwriting whatever was previously there before. Is there a way to get new text responses to appear on the line below any previous inputs? This is where I'm up to:
if(MasterOnOff.Checked == true)
{
FeedbackTextBox.Text = "Master Switch On";
}
if(MasterOnOff.Checked == false)
{
FeedbackTextBox.Text = "Master Switch Off";
}
Working in C# Windows Forms, any help massively appreciated!
Try
multilineTextBox.Text += "Your message" + Environment.NewLine;
it´s equal to
multilineTextBox.Text = multilineTextBox.Text + "Your message" + Environment.NewLine;
Instead of FeedbackTextBox.Text = "..." which replaces the text in your textbox, try to append to the end of it by using += instead:
FeedbackTextBox.Text += "Master Switch Off\n".
which is the same as
FeedbackTextBox.Text = FeedbackTextBox.Text + "Master Switch Off\n"
The \n part is just the character for new line so that it doesn't add everything as a single line.
Related
i created a windows forms-app with visual studio and im programming in c#.
I have one textbox, one richtextbox and one button. If im writing something in the textbox and press the button, the text from the textbox is shown in the richtextbox (just like a simple chat). My problem is, that if i already wrote something and add something new, the old text in the richtextbox gets replaced by the new one. I want that everything i wrote is among each other.
My source code:
string message = textbox1.Text;
richTextBox1.Text = "Name:" + message;
Of course; you set the Text of the richtextbox to a new text each time you access it
Consider using richTextBox1.AppendText(message) instead
Using the equals operator replaces the value in the .Text property. If you'd like to append text, use the += operator:
string message = textbox1.Text;
richTextBox1.Text += "Name:" + message;
Alternatively, you can skip the shorthand and say:
string message = textbox1.Text;
richTextBox1.Text = richTextBox1.Text + "Name:" + message;
Or, use AppendText():
richTextBox1.AppendText(message)
I am creating a "Programming Language" for a friend of mine and he wanted the "print" command or in this case "write" so I made it but he also wanted it to check for a "^n" which will mean "next line" but I don't know how to do that
I am using two text boxes
Here is the code that reads the first textbox for commands:
string[] text = textBox1.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
foreach (string line in text)
{
if (line.Contains("write") || line.Contains("WRITE"))
{
textBox2.Text = line.Substring(6);
if (line.Substring(6) == "^n")
{
//Print words after the "^n" to the next line
}
}
}
But I don't know how I can print off to the second line
BTW:
textBox1 is the TextBox the commands for the language are type
the the user presses the run button and then it reads for the commands
and if the command on a line is "write" then put what I typed after "write" into textBox2
I hope I made that clear enough for you to understand
Set textBox2.Multiline=true to make the second TextBox multiline. Then use textBox2.Text = line.Substring(6).Replace("^n", Environment.NewLine) to set the text.
I am trying to extract the text under the mouse using the following code
private static async Task<string> getText(double x, double y)
{
try
{
var location = new System.Windows.Point(x, y);
AutomationElement element = AutomationElement.FromPoint(location);
object patternObj;
if (element.TryGetCurrentPattern(TextPattern.Pattern, out patternObj))
{
var textPattern = (TextPattern)patternObj;
var range = textPattern.RangeFromPoint(location);
range.ExpandToEnclosingUnit(TextUnit.Word);
var text = range.GetText(-1).Trim();
range.ExpandToEnclosingUnit(TextUnit.Line);
text += " [" + range.GetText(-1).Trim() + "]";
return text;
}
else if (element.TryGetCurrentPattern(ValuePattern.Pattern, out patternObj))
{
var valuePattern = (ValuePattern)patternObj;
return valuePattern.Current.Value;
}
else
{
var text = "";
var patterns = element.GetSupportedPatterns();
foreach (var pattern in patterns)
{
text += pattern.Id + " " + pattern.ProgrammaticName;
text += Environment.NewLine;
}
return text;
}
}
catch (Exception ex)
{
return ex.Message;
}
}
this works normally (using the Money Metro app to test). However, if the text box is scrolled beyond the first line, the UI Automation begins to pull the wrong text because it thinks the top of the window is the top of the text. For example for the image below, the mouse is on "increasing" and the top of the text (starting with "A majority...") is visible.
If we scroll past the first line, and place the mouse again on "increasing", this time it gets confused and is pulling from the line above. As we scroll more and more the displacement becomes larger and larger. In the image below, it picks up "with" from the line above.
Any ideas for workaround also much appreciated.
Update. The "class" in this case is RichTextBlock. Same issue in TextBlock in the Windows Store. Seems like an issue with RangeFromPoint not accounting for scrolling past the first line.
I am using .NET 4.0 but this also occurs in 2.0...
In my opinion, a RichTextBox in MultiLine mode should not select lines of text the way it does.
In the control, input several lines of text with at least one empty line in the middle, like this:
The quick brown fox jumpsover lazy dog.
More people preferapples to oranges.
It should be possible to place the insertion point at the beginning (before "The") and use the keyboard's Down arrow key to select the first two lines--not including the blank line below it, yet it includes the blank line. I know it's not a big thing to hit the Left arrow key to deselect the blank line but it is annoying and besides, it should work like other applications and like the TextBox control.
I looked at the control's properties and found nothing but perhaps there is one and I can't think of what it is named.
Must I write this behaviour myself?
Thanks.
bool HasIgnoredSelection { get; set; }
HasIgnoredSelection = false;
rtb.KeyUp += (e) => {
int cursorIndex = rtb.SelectionStart + rtb.SelectionLength;
bool isBlankLineSelection = e.Shift
&& e.KeyCode == Keys.Down
&& rtb.Lines[rtb.GetLineFromCharIndex(cursorIndex)] == Environment.NewLine;
if (!HasIgnoredSelection
&& isBlankLineSelection)
{
rtb.SelectionLength -= Environment.NewLine.Length;
HasIgnoredSelection = true;
}
else if (isBlankLineSelection)
{
HasIgnoredSelection = false;
rtb.SelectionLength += Environment.NewLine.Length;
}
else
{
HasIgnoredSelection = false;
}
};
Of course, this is the hacky way of doing it that you were talking about, though I'm not sure this is even what you want. It would prevent you from continuing to select text by the keyboard unless you held the Down key, so there's other ways you could approach this.
edit: added the ability to press down twice to get it to jump to the line after the blank line
I have to add details of my file into a multiline textbox. But all the details are getting added in a single line in the text box and not in a vertical sequence. I used Environment.NewLine and also used "\r\n", but it's not of any help. I have ticked the multiline text box in a Windows Forms form and also set it to true but to no avail.
My line of code is like this:
m_Txt.Multiline = true;
m_Txt.Text = fileInfo.m_Title + "\r\n" +
fileInfo.m_Identifier + Environment.NewLine +
fileInfo.m_TotalTime;
A cleaner answer is:
Assuming txtStatus is a textbox:
txtStatus.Multiline = True;
txtStatus.Clear();
txtStatus.Text += "Line 1" + Environment.NewLine;
txtStatus.Text += "Line 2" + Environment.NewLine;
Using the built in enumeration means cleaner code.
Shift+Enter
In the Visual Studio resource editor, you can hit "Shift + Enter"
to create a new line, as doing something like "\r\n" will get escaped
out. You will also need to increase the cell height to see both
lines as it does not auto-size.
If you're doing it programatically, append the new line to m_Txt.Lines, which is a string[].
m_Txt.Lines = new string[]{ fileInfo.m_Title, fileInfo.m_Identifier, fileInfo.m_TotalTime};
Not sure why your code would not work unless something else is going on.
I just created a WinForms project using C#, added a textbox, set it multiline and added the following code - works a charm.
textBox1.Text = "a\r\nb";
I just wrote this code, seems to be working fine.
public void setComments(String comments)
{
String[] aux;
if(comments.Contains('\n')) //Multiple lines comments
{
aux = comments.Split('\n');
for (int i = 0; i < aux.Length; i++)
this.textBoxComments.Text += aux[i] + Environment.NewLine;
}
else //One line comments
this.textBoxComments.Text = comments;
}