Using Windows forms c# message box - c#

How will I get a messagebox "Or another way of doing this" to have multiple lines?
I've got a messageBox to appear when the user presses F1 and I need it to have a sort of list:
Product Name = Alphanumeric + Special Characters.
Quantity = Maximum 100.
Price = Must be Numeric.
Etc
Thanks.

You can use \r\n or Environment.NewLine at the end of each line to be shown or you can use the StringBuilder class:
var message = new StringBuilder();
message.AppendLine("Product Name = Alphanumeric + Special Characters.");
message.AppendLine("Quantity = Maximum 100.");
message.AppendLine("Price = Must be Numeric.");
MessageBox.Show(message.ToString());

Add/concatenate Environment.Newline to your string.

Put "\r\n" at the end of each line of the message.
so, for example:
var message = "Product Name = MyProduct.\r\n";
message += "Quantity = Maximum 100.\r\n";
message += "Price = Must be Numeric.\r\n";
MessageBox.Show(message);

Just use Environment.NewLine where you want a newline to appear. For example:
string message = "Line 1" + Environment.NewLine + "Line 2";
MessageBox.Show(message);
This will output:
Line 1
Line 2

Related

Newline is not working in Xamarin.Android

I have string label = 20. I have added label prefix as "Goal" .in between i added \n . Two lines are not coming instead one line only appearing.I need Expected Output.
Expected Output:
Goal
20
ActualOutput:
Goal20
I have tried below methods, its not working
string label = Goal;
string cReturns = System.Environment.NewLine + "\n" + "\r";
string[] words = label.Split(cReturns.ToCharArray());
label = words +20;
or
label = "Goal\n20";
CAN ANYONE SUGGEST ME CORRECT ANSWER
Thanks
You try
instead of \n:
label = "Goal
20";
If i add drawtext in customview its not working
You could strip the \n and then offset the Y to get your text on the next line.
For Example :
string lab = "Goal";
canvas.DrawText(lab, 100, 100, p);
canvas.DrawText("20", 100, 150, p);
Effect.
using System.Environment.NewLine will help.
for example:
await MainPage.DisplayAlert("Connection Problem!", "Can't
access backEnd at the moment Please try again later!"
+ System.Environment.NewLine + ex.Message,
"OK");

Get a specific word from a line read from a .txt

right now I am reading some lines from a .txt.
Lets say, a user enters his name and in the .txt will be saved "Logged in {username} on 13/04/2016 at 10:55 am".
(Just an example.)
Now I want to read the .txt and print only specific parts into a textbox.
Meaning, in the textbox shall appear "{Username} - 13/04/2016 - 10:55 am".
So far, I am able to read from the .txt and print the whole line.
private void button_print_results_Click(object sender, RoutedEventArgs e)
{
int counter = 0;
string actual_line;
System.IO.StreamReader file_to_read =
new System.IO.StreamReader("myText.txt");
while ((actual_line = file_to_read.ReadLine()) != null)
{
textBox_results.Text = textBox_results.Text +"\n"+ actual_line;
counter++;
}
file_to_read.Close();
}
Is there a way, to reach this without overwriting the whole file?
And no, I can't change the format how the names etc. are saved.
(I used them here for a better understanding, the actual lines I need to read/check are different and auto-generated).
I don't expect full working code, it would be just great if you could tell me for which commands I need to look. Been a long time since I last worked with c#/wpf and I never worked much with Streamreader...
Thanks
I think regular expressions is the best tool for what you're trying to achieve. You can write something like this:
Regex regex = new Regex("Logged in (?<userName>.+) on (?<loginTime>.+)");
while ((actual_line = file_to_read.ReadLine()) != null)
{
Match match = regex.Match(actual_line);
if (match.Success) {
string loginInfo = string.Format("{0} - {1}", match.Groups["userName"], match.Groups["loginTime"]);
textBox_results.Text = textBox_results.Text +"\n"+ loginInfo;
}
}
There are couple of possible solutions for this. One most straight forward way for your case would be to use Substring and Replace.
Since the earlier string is always Logged in (note the last space) and you simply want to get the rests of the string after the phrase, replacing only the preposition of time words (" on ", " at ") with dash (" - ") you could take advantage on that:
string str = "Logged in {username} on 13/04/2016 at 10:55 am";
string substr = str.Substring(("Logged in ").Length) //note the last space
.Replace(" on ", " - ")
.Replace(" at ", " - ");
In your implementation, this is how it look like:
while ((actual_line = file_to_read.ReadLine()) != null)
{
actual_line = actual_line.Substring(("Logged in ").Length) //note the last space
.Replace(" on ", " - ")
.Replace(" at ", " - ");
textBox_results.Text = textBox_results.Text +"\n"+ actual_line;
counter++;
}
(Note: the solution above assumes the {username} does not contain spaced preposition of time words - which would almost likely be the case for a {username})
You could split the actual_line String so you get an array of Strings. And then fill the Strings you want to show in the TextBox into it.
string[] values = actual_line.Split(' ');
textBox_results.Text = textBox_results.Text + "\n" + values[2] + " " + values[6] + " " + values[7];
The text in the TextBox for example is "{username} 10:55 am"
You can use Regex for better performances as #Dmitry-Rotay suggested in the previous comment, but if you jave a not-so-big file your loop+string manipulations is an acceptable compromise.
Always use Environment.NewLine instead of "\n", it's more portable.
while ((actual_line = file_to_read.ReadLine()) != null)
{
actual_line = actual_line
.Replace(("Logged in "), String.Empty)
.Replace(" on ", " - ")
.Replace(" at ", " - ");
textBox_results.Text = textBox_results.Text
+ System.Environment.NewLine
+ actual_line;
counter++;
}

Creating a newline in rich text box

I need help on creating a new line for my RichTextBox which I cant make work when using CheckBox.
It keeps overlapping instead of creating a newline of words.
Tried using the method of rtbdisplay.text = (display+envrionment.newline);
example from my code:
if (rbtnSmall.Checked == true)
{
rtbDisplay.Text = "displaytext".PadRight(20) + "size".PadRight(23) +
qty.ToString().PadRight(20) + StrongDummy;
}
Use the RichTextBox.Text property or the RichtTextBox.AppendText method to append a string with a newline.
myRichTextBox.Text += Environment.NewLine + "My new line.";
// Or
myRichTextBox.AppendText( Environment.NewLine + "My new line." );
You can use c# Environment.NewLine Property as described in http://msdn.microsoft.com/en-us/library/system.environment.newline%28v=vs.110%29.aspx. Or, the "old style" like #"\r\n".
Rgds,

How to Show New Line command in text box?

Hi i am doing a small project in C#, and i need to know what commands are comming from input source so i can do my work accordingly..
here is example...
textBox1.Text = "First line \nSecond line";
richTextBox1.Text = "First line \nSecond line";
Richtextbox shows this output:
First line
Second line
Textbox show this output:
First line Second line
please tell me how to show new line "\n" or return "\r" or similar input as a character output in text or richtextbox. so i can know that newline command is coming from input data.
for example text or richtext box will show this output.
First line \nSecond line
thankx in advance.
Lets say that i have a string which have new line :
string st = "my name is DK" + Environment.NewLine + "Also that means it's my name";
Now that i want to show that there is new line in my text there you go :
textBox1.Text = st.Replace(Environment.NewLine, "%");
This will show the newline chat with % sign
For winforms application set
this.textBox1.Multiline = true;
and use "\r\n" as
textBox1.Text = "First line \r\nSecond line";
You want to either prefix your string with # or you can use a double slash before each n (\n). Both of these are ways of escaping the \ so that it displays instead of being treated as part of a new line.
#"This will show verbatim\n";
"This will show verbatim\\n";
You can utilize this by performing a Replace on your incoming text
richTextBox1.Text = richTextBox1.Text.Replace("\n", "\n\\n");
richTextBox1.Text = richTextBox1.Text.Replace("\r\n", "\r\n\\n");
In the replace, I left the original linebreak so that it will be there, just followed by the displaying version. You can take those out if you dont want that. :)
Use the combination "\r\n" (or Environment.NewLine constant which contains just that).
Change Your text box property from single line to Multiline then it will change to new line
TextBox1.TextMode = TextBoxMode.MultiLine;
TextBox1.Text = "First line \n New line ";
TextBoxt1.Text ="FirstLine \r\n SecondLine";

c# write message to textbox

Kind of new to c# GUI stuff
I am trying to output a message, a number to a textbox.
1 Button will calculate the number, then I want to write a message like " Number has been seen: "
I tried
Form2.resultBox.Text.Write("Number one has been seen: ", num0);
that doesn't work.
Also tried
Form2.resultBox += Console.WriteLine("Numer one has been seen: ", num0);
Im going to have about 16 of these messages
ideas?
Form2.resultBox.Text = "Number one has been seen: " + num0;
To set the value of a TextBox, you should set a value on the Text property like so:
resultBox.Text = "Number one has been seen: " + num;
how about using the string.Format
Form2.resultBox.Text = string.Format("Number one has been seen: {0}", num0);
eliminates having to use + sign

Categories

Resources