Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Me:A VB Beginner:Problem:ListBox

yauchildchewyauchildchew Member Posts: 19
I am making a [b]ListBox[/b], i have Buttons to add and remove the items.

To add a item, i will type in the text in a [b]Textbox1[/b] and then select its category from a [b]ComboBox[/b]. After i click Add, it will be shown in [b]ListBox[/b]. Also by clicking Add, it will update a text in another textbox; [b]TextBox2[/b]. Basically, its like this:]

(By selecting "Asian", a mark of 2: will be given. 3 for american.)

i type in TextBox1:"Chew"
then i select from ComboBox:"Asian"
click Add
RESULT
ListBox.Index(0)= " (Asian) Chew "
TextBox2="2"

i type in TextBox:"James"
then i select from ComboBox:"American"
click Add

ListBox.Index(0)= " (Asian) Chew "
ListBox.Index(1)= " (American) James"
TextBox2="5"

and when i enter another "Asian" again, it will becomes
TextBox2="7"

I can refer to the ComboBox.text to determine whether it is 2 or 3 will be added into TextBox2.text
HOWEVER when i need to Remove the items in the ListBox, i want the point of TextBox2 to be updated (deducted) too, accordingly.

I wish to do it this way;
When I click Remove,
it will check the selected item in ListBox,
if the selected item begins with (Asian),
the TextBox2 will be deducted by 2
else if it begins with (American),
TextBox2 will be deducted by 3.

I do not know how to refer to the Item in the ListBox,
I also do not know much the way to do it.

Someone out there please HELP!

Thanks in advance :)
«1

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    Use something like the following for delete:
    [code]
    If (List1.SelectedIndex <> -1) Then
    If List1.List(List1.SelectedIndex) Like " (Asian)*" Then
    Text2.Text = CLng(Text2.Text) - 2
    End If
    End If
    [/code]
    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • yauchildchewyauchildchew Member Posts: 19
    Thank you Richard for your spontaneous reply!
    I see you have been helping lots of beginners like me here!!

    Well, first of all, sorry because i didnt state that i am using VB 2005 Express Edition which i believe is quite different than VB 6.0.

    [b]So, this VB 2005 EE is similar to VB .NET, isnt it?[/b]

    Due to that, i have some problem with your codes.

    [color=Blue]If (List1.SelectedIndex <> -1) Then
    If [b][color=Black]List1.List[/color][/b](List1.SelectedIndex) Like " (Asian)*" Then
    Text2.Text = CLng(Text2.Text) - 2
    End If
    End If[/color]

    the bolded black line is where the error occurs and the hint said "List is not a member of 'System.Windows.Forms.Listbox'"

    anyway, you codes did help me to solve my problem :)
    i believe the codes of "Like "(Asian)*",
    is to check if the Text begins with (Asian) and * represents the following random text.

    With this, i have managed to solve my problem! Really thankful!

    Anyway, i still have some queries:

    [color=Blue]i have found out that someone deletes items of a ListBox with such codes:

    [italic]ListBox1.Items.Remove(ListBox1.SelectedItem)[/italic]

    So, this code and yours perform exactly the same task of deleting items in ListBox and i do not see any difference, is there?

    So far i am only able to delete one item at a time although i select more than one item in the ListBox. How can i delete more than one item at a time?[/color]

    And would you mind please recommend a reference book about VB 2005 EE to me?

    Thanks!









  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : Thank you Richard for your spontaneous reply!
    : I see you have been helping lots of beginners like me here!!
    :
    : Well, first of all, sorry because i didnt state that i am using VB
    : 2005 Express Edition which i believe is quite different than VB 6.0.
    :
    : [b]So, this VB 2005 EE is similar to VB .NET, isnt it?[/b]

    VB 2005 EE is VB.NET, or a version of it. Microsoft 'extends' the language slightly in each version - and also the .NET framework gets a bit updates. But mostly, it refers to the IDE (the design environment).

    :
    : Due to that, i have some problem with your codes.
    :
    : [color=Blue]If (List1.SelectedIndex <> -1) Then
    : If [b][color=Black]List1.List[/color][/b](List1.SelectedIndex)
    : Like " (Asian)*" Then
    : Text2.Text = CLng(Text2.Text) - 2
    : End If
    : End If[/color]

    Yeah, in VB.NET it's become .Items, and also Items is a Collection-derived class, whereas in VB it was just a String-array.

    :
    : the bolded black line is where the error occurs and the hint said
    : "List is not a member of 'System.Windows.Forms.Listbox'"
    :
    : anyway, you codes did help me to solve my problem :)
    : i believe the codes of "Like "(Asian)*",
    : is to check if the Text begins with (Asian) and * represents the
    : following random text.
    :
    : With this, i have managed to solve my problem! Really thankful!

    Good to hear :)

    :
    : Anyway, i still have some queries:
    :
    : [color=Blue]i have found out that someone deletes items of a ListBox
    : with such codes:
    :
    : [italic]ListBox1.Items.Remove(ListBox1.SelectedItem)[/italic]
    :
    : So, this code and yours perform exactly the same task of deleting
    : items in ListBox and i do not see any difference, is there?
    :

    My code did not delete - it just found the index of the item and modified the textbox value (I assumed you knew how to delete yourself, so I did not add it).

    : So far i am only able to delete one item at a time although i select
    : more than one item in the ListBox. How can i delete more than one
    : item at a time?[/color]

    Try something like:
    [code]
    While ListBox1.SelectedItems.Count > 0
    ListBox1.Items.Remove(ListBox1.SelectedItems.Item(0))
    End While
    [/code]

    :
    : And would you mind please recommend a reference book about VB 2005
    : EE to me?
    :
    : Thanks!
    :

    I haven't a clue tbh. I started VB6, got a really neat beginners book (but I wouldn't advise it anymore because it doesn't handle .NET). I've only actually programmed in .NET for a couple of months now - it's quite similair to VB6 in Syntax and also the 'framework' much resembles the way it used to be.
    However, my experience with some C++ object oriented code has helped me a lot.
    And I still find myself browsing through the MSDN Library half of the time, looking for specific information to solve problems I have - BECAUSE I never had a book for .NET.
    I was thinking about getting one, but - now that I've started really using it it goes quite fast and I probably won't need that book afterall.

    But to give a recommendation... depending on whether or not you are comfortable with VB syntax/keywords/statements, get a more advanced book that handles 1) the .NET framework in general and 2) the principles of inheritance, and also preferably 3) events and delegates. If you're less comfortable still, I advise you get more of a beginners book that starts with simple variables, basic function definitions and If-statements, Select statements and loops, before it goes into the entire .NET framework and Object Oriented code principle.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • yauchildchewyauchildchew Member Posts: 19
    Thanks again for your spontaneous response!

    In fact, i am a Civil Engineering student, barely learned any programming laguages in the past, maybe just some very basic of VB 6.0.

    Now i am doing a Research which requires me to do a light program which contains simple calculation. At first, my supervisor advised me to use C++ which by then i did not have any knowledge of. The reason they told me is that i can claim the rights of the Program and its source codes and a program made of C++ is harder to be copied or modified by others (I believe that nothing is uncrackable in computer technology though). Some lecturers of Computer Science advised me that if my program is more to graphic and simple calculation, then better go for VB. [b]Whats your opinion on this matter?[/b]

    With some basic knowledge of VB and by referring to books and online info, i managed to do limited C++ program. However, the output is always in the black DOS screen. I am told i can make it more graphical like a window application by calling built-in library, which till now i have not figured out how to do yet. Most of the info i have found lead me on how to program mathematical calculation and algorithm, but what i need more is to add graphics and colors to my dull DOS-based output screen.

    Could you recommend me online resources links or books about Graphical User Interface (GUI) with C++? Is C++ (say, Borland C++) similar to Visual C++? Of what i have heard, only Visual C++ can produce GUI application, the rest C++ are more to DOS-based output screen, is it true?

    To make progress, i have opted to go for VB first and maybe refine the program later by using C++. I go for the latest version; the VB 2005 EE, which like the others, i have encountered lots of problems which would not be problems if it was VB 6.0. I still havent solved a simple problem; that is to COPY and PASTE a form and also problem of RENAMING a form. Everytime i try to do that, errors pop up! [b]Have you encountered such nuisance when switching from VB 6.0 to .NET? And have you solved it?[/b]

    And this, i havent tried yet but i believe it'll work and ...
    [code]While ListBox1.SelectedItems.Count > 0
    ListBox1.Items.Remove(ListBox1.SelectedItems.Item(0))
    End While[/code]
    i will say...Your logic is excellent! Haha :)

    Before i joined this Programmers'Heaven, i have another unsolved mystery. I am not sure if this is possible or not, please enlighten!

    I have ONLY ONE Button to execute a few events. Well, i have given up on doing this long ago and thats why i cant remember what those events were anymore. Basically it is like this, say, for the 1st time i click the Button, i want to execute eventA and when for the second time i click on the SAME Button, i want to execure eventB. [b]Have you any idea how to do it?[/b]

    Again, thanks!

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : Now i am doing a Research which requires me to do a light program
    : which contains simple calculation. At first, my supervisor advised
    : me to use C++ which by then i did not have any knowledge of. The
    : reason they told me is that i can claim the rights of the Program
    : and its source codes and a program made of C++ is harder to be
    : copied or modified by others (I believe that nothing is uncrackable
    : in computer technology though).

    I wouldn't know about the rights to be honest. Can't one claim rights off a VB-written program?
    I would guess that C++ is harder to decipher than VB code, because it's far less dependant (VB
  • yauchildchewyauchildchew Member Posts: 19
    [link=http://www.programmersheaven.com/mb/VB_Beginner/364226/364226/ReadMessage.aspx?S=B20000#364226]VB.NET: Copy & Paste Form Problem[/link]

    I have opened another topic for my question.

    And i must say that your description is not fuzzy at all :)
    I have heard advices and explanation from seniors and friends and you have helped to summarize everything up, giving me a better picture to the solution i need!

    Well, for the time being i can proceed my Project :)

    Thanks!
  • yauchildchewyauchildchew Member Posts: 19
    Well, i have another query for using ListBox.

    How do i add items into a ListBox permanently as in even after i close the program and restart, that the added items will be still there? And for removing permanently, is it only slight modification to the codes of Adding permanently?

    Thanks :)
  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : Well, i have another query for using ListBox.
    :
    : How do i add items into a ListBox permanently as in even after i
    : close the program and restart, that the added items will be still
    : there? And for removing permanently, is it only slight modification
    : to the codes of Adding permanently?
    :
    : Thanks :)
    :

    To remove permanently, simple don't save permanently ;)

    Goto My Project::Settings. Add a new Setting with type System.Collections.Specialized.StringCollection and fill it with default items. Give it a proper name
    Then restore the listbox values from the My.Settings.NameGoesHere property. Change the properties value to 'permanently' add/delete.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • yauchildchewyauchildchew Member Posts: 19
    sorry to reply so late, been occupied for something for almost two weeks!

    so, i wanted to make this function of "permenantly" add/delete items (once added, will be there the next time running it, once deleted, wont be listed there the next running) to the ListBox to the end-user, does the methods you mentioned work that way?

    I have just tried to do what you advised but not successful yet (due to some of my own mistakes), before that, i would like to clarify the function of your recommendation.

    Thanks.
  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : sorry to reply so late, been occupied for something for almost two
    : weeks!
    :
    : so, i wanted to make this function of "permenantly" add/delete items
    : (once added, will be there the next time running it, once deleted,
    : wont be listed there the next running) to the ListBox to the
    : end-user, does the methods you mentioned work that way?
    :
    : I have just tried to do what you advised but not successful yet (due
    : to some of my own mistakes), before that, i would like to clarify
    : the function of your recommendation.
    :
    : Thanks.
    :

    Have you already figured out how to save the listbox entries?
    Assuming you have, when saving the entries, leave out the ones that have been permanently deleted.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
«1
Sign In or Register to comment.