When i click the button i want to assign the textbox value from another class.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Download myDownload = new Download();
        static string textBoxString;
        public Form1()
        {
            InitializeComponent();
        }

        public static string TextboxText
        {
            set { textBoxString = value; }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            myDownload.Do();
            textBox1.Text = textBoxString;
            textBox1.Invalidate();
        }
    }

    public class Download
    {
        public void Do()
        {
            Form1.TextboxText = "My text";
        }
    }
}

An even better solution would be to define the possible interactions in an interface, and let that interface be the contract between your form and the other class. That way the class is completely decoupled from the form, and can use anyting implementing the interface (which opens the door for far easier testing):

interface IYourForm
{
    string FirstName { get; set; }
}

In your form class:


class YourFormClass : Form, IYourForm
{
    // lots of other code here

    public string FirstName
    {
        get { return firstNameTextBox.Text; }
        set { firstNameTextBox.Text = value; }
    }
}
...and the class:


class SomeClass
{
    private readonly IYourForm form;
    public SomeClass(IYourForm form)
    {
        this.form = form;
    }

    // and so on

}

0 comments

Related Posts Plugin for WordPress, Blogger...