Put information in label based on what user picked. c# [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am currently trying to get a label(lets name it lblMessage) to pull information based on what the user has picked threw radio buttons and check boxes. Lets make the theme a sundae maker form.. people can select there flavor, size, and addons.
I'm just trying to make it so the label displays once they click the confirm button, a message that identifies the number of sundaes, sundae flavor and size the person ordered. like.. (You ordered 2 small hot fudge sundaes which will cost you
$x.xx (where x.xx is the cost for all sundaes ordered). I tried looking this up but i couldn't seem to find what to put into the label code... =\
I am a beginner at C# ... If any other information is needed, I can provide.

What you want to do is set the text of the label. You can use the String.Format method to take a string and replace certain values with values from variables, for example:
var result = MessageBox.Show("Are you sure?", "Are you sure?", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
Decimal total = int.Parse(textBox1.Text) * 1.25m;
// The {0} will be replaced with the first argument after the format string.
// The total.ToString("C") tells the decimal to format the string into a currency string (http://msdn.microsoft.com/en-us/library/fzeeb5cd(v=vs.110).aspx)
label1.Text = String.Format("You ordered {0} small hot fudge sundaes, which will cost you {1}", textBox1.Text, total.ToString("C"));
}
Now this example still fails if the textbox does not contain an int, but that can easily be handled using the TryParse method.

Related

String to decimal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The application that I've made I've got a text box called service cost. This allows the user to enter the cost of the service they have provided and this is decimal. I'm trying to get this service cost displayed in a DGV, I've got everything else working apart from this.
currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost);
The above is the code that I currently have, is there something wrong that I've done here?
From the question it is clear that txtServiceCost is the TextBox, and Convert.ToDecimal() expects a string as input so you should use txtServiceCost.Text instead for txtServiceCost. Since txtServiceCost is a control where as txtServiceCost.Text is a string
currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost.Text);
But i would like to suggest you to use decimal.TryParse
decimal userInput;
if (!decimal.TryParse(txtServiceCost.Text, out userInput))
{
// Throw some warning here that invalid input
}
currentComputer.ServiceCost = userInput;

I need to convert a bit[] into an [] of strings that represent each bit in the bit[] [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm working on an automated follow up system on highly antiquated techniques of registering the current status of a "Project" the GUI interaction in this system uses what are named "flags" that the user can "check" to designate the current status of a project. There are 11 possible boxes that can be checked and the system accepts multiple selections.
For example a user can select a check-box labeled "Confirmed" and or "Needs Follow Up" and or or "Is Scheduled" and or "Spoken with client" (There are 11 possible selections).
Here is the problem - whoever wrote this saved those selections to the database in a "bit sum" so the what you see is an int of the original bit[] for the check-box selections.
What I need to do is read the integer from the database and turn it back into a bit array of 11 values of 1 || 0 then from that bit array i need to determine which boxes of string value are checked in order to determine weather or not i need to perform an automated follow up.
So basically if "Confirmed" is checked i don't want to follow up
If "Needs-followup" is checked i need to follow up.
The problem here is that multiple selections can be present.
So after the int is turned into a bit[] we have for example
1,0,1,0,0,0,1,1,1,0,1 where each int represents a box checked.
i need to find a way to turn the above into an array of strings representing the box labels to determine which boxes are checked.
The usual way to do this in C# would be to use a flags enumeration - this is exactly what your bit field is.
[Flags]
enum ProjectStatus
{
Confirmed = 1,
NeedsFollowUp = 2,
SpokenWithClient = 4,
....
}
To test if a specific flag is set:
ProjectStatus status = (ProjectStatus)intFromDb;
if( ( status & ProjectStatus.Confirmed ) == ProjectStatus.Confirmed )
// the Confirmed flag is set
There is also a Enum.HasFlags extension method that simplifies this if you are in .NET 4 or higher.
If you do not wish to do it this way, you can find out if the bit at position x is set by doing this:
bool isSet = ( intFromDb & ( 1 << x ) ) != 0;
And use that to build your string.
Edit: I'd also suggest you read up a bit on bitwise operators and what they do. This might be a good start: http://blackwasp.co.uk/CSharpLogicalBitwiseOps.aspx

Put a "," sign on the number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i wanna ask. How do i put "," sign on the number whenever the number length is more than 3, then add 1 "," sign.
for example:
I have a number "100000000", and i want computer display it as "100.000.000,00", how do i do that?
Here is the image:
In picture above, shown that the SubTotal is "10000", i want computer display it as "10.000,00" and the Total beside SubTotal is "10000000", i want computer display it as "10.000.000,00".
My question is: how do i do that?
Thanks
A lot of it depends on the control(s) you are using. If you're using plain text boxes you can just set the format when setting the Text value:
txtbox1.Text = total.ToString("N2"); // numeric with separators and 2 decimal places
Other third-party controls let you choose the format with a property such as NumberFormat. Grid controls usually set the format on a column rather than an individual cell.
You should use
amount.ToString("N");
If you're doing it programmatically:
int myNumber = 10000000;
string output = String.Format("{0:n2}", myNumber);
or
int myNumber = 10000000;
string output = myNumber.ToString("n2");
The number after n is the number of decimal places (which can be 0 if you want).
Or you might need to set a format string of a user control to "n2" (without quotes) depending on how you are displaying the numbers.
Have a look at standard numeric format strings and custom numeric format strings on MSDN.
You should use the numeric format specifier to achieve what you want:
number.ToString("N", CultureInfo.InvariantCulture);

Finding a set of numbers equals to the number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to write a c# program that does following:
Takes two parameters: a list of objects and a number
Iterates through the list and find the first set of objects that equals to the number.
Stop iteration if a set is found and the return the set.
So, I have a list of user defined object, say Person as an example. Say, Person object has two fields, Name and age. For example,
MyList
- Person1: John, 10
- Person2: Mary, 25
- Person3: Mike, 35
- Person4: Ann, 20
- Person5: Joe, 5
I want to find a set from the list that equals to the number that I am passing in. If I am passing in above list and 50, I want to return Person2, Person4, Person5 as a list.
This is subset sum problem.
Unfortunately, it is NP-Complete, so there is no known polynomial solution for it.
However, it does have a pseudo-polynomial solution, using Dyanamic Programming, that is using the next recursive function:
f(i,0) = true
f(0,k) = false (k != 0)
f(i,k) = f(i-1,k) or f(i-1,k-weight[i])
Running with f(n,W) yields true if such solution exists, and false otherwise.
The dynamic programming solution fills up a table, after the table is full, you need to "retrace" your steps from table[n][W] back in order to find which items should be included in the set.
More information on getting the actual elements from the table can be found in this thread

Validating exact length of value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am using Microsoft Visual Developer C#. I am trying to validate a textbox so that the Book Code (int) entered is exactly 4 characters long. I used the Range Validator control to do so. For the MaximumValue and MinimumValue properties of the Range Validator I made them both equal 4. However this doesnt seem to work. Am I doing it wrong?
This is very simple, you should probably be thinking more about your problem before posting. However, I will empathize with a beginner and give a couple of solutions.
option 1 - convert to a string and check it's length;
string myVar = BookCode.ToString()
if (myVar.Length < 5)
// it's good!
else
// ERROR
option 2 - the largest value less than ten thousand is 9999, a four digit value.
if (BookCode < 10000)
// it's good
else
// it's bad
If you're just having users enter text into a text box and once they press some type of submit button you want to confirm that the text is 4 characters long then you can check that with inputControl.Text.Length == 4
from there you can show a message box and return if it's not equal to 4 or continue if it is.

Categories

Resources