Question radio button and checkboxes

jag250

Active member
Joined
Sep 16, 2020
Messages
28
Programming Experience
1-3
reset checkbox and radiobuttons:
rbPlain.Checked = false;
rbWheat.Checked = false;
rbEverything.Checked = false;
cbToasted.Checked = false;
cbCreamCheese.Checked = false;
Can anyone help me with resetting radio buttons and checkboxes? I need them to rest if I click the back button.


Also I need help with making the statement say bagel not bagels if (txtNumofBagels.Text != "1")

string strbagelName = "bagels";
string strNumberofBagels = txtNumofBagels.Text;
string strTypeofBagel = lblTypeof.Text;
string strToastedorNot;
string strCreamCheeseorNot;

C#:
  if (rbPlain.Checked == true)
        {
            strTypeofBagel = " Plain ";
        }

        else if (rbWheat.Checked == true)
        {
            strTypeofBagel = "Wheat ";
        }


        else if (rbEverything.Checked == true)
        {
            strTypeofBagel = "Everything ";
        }


        // determine what word goes in for strbagelName

        if (txtNumofBagels.Text != "1")
        {
            // add the word bagels


        }
        else
        {

        }

        // determine what word goes in for strToasted
        if (cbToasted.Checked == true)
        {
            strToastedorNot = "Toasted ";
        }
        else
        {
            strToastedorNot = "";
        }
        if (cbCreamCheese.Checked == true)
        {
            strCreamCheeseorNot = " with Cream Cheese";
        }
        else
        {
            strCreamCheeseorNot = "";
        }


        // determine what word goes in for strCreamCheese



        lblTypeof.Text = strToastedorNot + strTypeofBagel + strbagelName + strCreamCheeseorNot;
        //                  "Toasted"      "Wheat"        "bagels"       "with cream cheese"
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,217
Location
Sydney, Australia
Programming Experience
10+
Can anyone help me with resetting radio buttons and checkboxes? I need them to rest if I click the back button.
The obvious answer is that you put the code you already have in the Click event handler of the Back button. If that's not good enough then there's clearly relevant information that you're not giving us.

That said, this is an example of why you should separate data (the model) from presentation (the view). Ideally you would have a class that represents the data you want to display and your controls would simply display the state of an instance of that class. When you click the Back button, you reset the state of that object and then the controls simply update to reflect that state. The controls don't care about why that state changes. The view simply represent the current state of the model, whatever and why ever that is.
Also I need help with making the statement say bagel not bagels if (txtNumofBagels.Text != "1")
E.g.
C#:
var text = count == 1 ? "word" : "words";
If you have a number of words that may need pluralising and especially if some of those plurals don't simply append an "s" then you might create a Dictionary of words and their plurals and a method to return the appropriate form, e.g.
C#:
private Dictionary<string, string> pluralsByWord = new Dictionary<string, string>
                                                   {
                                                       {"person", "people"},
                                                       {"man", "men"},
                                                       {"woman", "women"},
                                                       {"boy", "boys"},
                                                       {"girl", "girls"},
                                                       {"baby", "babies"}
                                                   };

private string GetAppropriateForm(string word, int count)
{
    return count == 1 ? word : pluralsByWord[word];
}
You can then use this:
C#:
var text = GetAppropriateForm("woman", count);
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,679
Location
UK
Programming Experience
10+
Maybe start off with something like this. It is only pseudo code So I don't know how much of it will work, but its a pointer in the direct direction with examples for your questions in the code :
C#:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
        public delegate void Delegate_Callback_To(string itm_Value, Control control);

        public void UpdateUI_With_NewItem(string value, Control control)
        {
            control.Text = value;
        }
        public FoodRequests GetFood = new FoodRequests();
        public Orders GetOrders = new Orders();
        private void OrderButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(GetFood.FoodType))
            {
                Debug.WriteLine("Order something first");
            }
            else
            {
                Task updateOrderTask = new Task(UpdateOrder);
                updateOrderTask.Start();
            }
        }
        private void UpdateOrder()
        {
            string WithCream = "With No Cream"; string Toasted = "Not Toasted";
            if (GetFood.Toasted == CheckState.Checked)
            {
                Toasted = "Toasted";
            }
            if (GetFood.WithCream == CheckState.Checked)
            {
                WithCream = "With Cream";
            }
            List<FoodRequests> currentOrder = GetOrders.AddToOrder(GetFood);
            StringBuilder sb = new StringBuilder();
            strTypeofBagel.Invoke(new Delegate_Callback_To(UpdateUI_With_NewItem), sb.Append(string.Concat(GetFood.FoodItem, " : ", GetFood.FoodType, " ", Toasted, " ", WithCream)).ToString(), strTypeofBagel);
        }
        private void plainbagel_CheckStateChanged(object sender, EventArgs e)
        {
            GetFood.FoodItem = nameof(Food.Bagel);
            GetFood.FoodType = Food.Bagel.Plain.ToString();
        }
        private void wheatBagel_CheckStateChanged(object sender, EventArgs e)
        {
            GetFood.FoodItem = nameof(Food.Bagel);
            GetFood.FoodType = Food.Bagel.Wheat.ToString();
        }
        private void WithCream_CheckStateChanged(object sender, EventArgs e)
        {
            if (WithCreamCB.Checked && ToastedCB.Checked)
            {
                Debug.WriteLine("You can't have this item toasted with cream.");
                WithCreamCB.Checked = false;
                /* Offer the user the opertunity to switch check states */
            }
            else
            {
                GetFood.WithCream = WithCreamCB.CheckState;
            }
            GetFood.WithCream = WithCreamCB.CheckState;

        }
        private void toasted_CheckStateChanged(object sender, EventArgs e)
        {
            if (ToastedCB.Checked && WithCreamCB.Checked)
            {
                Debug.WriteLine("You can't have this item toasted with cream.");
                ToastedCB.Checked = false;
                /* Offer the user the opertunity to switch check states */
            }
            else
            {
                GetFood.Toasted = CheckState.Checked;
            }
            GetFood.Toasted = ToastedCB.CheckState;
        }
    }
    public abstract class Food
    {
        public enum Bagel
        {
            Plain,
            Garlic,
            Wheat,
            Everything
        }
    }
    public class FoodRequests : Food
    {
        public string FoodItem { get; set; }
        public string FoodType { get; set; }
        public CheckState Toasted { get; set; }
        public CheckState WithCream { get; set; }
        public Tuple<string, string, CheckState, CheckState> PickItem(FoodRequests ordered_Item)
        {
            return new Tuple<string, string, CheckState, CheckState>(ordered_Item.FoodItem, ordered_Item.FoodType, ordered_Item.Toasted, ordered_Item.WithCream);
        }
    }
    public class Orders
    {
        private List<FoodRequests> ListOfOrders = new List<FoodRequests>();
        public List<FoodRequests> AddToOrder(FoodRequests foodRequests)
        {
            /* Process your order */
            ListOfOrders.Add(foodRequests);
            return ListOfOrders ?? throw new ArgumentException($"{nameof(ListOfOrders)} is null or empty.");
        }
    }
I don't have time to explain the code, but if you have any questions, I will get to them when I can find my feet again. One suggestion if you use this, perhaps make the Orders class Abstract, and as I did with Food, then create your new class inheriting the Orders class, and add the types of Orders you will receive to your new class... You will notice that line 41 should update your UI from another task. If you are not using models as already suggested, you should be updating your UI with that example. By reasons already explained by @jmcilhinney.

5 AM in the morning here. And I'm up in 2 hours. Have a good night!
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,959
Location
Chesapeake, VA
Programming Experience
10+
I suspect that the "Back" button that the OP is talking about is the browser's "Back" button. The code here looks to be the same code he had in his other thread which dealt with WebForms.
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,679
Location
UK
Programming Experience
10+
Sigh

Is the topic about web apps or winforms?

If the former, it should be moved... It should also be expressed in the opening topic what your project type is. If we are not told, you may get misleading advice. There are no mind readers here.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,217
Location
Sydney, Australia
Programming Experience
10+
Sigh

Is the topic about web apps or winforms?

If the former, it should be moved... It should also be expressed in the opening topic what your project type is. If we are not told, you may get misleading advice. There are no mind readers here.
To be frank, it should be moved either way, given that there are dedicated forums for both. I know that some people are the opposite but, unless it's obviously Web Forms, I always assume WinForms if it's not stated. Unfortunately this is yet another lesson the OP needs to learn about posting "properly". One of these days we'll get a question in the right place that contains all the relevant information formatted properly.
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,679
Location
UK
Programming Experience
10+
Moved to web forms based on the pointer on p1/#4

unless it's obviously Web Forms, I always assume WinForms if it's not stated.

Well, I do too (almost all the time). And I guess this will teach the OP to be more specific when posting projects in the future. None the less, the advice given and the pseudo code can still be adapted and used if they want to use it. Very few tweaks will be required.
One of these days we'll get a question in the right place that contains all the relevant information formatted properly.
Now wouldn't that be a blessing. Perhaps when we place guidelines for people to follow. I'm sure this will help with situations like this.
 
Top Bottom