Jump to content

strings into radio buttons

Daniboi
Go to solution Solved by minibois,
3 minutes ago, Daniboi said:

so my task is to make a message box at the end that just says what the user has selected, so if the user picks the colour yellow then it says the colour is yellow

Why not just make button, when pressed it will do this:

string text = "";

if (radiobutton1.Checked == true)
{
text = radiobutton1.text;
}
else if (radiobutton2.Checked == true)
{
text = radiobutton2.text;
}
// etc. etc...

MessageBox.Show(text);

 

so i am trying to get my radio buttons to hold a string so i can use it in a message box later down the line. In the bit of code that i have already writen i have assigned variables like int to the radio buttons and presumed to do the same however because the string that i am assigning it to is null it doesnt seem to like that. Anyone got any ideas? (this is c# windows forms)

 

 

       

using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace TshirtForm
    
    {
    
        
    
        public partial class Form1 : Form
        {
    
            //shirt sizes
    
            decimal sizeCost = 0.00m;
            const decimal extraSmall = 4.50m;
            const decimal Small = 5.00m;
            const decimal meduim = 5.50m;
            const decimal large = 6.00m;
            const decimal extraLarge = 7.00m;
    
            //colour
    
            string red;
            string blue;
            string white;
            string green;
            string purple;
            string yellow;
            string colour;
    
            //material
    
            decimal fabCost = 0.00m;
            const decimal cotton = 1.00m;
            const decimal lycra = 2.50m;
            
    
            //quantity
    
            int quanty = 0;
    
            //printing
    
    
    
            const decimal printingCost = 2.00m;
    
            //misc
    
            decimal cost = 0;
            decimal totalCost1 = 0;
            decimal totalCost = 0;
            string surname = "";
    
    
            public Form1()
            {
                InitializeComponent();
    
            }
    
            private void radioButtonExtraLarge_CheckedChanged(object sender, EventArgs e)
            {
            
                sizeCost = extraLarge;
    
            }
    
            private void radioButtonMeduim_CheckedChanged(object sender, EventArgs e)
            {
                
                sizeCost = meduim;
            }
    
            private void radioButtonGreen_CheckedChanged(object sender, EventArgs e)
            {
                colour = green;
            }
    
            private void radioButtonBlue_CheckedChanged(object sender, EventArgs e)
            {
                colour = blue;
            }
    
            private void radioButtonExtraSmall_CheckedChanged(object sender, EventArgs e)
            {
              
                sizeCost = extraSmall;
            }
    
            private void radioButtonSmall_CheckedChanged(object sender, EventArgs e)
            {
                
                sizeCost = Small ;
            }
    
            private void radioButtonLarge_CheckedChanged(object sender, EventArgs e)
            {
               
                sizeCost = large;
            }
    
            private void radioButtonYellow_CheckedChanged(object sender, EventArgs e)
            {
                colour = yellow;
            }
    
            private void radioButtonPurple_CheckedChanged(object sender, EventArgs e)
            {
                colour = purple;
            }
    
            private void radioButtonWhite_CheckedChanged(object sender, EventArgs e)
            {
                colour = white;
            }
    
            private void radioButtonRed_CheckedChanged(object sender, EventArgs e)
            {
                colour = red;
            }
    
    
            private void radioButtonCotton_CheckedChanged(object sender, EventArgs e)
            {
                fabCost = cotton;
            }
    
            private void radioButtonLycra_CheckedChanged(object sender, EventArgs e)
            {
                fabCost = lycra;
            }
    
    
            private void radioButtonNo_CheckedChanged(object sender, EventArgs e)
            {
                
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                totalCost = printingCost;
    
    
    
    
            }
    
    
            private void buttonOrder_Click(object sender, EventArgs e)
            {
                totalCost = sizeCost * quanty;
                totalCost1 = fabCost * quanty;
    
    
                //fix this
                if (!string.IsNullOrWhiteSpace(textBox1.Text))
                {
                    cost = cost - 2.00m;
                }
    
                cost = fabCost + sizeCost + totalCost;
    
                labelDisplayCost.Text = cost.ToString("c");
    
                  MessageBox.Show("The total cost is " + cost + " and the amount you're ordering is " + quanty + "." +
                "You should collect it on the " + date + ".When collecting please refer to the surname which is" + surname );
            }
    
            private void numericUpDownQuant_ValueChanged(object sender, EventArgs e)
            {
                quanty = Convert.ToByte(numericUpDownQuant.Value);
            }
        }
    }

 

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure where you are doing what (pro tip: if you want people to read your code, please delete all irrelevant stuff and say where the issue is), but have you tried radioButtonName.Text (of course substituting radioButtonName for the name of your radiabutton)?

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, minibois said:

Not sure where you are doing what (pro tip: if you want people to read your code, please delete all irrelevant stuff and say where the issue is), but have you tried radioButtonName.Text (of course substituting radioButtonName for the name of your radiabutton)?

no because it comes up with

 

" The type or namespace name 'radioButtonYellow' could not be found (are you missing a using directive or an assembly reference?)"

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Daniboi said:

no because it comes up with

 

" The type or namespace name 'radioButtonYellow' could not be found (are you missing a using directive or an assembly reference?)"

Oh yeah, I forgot. You can't just call a Form.Controls object, because that can be called from another thread.

You should find a different approach to do what you want to do.

 

The best option will probably creating an instance of a new class at the initialize statement, that will have a dictionary, which holds the product and the quantity. Then you just add products to that list.

Of course product should be a class itself too, which holds the relevant information for a product (name, price, etc.). Then when a user presses 'done' you can generate a receipt based on what is in this products dictionary.

 

The easiest option - but least dynamic - is just making a string and adding product names and prices to it and then just printing that string at the end.

But it really depends on what you want/need to do.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, minibois said:

Oh yeah, I forgot. You can't just call a Form.Controls object, because that can be called from another thread.

You should find a different approach to do what you want to do.

 

The best option will probably creating an instance of a new class at the initialize statement, that will have a dictionary, which holds the product and the quantity. Then you just add products to that list.

Of course product should be a class itself too, which holds the relevant information for a product (name, price, etc.). Then when a user presses 'done' you can generate a receipt based on what is in this products dictionary.

 

The easiest option - but least dynamic - is just making a string and adding product names and prices to it and then just printing that string at the end.

But it really depends on what you want/need to do.

so my task is to make a message box at the end that just says what the user has selected, so if the user picks the colour yellow then it says the colour is yellow

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Daniboi said:

so my task is to make a message box at the end that just says what the user has selected, so if the user picks the colour yellow then it says the colour is yellow

Why not just make button, when pressed it will do this:

string text = "";

if (radiobutton1.Checked == true)
{
text = radiobutton1.text;
}
else if (radiobutton2.Checked == true)
{
text = radiobutton2.text;
}
// etc. etc...

MessageBox.Show(text);

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, minibois said:

Why not just make button, when pressed it will do this:


string text = "";

if (radiobutton1.Checked == true)
{
text = radiobutton1.text;
}
else if (radiobutton2.Checked == true)
{
text = radiobutton2.text;
}
// etc. etc...

MessageBox.Show(text);

 

would that also prevent other inputs from being added. so if the user picks blue and yellow it will say blue and yellow?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Daniboi said:

would that also prevent other inputs from being added. so if the user picks blue and yellow it will say blue and yellow?

If the user select both blue and yellow, with this code it will only show the first color it checked. 

That is because this is an if-else stack. Once the first 'if' is triggered, it will not check any of the other if-else or else options.

 

That way if you check red, then yellow then blue and the user check 'yellow' and 'blue'. It will check red, see it's not checked, go to yellow see it's checked and skip all the other else if and else options.

Furthermore, if you remove all the 'else'  options, it will check everything. But once it reaches 'blue', it will say "text = radiobuttonBlue.Text", that way all the previous text gets removed. 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

My advice would be to not get stuck into using whatever is visible on screen (a property of the radio button, the text) with what you actually put in the string.

You may want to format the radio button text in some way for easier readability, or you may want to add multiple language support but keep the messages in some specific language (like English for example) or you may want to display something on the screen in a specific way but use it somewhere else differently (ex show a price or amount on screen with 2 decimals, but log to file with 3-4 decimals)

 

Something else you can do is to store the long text once in a constant or a string and use some "keywords" in the string where you want certain values to be inserted.

Then, you can reuse that text in multiple places.

IN your specific example you could do something like :

 

constant or string variable  message = "The total cost is %%COST%% and the amount you're ordering is %%QUANTITY%%. You should collect it on the %%DATE%%. When collecting please refer to the surname which is %%SURNAME%%.";

 

So before you have to display it you could say something like:

 

variable text = message

text = replace ( search for = "%%COST%%"  , replace with = variable cost , search in = variable text )

text = replace ( search for = "%%QUANTITY%%"  , replace with = variable quantity , search in = variable text )

text = replace ( search for = "%%DATE%%"  , replace with = variable date , search in = variable text )

text = replace ( search for = "%%SURNAME%%"  , replace with = variable surname , search in = variable text )

 

now you can do msgbox ( variable text , other parameters)

 

note that the message string constant or variable is never changed, you create a new variable text and make the text replaces in the text variable.  You can get the original text again from the message constant and you can use it in other functions, if you define it outside the functions as global constant or something.

 

As for radio buttons, especially for some colors... you can have ARRAYS of radio buttons. The first radio button in the group starts at 0, and the index goes on to 1,2,3,4 ...

 

so you would have a single function private void radioButton_CheckedChanged , but it will have an additional parameter index or something like that, which you can use to differentiate between the actual radio button.

So if you have

0 = small

1 = large

2 = red

3 = blue

4 = brown

in the function you could have

 

dim colors = array ( black , black , red, blue, brown )  // because arrays start at 0, just put some color for 0 and 1 we don't care about

then somewhere in the function you could have

if index >1 and index <5  then   // radio button with index 2, 3 or 4 was clicked

   variable color  = colors[index]   <-  you put in the color variable the color from the array colors ... since values can only be 2,3 or 4 ... you get red , blue or brown

end if

 

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, Daniboi said:

would that also prevent other inputs from being added. so if the user picks blue and yellow it will say blue and yellow?

You cannot have multiple radiobutton selected at the same time. It's only 1 per group. To selected many you need to use a checkbox

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×