0

I have a massive problem with a project im working on. I am trying to make a WFA which will take input from user and then the user will choose to either add what they typed using a hashtable or delete something out of that hashtable by using the add and remove buttons...

Im really struggling on how to add the user input to the hashtable?? someone please help!!!

namespace Lab6_Library2 {
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void buttonAdd_Click(object sender, EventArgs e)
    {
        Hashtable books = new Hashtable();
        books = textBoxInput.Text;


    }

    private void textbox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void buttonView_Click(object sender, EventArgs e)
    {
        MessageBox.Show("All The Books Added are: \n" + textBoxInput);//+   );
    }
svick
  • 236,525
  • 50
  • 385
  • 514
user3008643
  • 89
  • 2
  • 8
  • 2
    The namespace name, is this from a book chapter or school homework? – Trido Nov 25 '13 at 00:50
  • Store the hashtable object as a member of the form. Provide the user with a button next to the textbox, when it is clicked, clear the textbox and insert its contents into the hashtable. – Asad Saeeduddin Nov 25 '13 at 00:50
  • 1
    Pretty please, don't use `Hashtable`. Instead, use the generic `Dictionary`. – svick Nov 25 '13 at 00:51

1 Answers1

2
  1. If you'd like to keep the collection content, you have to move it to class level:

    public partial class Form1 : Form
    {
        HashSet<string> books = new HashSet<string>();
    
        // (...)
    }
    

    I used generic HashSet<string> here, because it's the right one to use in your case.

  2. To add item to HashSet instance, use Add method:

    private void buttonAdd_Click(object sender, EventArgs e)
    {
        books.Add(textBoxInput.Text);
    }
    
  3. To remove items, use Remove method:

    books.Remove(textBoxInput.Text);
    
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Do you mean [Dictionary](http://stackoverflow.com/questions/772831/what-is-the-generic-version-of-a-hashtable)? There is no generic `Hashtable` (there is [HashSet](http://msdn.microsoft.com/en-us/library/bb359438(v=vs.110).aspx) or [Dictionary](http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx))... – Alexei Levenkov Nov 25 '13 at 00:59
  • I meant `HashSet` (there is no Key-Value relationship to use `Dictionary`. The OP confused me. Thank's for spotting! – MarcinJuraszek Nov 25 '13 at 01:01