Author Topic: ADD OR DELETE AN ITEM IN THE COMBO BOX WHILE RUNNING  (Read 2075 times)

ajithrengaraj

  • Newbie
  • *
  • Posts: 6
    • View Profile
ADD OR DELETE AN ITEM IN THE COMBO BOX WHILE RUNNING
« on: January 28, 2016, 09:04:31 AM »
HI

I am new to VB , Basically I am controls guy works with Plc

just started to learn VB to develop an own project

Archie helped me to use the combo box to write the selected item in my PLC

but i would like to make more easier to customer like , giving an option to edit the items in the combo box

Add or delete the items in the combo box

i could see different code in Online to do that since i am not a technical person, its hard to understand

it would be great if anyone explain me in clearly

thanks to all

dmroeder

  • Global Moderator
  • Full Member
  • *****
  • Posts: 206
    • View Profile
Re: ADD OR DELETE AN ITEM IN THE COMBO BOX WHILE RUNNING
« Reply #1 on: January 28, 2016, 10:00:11 AM »
Hello ajithrengaraj,

Try this:
  -Add a combo box, text box and button to your form
  -Double click the button, which should bring you to the code for the button press
  -Add the following to the button press code: 
Code: [Select]
ComboBox1.Items.Add(TextBox1.Text)
The items in a combo box can be referenced by an index (1,2,3,etc) or by item (text that you see).  To delete one, you can do this:
 
Code: [Select]
' Remove the third item in the list
ComboBox1.Items.RemoveAt(2)

Or if you can remove an item by the specific text that is displayed:
Code: [Select]
ComboBox1.Items.Remove("This is a test")
Or if you want to remove the currently selected item:
Code: [Select]
ComboBox1.Items.RemoveAt(ComboBox1.SelectedIndex)
« Last Edit: January 28, 2016, 10:08:00 AM by dmroeder »

ajithrengaraj

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: ADD OR DELETE AN ITEM IN THE COMBO BOX WHILE RUNNING
« Reply #2 on: January 28, 2016, 10:41:09 AM »
hi dmroeder

thats amazing , its really pulls me in Vb

looks cool to see how it working

thanks bud

and i would like to do more on that like when i click a button it pops me a new window where i insert a text box to add the item in the combo box list

how to connect the combo box in window1 and text box in window 2

thanks dmroeder

ajithrengaraj

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: ADD OR DELETE AN ITEM IN THE COMBO BOX WHILE RUNNING
« Reply #3 on: January 28, 2016, 10:47:53 AM »
I found a problem when i use the above codes

when i close and open the application i loose all my added item and it showing again the default items when i created the combo box

is there any way i can hold the added item in combo box ? even after closing the application

rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: ADD OR DELETE AN ITEM IN THE COMBO BOX WHILE RUNNING
« Reply #4 on: January 28, 2016, 10:57:33 AM »
ajithrengaraj,

You would have to save them somewhere when you change them and then read them back when you re-start the program.
a Simple text file would suffice, or if you are using a database it would work also.

Let me see if I have some example code around, or can make some up.
You can google FilestreamReader and filestreamwriter to get some examples.

ajithrengaraj

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: ADD OR DELETE AN ITEM IN THE COMBO BOX WHILE RUNNING
« Reply #5 on: January 28, 2016, 03:52:58 PM »
Thanks

I found some example code to add and delete line in combo box through text file

but when i add a line , I want to check if the line is already existing in the file .

if it is existing i don't want to add it , it should give me a warning popup message .

i appreciate everyone's help

thanks again

rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: ADD OR DELETE AN ITEM IN THE COMBO BOX WHILE RUNNING
« Reply #6 on: January 28, 2016, 05:08:52 PM »
Just put all of the defaults in the text file and on startup clear the combo box.

It'll be something like combobox1.items.clear

dmroeder

  • Global Moderator
  • Full Member
  • *****
  • Posts: 206
    • View Profile
Re: ADD OR DELETE AN ITEM IN THE COMBO BOX WHILE RUNNING
« Reply #7 on: January 28, 2016, 05:29:57 PM »
If you use Combox1.FindString("Item you are looking for"), it will return the index of the item you put in the quotes.  If it does not find the item, it returns a -1.

So you could do this:

Code: [Select]
If ComoBox1.FindString("String from text file") = -1 then
  ComboBox1.Items.Add("String from text file")
else
  'already exists
  MessageBox.Show("That one already exists", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
end if
« Last Edit: January 28, 2016, 05:34:17 PM by dmroeder »

ajithrengaraj

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: ADD OR DELETE AN ITEM IN THE COMBO BOX WHILE RUNNING
« Reply #8 on: January 29, 2016, 08:45:11 AM »
hey thanks to both of you

it worked !! now i can understand the VB code little bit ( better than nothing :P )

great work guys :) :)