How to delete a specific text in a textbox in Windows Forms Application?

May 7, 2017 at 12:14pm
I need help in creating a shopping list in Windows Forms Application C++ in Visual Studio 2015. I have learned the basics about C++, but they dont seem to help me here when I am trying to create an windows app.

This i how a part of the MyForm.h-file look for my Windows Forms application:

private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {

textBox1->Text = " ";
textBox2->Text-> Remove(textBox2->Text);
}


It doesnt seem to work, meaning the funktion abow doesnt work. I want to specificaly delete a text in my shoppinglist(texBox2). So how do you do that???
Last edited on May 7, 2017 at 12:17pm
May 7, 2017 at 12:28pm
What do you want to delete? If you have "apple" in your textBox1 and want to delete it in textBox2 you could do it like that:
1
2
3
4
  if (textBox1->Text != "")
  {
    textBox2->Text = textBox2->Text->Replace(textBox1->Text + "\r\n", "");
  }


BTW It would be much better to use a ListBox instead of a TextBox to store the items
http://www.functionx.com/vccli/controls/listbox.htm
May 7, 2017 at 12:49pm
Ok if I where you use a ListBox then how do I then input different groseries into the listbox and then how do I specificaly delete chosen groseries from the listbox?
What are the codes for them?:

If this is how my codes look in MyForm.h : Button3 is to remove text from listbox1, And button2 is to add text into listbox.

private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {

textBox1->Text = " ";
textBox1->Text-> Remove(textBox2->Text);
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {

Application::Exit();

}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {

String ^ in = textBox1->Text + "\r\n";
listBox1->Text += in;
textBox1->Text = "";
textBox1->Focus();

}

I dont understand!
Last edited on May 7, 2017 at 12:55pm
May 7, 2017 at 4:07pm
Adding an item to the listbox:
1
2
3
4
    // add item to listbox
    listBox1->Items->Add(textBox1->Text);  
    textBox1->Clear();
    textBox1->Focus();


Removing item form listbox:
1
2
3
4
5
6
7
// find index of text
    int index = listBox1->Items->IndexOf(textBox1->Text); 
    
    if (index != -1) // found
      listBox1->Items->RemoveAt(index);
    else
      MessageBox::Show("Item not found");
Topic archived. No new replies allowed.