locked
Label not updating text value? Unless clicked? RRS feed

  • Question

  • Hi all!
    Sorry me again!


    I currently have a listbox,

    This listbox gets items added it it which are clicked from a combo box, bit like a basket.

    Now I have a label and it has this code-

    label5.text = listbox1.items.count

    Now, i test it each time i add a item to listbox the label does not update, if I click it once with my mice it suddnely update correctly.


    It's got no click event, so not sure whats up.

    Any help please?

    Thanks!


    Thursday, June 26, 2008 8:23 PM

Answers

  • Exactly what I suspected.

     

    You are adding items to the listbox when the combobox selected index is changed.  What you need to do is only add them when the index change is committed.  To understand why click on the arrow on the combobox to drop the list and then scroll down the list with the down arrow.  You will find that every item you pass through is added to the list.  If you move that code to the SelectionChangeCommitted event then items will only be added when you click on an item in the list or select it with the Up/Down keys and then press Enter.

     

    Then move the code you currently have in the labels textchanged event into the same place and it should work ok.

     

    Your problem seems to be understanding what an event is.

     

    Most controls have a large number of events which occur, generally, when the user does something (but not always!).  e.g. the combobox SelectedIndexChanged event fires whenever the highlight in the combobox moves to a different item.  When the event fires any code you have put in the event handler will run.  You can see a full list of the events for a control by selecting the control from the drop down list at the top left of the code editor and then drop down the events list in the right hand drop down.   The reason your attempt to use the TextChanged event for the label doesn't work is that the event does not fire (and so your code won't run) until the text in the label changes.  As suggested above move that code into the combobox SelectionChangeCommitted event handler and then put a messagebox.show line in the label text changed and you will find that the text changes ok and that change then causes the messagebox to be shown.

    Thursday, June 26, 2008 11:01 PM

All replies

  • hmm think I worked it out.

    Cause i access code for every new control with a double click, it's attached a CLICK as default event.

    I've removed it and changed it to TEXT CHANGED event

    Now it just stays as a 0


    Any suggestions.

    Tahnks

    Thursday, June 26, 2008 8:26 PM
  • You don't write code for a control - you write it to handle a control's events.

     

    In this case you want to update the text property of the label when you add an item to the listbox by clicking on a combobox.  That is not an event of the label.  The event you need is the selection of an item on the combobox.  From what you have said I suspect you have written the code to add the item to the listbox in the combobox SelectedIndexChanged event handler, which is the default presented if you just double click the control.  Although that will appear to work if you just use the mouse you should investigate what happens if you scroll through the combobox with the up/down keys.

     

    A better event to use would be the SelectionChangeCommitted event.  And if you put your label5.text = ....... in there as well you should find it updates correctly.

     

    Thursday, June 26, 2008 9:32 PM
  • hmmm I'm starting to get confused now.. and think my code is all wrong.
    This is what I got LOL might look like *** to you but im learning

    [code]

     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            ListBox1.Items.Add(ComboBox1.SelectedItem)
        End Sub

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ComboBox1.Items.Add("Cheese And Tomato")
            ComboBox1.Items.Add("Pepperoni")
            ComboBox1.Items.Add("Vegi")
            ComboBox1.Items.Add("Meat Feast")
            ComboBox2.Items.Add("Garlic Bread")
            ComboBox2.Items.Add("Chicken Dippers")
            ComboBox2.Items.Add("Potato Wedges")
        End Sub

     Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
            ListBox1.Items.Add(ComboBox2.SelectedItem)
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ListBox1.Items.Remove(ListBox1.SelectedItem)
        End Sub

        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        End Sub

        Private Sub Label5_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label5.TextChanged
            Dim price As Integer
            price = ListBox1.Items.Count
            Label5.Text = price * 10 '10 pound per pizza added
        End Sub
    End Class

    [/code]
    Thursday, June 26, 2008 10:36 PM
  • Exactly what I suspected.

     

    You are adding items to the listbox when the combobox selected index is changed.  What you need to do is only add them when the index change is committed.  To understand why click on the arrow on the combobox to drop the list and then scroll down the list with the down arrow.  You will find that every item you pass through is added to the list.  If you move that code to the SelectionChangeCommitted event then items will only be added when you click on an item in the list or select it with the Up/Down keys and then press Enter.

     

    Then move the code you currently have in the labels textchanged event into the same place and it should work ok.

     

    Your problem seems to be understanding what an event is.

     

    Most controls have a large number of events which occur, generally, when the user does something (but not always!).  e.g. the combobox SelectedIndexChanged event fires whenever the highlight in the combobox moves to a different item.  When the event fires any code you have put in the event handler will run.  You can see a full list of the events for a control by selecting the control from the drop down list at the top left of the code editor and then drop down the events list in the right hand drop down.   The reason your attempt to use the TextChanged event for the label doesn't work is that the event does not fire (and so your code won't run) until the text in the label changes.  As suggested above move that code into the combobox SelectionChangeCommitted event handler and then put a messagebox.show line in the label text changed and you will find that the text changes ok and that change then causes the messagebox to be shown.

    Thursday, June 26, 2008 11:01 PM