-1

this is the topic connection for get user input in form 2 and display data in form 1.

This is my code in form2.

public string UserText
    {
        get 
        { 
            return this.textBox1.Text; 
        }
        set 
        {
            this.UserText = value;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {


        if (textBox1.Text == "")
        {
            MessageBox.Show("Please enter keyword to search");
        }
        else 
        {
            //anta data input to form1.
            UserText = textBox1.Text;
        }

and this is my code in form1

private void Form1_Load(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.ShowDialog();

        string text = form2.UserText;
    }

i want when we click on button search, it will automatically display the data when we load the form1.

when i run, it says at the setter:

make sure you do not have an infinite loop or infinite recursion.

why it says that?what did i do wrong?

i also had tried did.

public string UserText
    {
        get 
        { 
            return this.UserText; 
        }
        set 
        {
            this.UserText = value;
        }
    }

but it appears the same.

====EDIT==== now im trying to use this:

public string UserText
{
    get 
    { 
        return this.textBox1.Text; 
    }
    set 
    {
        this.textBox1.Text = value;
    }
}

also i tried this:

public string UserText { get; set;}

it does not show the error however, it also does not load the form1. the operation just stops there. is there anything i have done it wrong?

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

2 Answers2

3

The code in your setter for the UserText property: this.UserText = value; calls itself. Based on the getter, I think you should make the setter be like this:

set
{
    this.textBox1.Text = value;
}
Dan
  • 9,717
  • 4
  • 47
  • 65
  • ok. it does not show the error but why when i click the button1, it does not appear form1?what should i add to make it load the form1 too? – sara brown Aug 07 '12 at 02:18
  • Adding this property, and making it public will only expose it so that in your button5_Click method, after you call form2.ShowDialog(), you can access form2.UserText, and it will contain the value that you entered in the textbox in form2. – Dan Aug 07 '12 at 02:31
  • It would be a really good idea to change your names from `button1`, `button5`, `form1`, and `form2` to names that accurately describe the functionality of the control. – Dan Aug 07 '12 at 02:32
  • did u mean by this: public void searchButton_Click(object sender, EventArgs e)? it still does not work. – sara brown Aug 07 '12 at 02:41
  • im accepting because it does not appear the error. only it does not load the form. i think my mistake is just minor which i may not noticing it. that is what i thought. – sara brown Aug 07 '12 at 02:46
1

if you are using C# 4

public string UserText {get;set;}

you have to put the Form2 in the Form1 Constructor

    public Form1()
    {
        this.InitializeComponent();

    Form2 form2 = new Form2();
    form2.ShowDialog();

    string text = form2.UserText;
    }

this will happend before the Form1 is shown

S3ddi9
  • 2,121
  • 2
  • 20
  • 34
  • it the same with my comment above. it does not show the error but why when i click the button1, it does not appear form1?what should i add to make it load the form1 too? – sara brown Aug 07 '12 at 02:25