c# write message to textbox - c#

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

Related

String randomly containing a newline C#

I'm completely lost with what is happening here.
string send = "!points add " + entries[winner] + " " + prize.ToString();
What I want to send is "!points add winnername prizeamount" but what I get is "!points add winnername\nprizeamount". I put \n because it writes a new line but trying to replace "\n", "\r" and "\t" with " " does nothing.
enter image description here
all I need is the message to be exactly"!points space add space winnername space prizeamount space"
If it's important the entries in my code is a List of strings
The entries strings already contain the new line character(s).
I suggest you replace with Environment.NewLine:
Replace Line Breaks in a String C#
The string object, 'entries[winner]' is having line-feeds (LF) or carriage-returns (CR). You try this to remove all LFs and CRs,
string send = "!points add "
+ entries[winner].Replace("\r", string.Empty).Replace("\n", string.Empty)
+ " " + prize.ToString();
Alternatively, you can use Trim() to remove leading\ trailing LFs\ CRs.
string send = "!points add "
+ entries[winner].Trim()
+ " " + prize.ToString();
No repro. The following code doesn't assert.
var entries=new List<string>{"Aaa", "Bbb", "Ccc"};
int prize=90;
int winner=1;
var send=String.Format("!points add {0} {1}",entries[winner],prize);
var send2="!points add " + entries[winner] + " " + prize.ToString();
Trace.Assert("!points add Aaa 90"==send);
Trace.Assert(send2==send);
If the result contains newlines, it's because the entries values contain newlines.
The best solution would be to clean the input data before storing it in the list, eg with String.TrimEnd or String.Trim, When loading data from a file for example, you can't be sure it doesn't contain trailing spaces.
To read clean data from a file you could use :
var entries=File.ReadLines()
.Select(line=>line.Trim())
.ToList();
If you add the entries one by one from user input :
entries.Add(newEntry.Trim());
If you can't change how the data is read (why?) you can trim when whenever you use an entry value:
var send=String.Format("!points add {0} {1}",entries[winner].Trim(),prize);
Loading clean data is a lot easier

How to slice/trim this value?

I am trying to remove the last 6 characters from item.Size because the data has a decimal place and 5 trailing 0s in the database.
sb.Append("<div>" + item.Size + " " + item.Units + " </div>");
ie. item.Size is displayed as 1.00000 and I need it to just be displayed as 1.
This is part of a StringBuilder, and as I'm new to coding, not even sure the right way to go about this.
sb.Append("<div>" + (int)item.Size + " " + item.Units + " </div>");
StringBuilder has the same formatting capabilities as String.Format when you use the AppendFormat method:
sb.AppendFormat("<div>{0:N0} {1} </div>", item.Size, item.Units);
The format string "N0" tells it to format the number with 0 decimal points. That assumes the item.Size is stored as a numerical type. If not, simply remove the part of the string you don't want:
sb.AppendFormat("<div>{0} {1}</div>", item.Size.Split('.')[0], item.Units);
Here I've used Split, assuming that the value is actually something like what you've shown in your example.
Better you use int.TryParse(or Int32.TryParse) method. because if item.Size is not convertible to int, then it wont give you any exception. you can use int or long according to your choice. So you can handle this in your code according to the if/else condition.
Sample Code:
int size;
string str = "";
if(int.TryParse(str, out size) == true)
{
}
else
{
}

c# richtextbox only shows one line

been searching the internet for some time now and cant find a solution to my problem.
I'm trying to parse some lines into the richtextbox, but i only get one line instead of all of them. Heres a part of my code
foreach (var val in lineCountDict)
{
richTextBox2.Text = (val.Key + " - " + val.Value + " Drops -" +
((double)val.Value / (double)mobDeathCounter) * 100 + " % chance\n");
}
So when i run this i only get one line, any advice would be awesome
Thank you!
Of course you get one line since in each iteration you set richTextBox2.Text to the new value which replaces the old value.
Use richTextBox2.Text+="..."

adding space in texbox results when sending thru email

wassup guys im new to C# coding and i did a search but couldn't find exactly what im looking for. So i have a couple of text-boxes which holds string elements and integers
what i want to do is when these boxes are filled in i want to send a summary of the email to client/customer but the format is whats getting me.
(first, one) are strings equaling different text-boxes
my code is:
emailCompseTask.Body = first + one + Enviroment.NewLine +
second + two + Enviroment.NewLine
and so on problem is which i send thru email it shows something like this:
computer service25.00
instead of:
computer service 25.00
is there a way to add spacing to make this more presentable? or even a better way perhaps thanks in advance guys
try this :
emailCompseTask.Body = first + one + " "+ second + two ;
body takes as HTML input, check here for more spacing option.
I'm a bit confused, but you just want to add some spacing in the output? Just throw some spaces in there like you would another variable.
first + " " + one + Environment.NewLine
+ second + " " + two + Environment.NewLine;
You can use a table
string tableRow = #"<tr>
<td>{0}</td>
<td>{1}</td>
</tr>";
string htmlTable = #"<table>
{0}
</table>";
string rows = "";
// Can do this in a loop
rows += string.Format(tableRow, first, one);
rows += string.Format(tableRow, first, one);
emailComseTask.Body = string.Format(htmlTable, rows);

How can I format C# string output to contain newlines?

strEmail =
"Hi All,"
+ "<br>"
+ "The User "
+ lstDataSender[0].ReturnDataset.Tables[0].Rows[0][1].ToString()
+ " has been created on "
+ DateTime.Now.ToShortDateString()
+ "."
I am writing C# code to generate an email whenever a new user has been created. I need the body of the mail in a mail format like
hi,
The user "xxx" has been created on "todaysdate".
Thanks,
yyyy
so I need to insert linebreaks and bold for some characters. How might I achieve this?
If this is a plain text email (which it looks like it is), use \r\n for new lines.
strEmail = string.Concat("Hi All,\r\n\r\nThe User",
lstDataSender[0].ReturnDataset.Tables[0].Rows[0][1].ToString(),
"has been created on ",
DateTime.Now.ToShortDateString(),
".\r\n\r\nThanks, yyyy");
Strickly speaking this should be Environment.NewLine to support different platforms, but I doubt this is a concern.
Set
yourMessage.IsBodyHtml = true
Msdn-Reference can be found here.
In order to form, you can use like below:
Response.Write("Hi,<br/><br/>The user.......");

Categories

Resources