0

I am trying to have a button generate a text file and save a new combobox item based on what is entered in the combobox field or replace an existing one but it seems to be adding a new entry every time. It overwrites the text file it generates just fine.

I tried having the button delete the combobox entry that matches the name entered and then add a new one with the same name but when i do that it clears the combobox field and enters a blank item. This is the original code without the remove part.

Sub Button9Click(sender As Object, e As EventArgs)
        My.Computer.FileSystem.WriteAllText("C:\Users\" & Environment.UserName & "\desktop\Templates\" & comboBox2.text & ".txt",TextBox4.Text, False)
        ComboBox2.Items.Add(comboBox2.Text)
End Sub

For example if I Put "Test" in the combobox field and click save twice, i get two "test" items. then If I use a delete button that has:

Sub Button10Click(sender As Object, e As EventArgs)
        My.Computer.FileSystem.DeleteFile ("C:\Users\" & Environment.UserName & "\desktop\templates\" & comboBox2.text & ".txt")
        comboBox2.Items.remove(comboBox2.Text)
End Sub

it deletes only one entry. if I do it again to remove the duplicate, since the text file no longer exists, the program crashes.

How can I write this so if what is written in the combobox matches an existing entry exactly, it overwrites the existing item? It does overwrite the text document it creates, no problem as is.

  • Items is just a collection like any other. How do you usually set an item in a collection? – jmcilhinney May 08 '19 at 00:44
  • The way I have been doing it. Or via insert if I want to choose where it goes but the collection is set to sorted so that is irrelevant. I just need to know if there is something missing that will allow an entry to be replaced. I recently even tried comboBox2.Items.replace(comboBox2.Text) but thats not a thing. – Nicholas Tiberius Ross May 08 '19 at 01:21
  • I ask again, how do you usually set an item in a collection, or an array for that matter? If you can set the value of an array element or the value of an item in any other type of collection then you can do this too, because it's exactly the same. If you can't do that, you should use a search engine to find out how. – jmcilhinney May 08 '19 at 01:46
  • I have been googling this particular issue over and over again for many hours and the only information I can find is about stopping an add from happening if the list already contains that entry but that is not what i am trying to accomplish. I dont mean to be rude but if I knew how to do this I would have done it. if you are not able to offer me anything other than a vague comment rather than an actual answer I would appreciate it if you stopped commenting. If someone asks you how to do something, please refrain from saying the equivalent of, "Oh well have you tried doing it?": – Nicholas Tiberius Ross May 08 '19 at 01:50
  • [Here](https://stackoverflow.com/questions/25090837/how-do-i-replace-an-item-in-listof-string-in-vb-net) is an example on this very site that demonstrates how to set a list item. That took me seconds to find with a web search. Your problem, which is depressingly common, is that you don't know how to determine appropriate keywords to perform an effective search. As information becomes more readily available, people seem to become proportionally worse at finding it. – jmcilhinney May 08 '19 at 02:41
  • So if I wanted to enter a list of items and have one or more show up as a console command, you have it right on the money. But that example does NOT answer my question. nowhere in that article does it explain how to overwrite a list item to prevent duplicates. I have never said this before on Stack, but please do not reply. You are only making this more frustrating. – Nicholas Tiberius Ross May 08 '19 at 03:16
  • Except it does, right here: `answers(1) = correctanswer`. That code replaces an existing list item with a new value. What you are talking about is exactly the same. A list is a list and you set an item the same way regardless of what the list is for or what it contains. There's nothing special about your list that requires it to be used in a different way to any other list, which is exactly what I said before and you were determined to ignore for some reason. – jmcilhinney May 08 '19 at 04:39
  • 1
    Except for the fact that you live so far outside the box and you can't see the simple solutions right in front of you. I get that you have been doing this a long time but I honestly, no offense, think that you have forgotten what it is like to be newer to coding. Rather than trying to give convoluted indirect answers by asking questions like "how do you usually set an item in collection?" You could have just answered the question or offered an example instead of making me feel stupid. Honestly you were of no help at all. – Nicholas Tiberius Ross May 08 '19 at 05:29
  • I did offer you an example, but you denied that it was an example. I'm, not trying to make you feel stupid. If you feel stupid, that's on you. My point was that it was a very easy problem to solve but you were trying to make it more complicated that it was. All you had to do was answer the question I asked: "how do you usually set an item in a collection". The fact that you were searching for hours shows that you were over-complicating it. Sometimes we can't see the wood for the trees and that's exactly why I tried to simply it by asking that question. You ignored it. Anyway, I'm done. – jmcilhinney May 08 '19 at 06:49
  • Just had to get the last word in huh? Not shocked. It is completely in line with every other personality trait that you have displayed so far during this god-awful conversation. I'm just going to delete this post so that you can't bother me anymore. I guess it's a good thing you didn't post a real answer so that you couldn't be scored huh? Totally explains your ratio. – Nicholas Tiberius Ross May 08 '19 at 15:33

1 Answers1

0

Under the click event of the button, simply check if the item already exists before adding it to the combo box. Try this:

Sub Button9Click(sender As Object, e As EventArgs)
    My.Computer.FileSystem.WriteAllText("C:\Users\" & Environment.UserName & "\desktop\Templates\" & comboBox2.text & ".txt",TextBox4.Text, False)
    If (Not ComboBox2.Items.Contains(comboBox2.Text)) Then
        ComboBox2.Items.Add(comboBox2.Text)
    End If
End Sub
preciousbetine
  • 2,959
  • 3
  • 13
  • 29
  • I will give that a go. I was hoping there was such a check available but could not find the right code to sort that out. I will get back to you shortly – Nicholas Tiberius Ross May 08 '19 at 03:26
  • I was able to use your idea in combination with an idea I had earlier to get the Job done. When I did this earlier I lost the text in the combobox as I removed the entry to replace it so i was not able to rebuild the entry with the same add script, So now I just pass the combobox text to a label In the event that the entry is found and then re-add the combobox item using the label text. Thanks for the help. – Nicholas Tiberius Ross May 08 '19 at 03:40