Pregunta

I am relatively new to C# and I could use some help with getting over this hurdle. I have a windows form along with classes to that will create call objects with values entered by the user and add them to a list. I would like to take an existing call and display its values in different text boxes in a seperate form that is opened when I click a button. I have a 'FindCall' method that finds the desired call in the list by its 'callerName' and 'phoneNumber' values. If the call is successfully found it will display the edit form. This works fine and the form opens, so I know that the code to find the call is functioning properly. However I cannot wrap my head around how I can display values of that call object in the text boxes on that edit form. Any help would be greatly appreciated.

Thanks

¿Fue útil?

Solución

A simple way to do that would be to create a property on the second form. This will allow you to set the values on the form before displaying it and get any new values afterwards.

On the edit form have something like...

string SomeValue
{
    get { return SomeValueField.Text; }
    set { SomeValueField.Text = value; }
}

...where SomeValueField is a TextBox on the edit form.

Then in the calling form you can access the TextBox via the property...

var editForm = new EditForm();

editForm.SomeValue = "...";

editForm.ShowDialog();

var newValue = editForm.SomeValue;

Otros consejos

So you have a form let's call it Form1 that opens up a new Form lets call it Form2 using something like this.

    private void Button1_Click(object sender, EventArgs e)
    {
        Form2 _newForm = new Form2();
        _newForm.Show();
    }

All you need to do is inside Form2 create a public void that passes through a string to set the value of your text box so in Form2 you have:

    public void SetTextBox(string _txt)
    {
        TextBox1.Text = _txt;
    }

Then you change your Form1 code to include:

    private void Button1_Click(object sender, EventArgs e)
    {
        Form2 _newForm = new Form2();
        _newForm.SetTextBox("Your Text Here");
        _newForm.Show();
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top