0

enter image description here enter image description here

in the left picture, there is search button. when click, it will popup the second form (right picture).

when entering the keyword on search form (form2), the data will appear at the form1. how to pass the word enter by user in form2 to form1?

this is the code in form1.

private void button5_Click(object sender, EventArgs e)
    {            
        Form2 form2 = new Form2();
        form2.ShowDialog();   //open form2-search form  

        //kene get data input dr form2
        XmlDocument xml = new XmlDocument();            
        xml.Load("C:\\Users\\HDAdmin\\Documents\\SliceEngine\\SliceEngine\\bin\\Debug\\saya.xml");
        XmlNodeList xnList = xml.SelectNodes("/Patient/Patient/Name");
        foreach (XmlNode xn in xnList)
        {
            string name = xn.InnerText;
            listBox21.Items.Add(name);                
        }
    }

this is the code in form2.

private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            MessageBox.Show("Please enter keyword to search");
        }
        else 
        {
            //send data input to form1.
        }

can anyone help me with this? thank you

===EDIT===

i am referring to this link to solve this problem. There are two ways and i am using the second method and it works perfectly. I am crying out loud for this. thank you to the blogger owner.

i also found that, in order to view the data, i need to view it in TextBox and not ListBox. what i did before is im trying to view this in ListBox. i am not sure why but that is it. anyway, this problem SOLVE! thanks again for those who help me with this topic. i am grateful.

sara brown
  • 1,057
  • 8
  • 29
  • 46

4 Answers4

2

You can, for example, simply use a property:

Form2:

public string UserText { get; set;}

...

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text == "")
    {
        MessageBox.Show("Please enter keyword to search");
    }
    else 
    {
        UserText = textBox1.Text; // set the Text
    }

Form1:

private void button5_Click(object sender, EventArgs e)
{            
    Form2 form2 = new Form2();
    form2.ShowDialog();   //open form2-search form  

    string text = from2.UserText; get the Text

    ....
sloth
  • 99,095
  • 21
  • 171
  • 219
2

Create a property (or properties) on Form2 exposing the values of the controls you want. So if you want the search term do it like:

public string SearchTerm
{
   get { return this.textBox1.Text; }
}

Also, on a side-note; don't forget to check if the user actually did press search.

The way you have it now, when a user closes the form with the x it will also search. That doesn't seem logical to the user.

Make the button on your Form2 ModalResult.OK and do it like this:

if (form2.ShowDialog() == ModalResult.OK)
{
  // Do your thing
}
Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
1

You can sign for Form2 button clicked event in Form1 class:

// Form1's button5 clicked event handler.
private void OnButton5Clicked(object sender, EventArgs e)
{
    form2.button1.click += this.OnSearchButtonClicked;
}

// form2.button1 clicked event handler.
// this method will rise when form2.button1 clicked.
private void OnSearchButtonClicked(object sender, EventArgs e)
{
    if (form2.textBox1.Text == "")
        {
            MessageBox.Show("Please enter keyword to search");
        }
        else 
        {
            // unsign from event!!!
            form2.button1.click -= this.OnSearchButtonClicked;

            // here you can use form2.textBox1.text
            string searchRequest = form2.textBox1.Text;
        }

    // your business-logic...
}

However, answers proposed by @BigYellowCactus and @Gerald Versluis are simpler and more preferable.

By the way, do not use default button names. It'll be hard to understand their purposes in future. You can rename form1.button5 in form1.showFindWindowButton and form2.button1 in form2.startSearchButton.

Illia Ratkevych
  • 3,507
  • 4
  • 29
  • 35
0

I used a simple solution in my project and few days ago. I recommend using inner-class form. create a normal form to get the seach string (just like you did), for example fSearch, then use ShowModal to display it instead of Show(). here is an example (psuedo c#):

class MainClass : form
{
  String search = String.Empty;
  private void button5_Click(object sender, EventArgs e)
  {
     SearchString s = new SearchString();
     s.ShowModal();

     search = s.search;
  }
.
.

  class SearchString : Form
  {

     public String strString = String.Empty;

     private void btnOK_Click(object sender, EventArgs e)
     {
      this.strString = text1.text;
      this.close();
      }
  }
}
Hamed Salameh
  • 223
  • 1
  • 5
  • 13