Conditional MessageBoxes - c#

I'm currently making a script to display particular MessageBoxes if different conditions are fulfilled. I have 8 conditions to check for and each of them have to display different MessageBoxes if they are "0" or "1".
An shortened example of my code is as follows:
// Similar if(y[1] == "1") statements above with similar Messages but without the corresponding fruit(s)
if (y[2] == "1")
{ MessageBox.Show("Multiple goods required! Please get the following
items off the shelves" + Environment.NewLine +
"1. Apple" + Environment.NewLine + "2. Pear" +
Environment.NewLine + "3. Orange");
}
else if (y[2] == "0")
{ MessageBox.Show("Multiple goods required! Please get the following
items off the shelves" + Environment.NewLine +
"1. Apple" + Environment.NewLine + "2. Pear");
}
My knowledge of C# is quite basic, but I'm willing to learn! Please help!

I would make messages conditionally, and write the code for showing the message box once:
string msg = "Multiple goods required! Please get the following items off the shelves";
if(y[2] == "0" || y[2] == "1")
{
msg += Environment.NewLine + "1. Apple"
+ Environment.NewLine + "2. Pear";
if (y[2] == "1")
msg += Environment.NewLine + "3. Orange";
MessageBox.Show(msg);
}
I just made the above to be equivalent to your code, otherwise I think it could be written much better.
Also consider using String.Format and StringBuilder for creating the message, instead of concatenating small strings.

if your question is how to display the messageboxes easier it would be this way :
for(int i =0;i<y.length;i++){
if(y[i] == 0){
MessageBox.Show("Multiple goods required! Please get the following
items off the shelves" + Environment.NewLine +
"1. Apple" + Environment.NewLine + "2. Pear");
}
else if(y[i] == 1){
MessageBox.Show("Multiple goods required! Please get the following
items off the shelves" + Environment.NewLine +
"1. Apple" + Environment.NewLine + "2. Pear" +
Environment.NewLine + "3. Orange");
}
}
if this was not your question let me know i will come back and try to help

Related

Ident Tabs for Multiple New Line String Variable in Net Core

How do I ident a set of lines when writing to a file, with a tabs? I need to go through each line in the variable and create 8 spaces or two tabs for each line, when writing to the new file.
If the string was one line, it would be easy, with " " + test, however this has multiple lines.
public static string testLine=
"Line1" + Environment.NewLine +
"Line2" + Environment.NewLine +
"Line3" + Environment.NewLine +
"Line4" + Environment.NewLine +
using (System.IO.StreamWriter file = new System.IO.StreamWriter(filePath, true))
{
file.WriteLine(testLine);
String is composed of new line, line breaks enters, etc .
Is there any Microsoft Ident function library to support this?
Needs to handle multiple string variables in the future.
All you need to do is prefix the string with whitespace or a tab and replace all occurences off newlines with a newline and a suitable whitespace:
testLine = " " + testLine.Replace( Environment.NewLine, Environment.NewLine + " " );
You can explicitly handle an empty string to avoid indenting nothing:
testLine = String.IsNullOrEmpty(testLine) ? testLine : " " + testLine.Replace( Environment.NewLine, Environment.NewLine + " " );
Substitute "\t" for " " to insert a tab character.

Message Box issues in C#

MessageBox.Show ("About Developer"+ "Mandelbrot by Milan." + Environment.NewLine +
"Email: xyz#abc.com" + Environment.NewLine +
"Contact No: +977123456789" + Environment.NewLine);
I want About Developer as title. I searched but I didn't get suitable solution to my problem.
There are a bunch of overloads on MessageBox.Show. The one you want is the one where you call it with 2 strings like this:
MessageBox.Show ("Mandelbrot by Milan." + Environment.NewLine +
"Email: xyz#abc.com" + Environment.NewLine +
"Contact No: +977123456789" + Environment.NewLine,
"About Developer");

Can make the color separately?

As you can see it is all red. I need the number to be red, but text to be black as shown on picture below. This is my code :
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
str1 = str;
str1 += listBox2.Text;
wassaw.Text = str1;
TextBox1.ForeColor = Color.Red;
switch (str1)
{
case "Привет1" :
TextBox1.Text = "" + Environment.NewLine + "1. привет " + Environment.NewLine + "2. привет " + Environment.NewLine + "3. привет ";
break;
case "Привет2" :
TextBox1.Text = "" + Environment.NewLine + "1. привет ";
break;
case "Приветф3" :
TextBox1.Text = "" + Environment.NewLine + "1 привет ";
break;
case "Приветы4" :
TextBox1.Text = "" + Environment.NewLine + "1 привет ";
break;
}
}
Not using a regular TextBox. The TextBox control just allows single color, single formatting text.
There are several options. You could create your own control, use external libraries, or you can use the RichTextBox which does allow formatting and coloring. With a little help, you can achieve what you want:
See this question how to do that: Color different parts of a RichTextBox string.
Another option would be to use a grid control, like the TableLayoutPanel and put the numbers and text in separate labels.

How to properly display Line Feed in string when used on webpage or email

I have a string that I save to a database. Problem is it only displays the line feed when I am in the edit mode form. When I just display it (no editing) in my webpage or use it in an email the string displays with no line feeds and it is all run together (See pictures below). I'm using MVC 4 and VS2012.
Can anyone please tell me how to get the line feeds to always display?
The string is generated from a form with the following code:
string LF = " \r\n ";
//string LF = Environment.NewLine; // tried this also, did'nt work.
string appendComments = " ";
contact.Subject = "Quick Quote";
appendComments += "Make Sure to Contact Customer by: " + Request.Form["radio"].ToString(); ;
appendComments += LF + LF + "CARPET CLEANING: " + LF;
if ((Request.Form["bedRms"]) != "0") { appendComments += "Bedrooms =" + Request.Form["bedRms"] + ", " + LF; }
if ((Request.Form["familyRm"]) != "false" || (Request.Form["familyRm"]) == "on" ) { appendComments += "Family Room = Yes, " + LF; }
if ((Request.Form["livingRm"]) != "false" || (Request.Form["livingRm"]) == "on") { appendComments += "Living Room = Yes, " + LF; }
if ((Request.Form["diningRm"]) != "false" || (Request.Form["diningRm"]) == "on") { appendComments += "Dining Room = Yes, " + LF; }
if ((Request.Form["ld-combo"]) != "false" || (Request.Form["ld-combo"]) == "on") { appendComments += "Living/Dining Combo = Yes, " + LF; }
if ((Request.Form["pets"]) != "false" || (Request.Form["pets"]) == "on") { appendComments += "Pet Spot/Stain Issue = Yes " + LF; }
Here I save it to the database.
Line Feed Works in the Edit Form:
works in edit form http://www.leadingedgewebsites.com/linefeedworks.jpg
Line Feed Does Not Work When Displaying it on the webpage:
Does Not Work on Webpage http://www.leadingedgewebsites.com/linefeednowork.jpg
Line Feed Does Not Work on Email:
Does Not Work in email http://www.leadingedgewebsites.com/inemail.jpg
I do appreciate you taking the time to help me.
Thank you.
HTML ignores all extraneous whitespace, including newlines.
To change this, use the <pre> tag, or the white-space: pre CSS property.
For web pages you have to replace your " \r\n " with <br />, since it only recognizes HTML line breaks.
For Email and other you should use Environment.NewLine instead of " \r\n ", since that will display a line feed based on the container.
If you want to display a new line on a webpage use line break tag - <BR>

c# replace timestamp value string of text in textfield

i currently have a textbox field, everytime a change is made, i add who it was updated by and what time
right now it keep appending that text
how can i find the line that says "LastEdited: ", and only replace the time stamp value?
this is what i do now
maybe someone can post me example of code how to grab the timestamp and replace it?
if (txtMemberNotesOriginally != txtMemberNotesChanged) {
txtMemberNotes.AppendText("LastEdited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
i am not an expert, so an example of code would bevery useful
i do use this textbox to save other notes as well (which i wouldn't wanna loose)
can i do this
foreach (var line in txtMemberNotes.Lines) {
if (line.StartsWith("Last Edited: "))
{
txtMemberNotes.Text = txtMemberNotes.Text.Replace(line, "Last Edited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
}
else
{
txtMemberNotes.AppendText("Last Edited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
}
}
The easiest way to solve your problem seems to stop appending text and just set the text value. No parsing is necessary, just overwrite the exiting values with your newer value.
if (txtMemberNotesOriginally != txtMemberNotesChanged) {
txtMemberNotes.Text = "LastEdited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine;
I guess theres always Regex if you really wanted:
string s = #"Some notes here etc etc..
Last edited: 12/12/2012 12:00:00 AM";
Console.Write(Regex.Replace(s, #"Last edited: .*$", "Lasted edited: " + DateTime.Now.ToString()));
Or more specifically using your code:
txtMemberNotes.Text = Regex.Replace(txtMemberNotes.Text, #"Last edited: .*$", "Lasted edited: " + DateTime.Now.ToString());

Categories

Resources