Cuadros de lista y cuadros de texto con aplicación de Windows

I read an entire chapter of a book in regards to c# windows applications (Barbara Doyle is the author) and attempted a problem in it. I must not be getting the fundamental principles of listbox and checkboxes correctly.

My question is, for this, how do you clear a listbox. I do not mean pastyBox.Items.Clear();, as that method clears out the entire listbox, but how would I... "reset it", as you would expect if you wanted to start new on a new order (I feel the word reset may be more appropriate)? Also, no matter what I seem to do I cannot clear the textbox, even if I set it to txtBoxTotal = ""; or attempt to make values to zero.

There is something I am completely missing and hoping someone can point it out. Maybe it is a very easy mistake or just some knowledge I do not know. Here is the code with the way I have the checkboxes being cleared, I deleted the few lines of code I had attempted to solve the text box and list box

I thought maybe at the end it was something with it being a selected index since I ran an array through it.... I don't have any clue anymore it seems.

EDIT:: As of now, when I load my application, lets say I click baklava and tea, it shows 3.80 in the text box, when I hit clear it removed the arrow in front of tea, baklava is still selected and the txtbox does not change Im wanting the textbox to clear and the selected baklava to be unselected

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    decimal[] pastryPrice = { 3.00m, 2.85m, 3.25m, 4.35m, 3.40m, 2.30m, 2.90m,
    1.75m,2.00m, 1.50m };
    decimal pastry;
    decimal total;
    decimal drinkCost = 0;

    private void pastryBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (pastryBox.SelectedIndex)
        {
            case 0:
                pastry = pastryPrice[0];
                break;
            case 1:
                pastry = pastryPrice[1];
                break;
            case 2:
                pastry = pastryPrice[2];
                break;
            case 3:
                pastry = pastryPrice[3];
                break;
            case 4:
                pastry = pastryPrice[4];
                break;
            case 5:
                pastry = pastryPrice[5];
                break;
            case 6:
                pastry = pastryPrice[6];
                break;
            case 7:
                pastry = pastryPrice[7];
                break;
            case 8:
                pastry = pastryPrice[8];
                break;
            case 9:
                pastry = pastryPrice[9];
                break;
        }
    }
    private void txtBoxTotal_TextChanged(object sender, EventArgs e)
    {
        total = pastry + drinkCost;
        txtBoxTotal.Text = Convert.ToString(total);
    }
    private void ComputeDrinkCost_CheckedChanged(object sender, EventArgs e)
    {
        if (this.Coffee.Checked)
        {
            drinkCost = 2.00m;
        }
        if (this.Tea.Checked)
        {
            drinkCost = 1.80m;
        }
        if (this.Water.Checked)
        {
            drinkCost = 0.00m;
        }
        if (this.Cream.Checked)
        {
            if ((this.Coffee.Checked) || (this.Tea.Checked))
            {
                drinkCost += 0m;
            }
            else
            {
                drinkCost += .50m;
            }
        }
        if (this.Sugar.Checked)
        {
            if ((this.Coffee.Checked) || (this.Tea.Checked))
            {
                drinkCost += 0m;
            }
            else
            {
                drinkCost += .30m;
            }
        }
        if (this.WhippedCream.Checked)
        {
            drinkCost += .60m;
        }
        if (this.Cocoa.Checked)
        {
            drinkCost += .90m;
        }
        if (this.Cinnamon.Checked)
        {
            drinkCost += .25m;
        }
    }
    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
    private void clearOrderToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.clearDrinks();
    }
    public void clearDrinks()
    {
        Tea.Checked = false;
        Coffee.Checked = false;
        Water.Checked = false;
        Cream.Checked = false;
        Sugar.Checked = false;
        Cinnamon.Checked = false;
        WhippedCream.Checked = false;
        Cocoa.Checked = false;
    }
}
}

preguntado el 10 de septiembre de 13 a las 00:09

I didnt really get your question but try binding empty list item to the list -

FYI, you have an extra closing brace in there. I left it in because I don't believe in making substantive code changes in an edit, especially to a question, but after formatting the code properly it becomes apparent (and my code editor confirmed: 22 opening braces, 23 closing braces). -

He editado tu título. Por favor mira, "¿Deben las preguntas incluir "etiquetas" en sus títulos?", donde el consenso es" no, no deberían ". -

Im reading it right now @JohnSaunders -

set SelectedItem = null ? -

2 Respuestas

I'm not sure what you mean by 'resetting' the listbox but you could use

pastryBox.Items.Clear();
pastryBox.Items.AddRange(pastryPrice)

This gets the listBox to a set state if there have been edits on it.

También puedes usar

txtBoxTotal.Text = string.Empty;

or

txtBoxTotal.Text = "0";

It would be better to data bind but you will need to read up on it. HTH.

Respondido el 10 de Septiembre de 13 a las 00:09

Para limpiar el Textbox

txtBoxTotal.Text = ""

Para el listbox

I don't understand what you are trying to ask? Just make your question more clear..........

Respondido el 10 de Septiembre de 13 a las 00:09

made an edit to maybe make more clear, I attmpted the solution you offered earlier, and it did not work. The data still remains the same in the txtbox - Zoro

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.