简体   繁体   中英

Value Form Showdialog doesn't display in TextBox but shows in MessageBox.Show()?

i have MDI >> Form1 >> Form2, and inside form1 i use component Timer to check value from Form2.

Form1

namespace Purchasing
{
    public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
    {
        private string find_code;
        public string _code
        {
            set { find_code = value.ToUpper(); }
        }

        public XtraForm1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XtraForm2 frm = new XtraForm2();
            frm.ShowDialog();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (find_code != null)
            {
                textBox1.Text = find_code;
                find_code = null;
            }
        }
    }
}

Form2

namespace Purchasing
{
    public partial class XtraForm2 : DevExpress.XtraEditors.XtraForm
    {
        public XtraForm2()
        {
            InitializeComponent();
        }

        private void XtraForm2_Click(object sender, EventArgs e)
        {
            XtraForm1 frm = new XtraForm1();
            frm._code = "123";
             Close();
        }
    }
}

but after i run this program value 123 can't show at textbox1, if i Messagebox.Show(find_code) , the value appears. What's wrong with mycode?

Add XtraForm1 as a member of XtraForm2 as mentioned below:

public partial class XtraForm2 : DevExpress.XtraEditors.XtraForm
{
    public XtraForm1 frm1;
    public XtraForm2()
    {
        InitializeComponent();
    }

    private void XtraForm2_Click(object sender, EventArgs e)
    {
         frm1._code = "123";
         Close();
    }
}

In XtraForm1 class, set the above added member as:

private void button1_Click(object sender, EventArgs e)
{
    XtraForm2 frm = new XtraForm2();
    frm.frm1 = this;
    frm.ShowDialog();
}
public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
{        
    public XtraForm1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        XtraForm2 frm = new XtraForm2();
        frm.ShowDialog();
        textBox1.Text = frm.Code;
    }       
}

namespace Purchasing
{
    public partial class XtraForm2 : DevExpress.XtraEditors.XtraForm
    {
        public string _code = string.Empty;
        public string Code
        {
           get { 
               return _code;
           }
        }

        public XtraForm2()
        {
            InitializeComponent();
        }

        private void XtraForm2_Click(object sender, EventArgs e)
        {
            _code= "123";
            this.Close();            
        }
    }
}

in your XtraForm2_Click you create a new Form, then you cant acces to the value from your old Form.

you can test this (not tested but may work)

Form2

namespace Purchasing
{
  public partial class XtraForm2 : DevExpress.XtraEditors.XtraForm
  {
    public string Code { get; set; }
    public XtraForm2()
    {
        InitializeComponent();
    }

    private void XtraForm2_Click(object sender, EventArgs e)
    {
        Code = "123";
    }
  }
}

Form1

namespace Purchasing
{
  public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
  {
    private Form _frm;
    public XtraForm1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _frm = new XtraForm2();
        _frm.Show();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(((XtraForm2 )_frm).Code))
        {
            textBox1.Text = ((XtraForm2 )_frm).Code;
            ((XtraForm2 )_frm).Code = null;
        }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM