I am trying to display 3 values in a message box when the user clicks Submit as a means to verify correct input.
The 3 values come from comboBoxes in the form: age, height, weight.
In my current set up, the box will only say "age:" with the actual numerical value in the top border.
How can I get the 3 comboBox data items to appear inside a message box with appropriate titles?
Like this:
Age: 27
Height: 62
weight: 180
Data are stored in the variables age_Num.Text, height_Num.Text, and weight_Num.Text
MessageBox.Show("Age:", age_Num.Text); //just shows "Age:". Value is in titlebar of mb
In case of a ComboBox you can get the selected text through the ComboBox.SelectedText property.
To build a string from multiple values you can use String.Format().
string age = age_num.SelectedText;
string height = height_Num.SelectedText;
string weight = weight_Num.SelectedText
string text = String.Format(
"Age: {0}, Height: {1}, Weight: {2}", age, height, weight);
MessageBox.Show(text);
You must concatenate those values into a single string. Try this, using StringBuilder:
StringBuilder MessageText = new StringBuilder();
MessageText.AppendLine(string.Format("Age: {0}", age_Num.Text));
MessageText.AppendLine(string.Format("Height: {0}", height_Num.Text));
MessageText.AppendLine(string.Format("Weight: {0}", weight_Num.Text));
MessageBox.Show(MessageText.ToString());
You can use values of combo directly,
MessageBox.Show("Sometext 1:" + cbo1.SelectedValue.ToString() + " Sometext 2:" + cbo2.SelectedValue.ToString() + " Sometext 3:" + cbo3.SelectedValue.ToString());
or as you already have in variables.
MessageBox.Show("Age: " + age_num.Text + " Height: " + age_num.Text + " Sometext 3: " + weight_Num.Text);
If you don't like any of the above, simply create a dialog form set up as you want, with static function to show it modally.
Related
Im a noob and i want make a letter format for this im getting name,contactno and adress and i want to show the user input with the place holder before them in the right of the screen, i tried it useing padRight but there is a issue , as im giving a fix number , when the user input is long then the remaing input shifts to the secong line
Console.WriteLine(" ".PadRight(100) + "Name: " + name);
Console.WriteLine(" ".PadRight(100) + "Contact No: " + contact_no);
Console.WriteLine(" ".PadRight(100) + "Address: " + adresss);
and if there is a better way of doing this please let me know
Currently I am attempting to create a dictionary which maps a selected item form a list view with a corresponding string output in a rich text box. I would like to bold specific text in the string, which will always be the same (constant) and also adding dynamic text to the string that would change.
Something like this:
ID: 8494903282
Where ID is the constant text I need bolded and the numbers would be a dynamic ID that changes. I will need to have multiple lines with different data in this format which will be changing:
ID: 8494903282
Name: Some Name
Date: 3/15/2018
Currently I have a rich text box to output to and I am trying to use some string formatting to do what I want but this is not working correctly. Essentially I need a string value I can store in a dictionary so when an item gets selected I can just set the rtf property of the text box to the value of that dictionary item.
Below I have my current format string I am attempting to set the rtf property to:
string s1 = string.Format(#"{{\rtf1\ansi \b Commit ID: \b0 {0}\line}}", entry.ID);
string s2 = string.Format(#"{{\b Author: \b0 {0}\line}}", entry.Author);
string s3 = string.Format(#"{{\b Date: \b0 {0}\line}}", entry.Date.ToString("d"));
string s4 = Environment.NewLine + Environment.NewLine + entry.Message;
contents = (s1 + s2 + s3 + s4);
Then setting the rtf property of my rich text box:
LogContentsTB.Rtf = Logs[LogNamesLV.SelectedItems[0].Name];
Where logs is a dictionary of the form < string, string > that holds the format string for the specific item.
However, I get the following output rather than my expected output:
This is the correct form of output for the first item but nothing else appears. If there are any other ways to do this I am open to suggestion.
After doing some light reading on the rtf syntax I noticed that I was trying to close off each string with curly braces. Curly braces are used for RTF groups. For some reason the rich text box in windows forms did not play well with that.
Another thing to notice is that the string.format method was probably the main culprit for cause of issues with this type of formatting. In my answer I do not use it but rather just add the string directly into the rtf formatted string i.e. < format >< variable string >< ending format >
If you look at NetMage's response, you will notice he only puts an opening brace on the very first string, s1. This is to group the whole string. But we need to add a closing brace on the final string, s4, to finish the grouping. Below is the final code and screenshot that worked for my application.
string s1 = #"{\rtf1\ansi\b ID: \b0 " + entry.ID + #" \line\line";
string s2 = #"\b Author: \b0 " + entry.Author + #" \line\line";
string s3 = #"\b Date: \b0 " + entry.Date.ToString("d") + #" \line\line ";
string s4 = entry.Message + #"}";
contents = s1 + s2 + s3 + s4;
Thanks for pointing me in the right direction!
I think your RTF formatting is wrong. You could try:
string s1 = string.Format(#"{{\rtf1\ansi\r\b Commit ID:\b0 {0}\line\r", entry.ID);
string s2 = string.Format(#"\b Author: \b0 {0}\line\r", entry.Author);
string s3 = string.Format(#"\b Date: \b0 {0}\line\r", entry.Date.ToString("d"));
string s4 = Environment.NewLine + Environment.NewLine + entry.Message + "}}";
contents = (s1 + s2 + s3 + s4);
void ClearAllRichtextboxes()
{
richTextBox3.Clear();
richTextBox5.Clear();
richTextBox6.Clear();
richTextBox9.Clear();
richTextBox10.Clear();
}
ClearAllRichtextboxes();
if (comboBox5.Text == "Primer")
{
richTextBox5.Text = "This is the number of primer tins" + primer.ToString();
richTextBox6.Text = "This is the cost of the primer tins" + primercost.ToString();
}
if (comboBox3.Text == "Matt")
{
richTextBox10.Text = "This is how many 2.5 tins of paint are needed: " + val44.ToString();
richTextBox9.Text = "This is the matt cost" + valmatt.ToString();
}
if (comboBox3.Text == "Vinyl ")
{
richTextBox10.Text = "This is how many 2.5 tins of paint are needed" + val44.ToString();
richTextBox9.Text = "This is the of vinyl cost" + valmatt.ToString();
}
if (comboBox3.Text =="Silk")
{
richTextBox10.Text = "This is how many 2.5 tins of paint are needed" + silkval.ToString();
richTextBox9.Text = "This is the cost: " + valcostsilk.ToString();
}
Currently I am inserting text into multiple textboxes, instead I would like to output variables in one rich text box - by appending the strings.
You could do something like string formatting to help space the strings with spaces.
something like
richTextBox1.Text = String.Format("This is the number of A in B: {0}\r\n This is the number of X in Y: {1}", output1, output2);
\r\n indicates a new line, you can find more information about the String.Format() method on msdn:
https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx
Update the richTextBox.Text with the new information. If you want to append the new strings to what is already there use "+". You can save the string as its own variable if it helps.
richTextBox.Text = "First segment.";
richTextBox.Text = richTextBox.Text + " Second segment.";
More info about string concatenation: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/how-to-concatenate-multiple-strings
I'm creating a standard calculator with a history feature. Previous solutions will show in a label box every time the user clicks the "=" button
After converting this string:
string histo = (operand1 + " " + operation + " " + " " + operand2 + " = " + result);
To a list by using this code:
List<string> hist = histo.Split().ToList()
What I want to do next is to print it to a label box to show history. How can I do that? Thank you.
Your question is not very clear and I do not think that label is a good choice to show the history. Combobox would be a better option, if you want to allow users to select items from history.
As far as your question on how to display it in a label is concerned you can use the following code. You can replace "," with Environment.NewLine.
lbl.Text = String.Join(",", hist);
I'm trying to create a program wherein the users will provide their birthdate from multiple combo boxes. One for month, then date, and third is for year. And combine their values into one string. So far, here's my code:
string bdate = " "+ bday_month.SelectedText + " " + bday_date.SelectedText + ", " + bday_year.SelectedText;
MessageBox.Show(bdate);
The problem with this is that when I have values for all combo boxes, bdate only displays the third combo box value. See below:
Any idea?
Use the ComboBox.Text property instead of the ComboBox.SelectedText:
string bdate = " " + bday_month.Text + " " + bday_date.Text + ", " + bday_year.Text;
MessageBox.Show(bdate);
The ComboBox.SelectedText property returns the text highlighted in the editor, but not the entire text.
comboBox.SelectedText
is a value indicating the currently selected text in the control and
comboBox.Text
is the current text in the ComboBox
so use .Text instead