首页 文章

将值从另一个类传递到表单中的文本框?

提问于
浏览
0

我有一个 class 和一个表格 . 该类旨在在引发事件时执行某些过程,并将值返回到表单以仅显示 . 我有问题将值传递回形式 . 例如,我在类 print 中有这个代码:

public class PrintClass : Form1
{

    public void printEventHandler(object sender, EventArgs e)
    {

        string text = "Process Completed";
        append_Tbox(text);

    }

}

和form1中的方法显示文本:

public void append_Tbox(string s)
    {

        TboxPrint.AppendText(s);
    }

但是,没有显示任何内容 . 我相信有些不对劲,但我无法弄清楚 . 将值从类传递到表单的最快方法是什么?

3 回答

  • 1

    首先,您的处理类不应扩展 Form1 . 这会给你一种幻觉,你可以访问现有表单的方法,但是当你这样做时,它会创建一个全新的表单,而不是显示它 . 该表单使's own set of all instance fields, so you'无法访问主表单的控件 . 即使这样可行(并且它赢得了't) it'不是一个设计良好的解决方案 .

    这样做的正确方法实际上要容易得多 . 你只需要让你的另一个类从它的方法中返回一个值:

    public class PrintClass
    {
        public string DoWork()
        {
            Thread.Sleep(2000);//placeholder for real work.
            return "Process Completed";
        }
    }
    

    现在,您的主窗体可以调用该方法并将返回值附加到文本框 .

    执行此操作后,您将在工作发生时阻止该UI线程,从而防止重新绘制表单或处理任何其他事件 . 您需要在后台线程中完成工作,然后编组回UI线程以使用结果更新UI . 有很多方法可以做到这一点,但如果你使用 await C#5.0是最简单的方法:

    public class Form1 : Form
    {
        private void SomeEventHandler(object sender, EventArgs args)
        {
            string result = await Task.Run(()=>new PrintClass().DoWork());
            TboxPrint.AppendText(result);
        }
    }
    

    如果你需要一个C#4.0解决方案,你可以使用 ContinueWith ,这或多或少是上面要翻译的内容,但它不是那么干净的语法 .

    public class Form1 : Form
    {
        private void SomeEventHandler(object sender, EventArgs args)
        {
            Task.Factory.StartNew(()=>new PrintClass().DoWork())
                .ContinueWith(t => TboxPrint.AppendText(t.Result)
                    , CancellationToken.None
                    , TaskContinuationOptions.None
                    , TaskScheduler.FromCurrentSynchronizationContext());
        }
    }
    
  • 0

    我在主表单中创建了委托

    public delegate void LoginDelegate(string s);
    
     public partial class AirLineReservationMDI : Form
        {
            LoginDelegate loginAirLineDelegate;
    
        }
    
    loginAirLineDelegate = new LoginDelegate(DisableToolStripMenuItems);
    
    
     public void DisableToolStripMenuItems(string s)
            {
               this.viewToolStripMenuItem.Visible = true;
                this.bookingToolStripMenuItem.Visible = true;
                this.existingUserToolStripMenuItem.Visible = false;
                this.newUserToolStripMenuItem.Visible = false;
                this.toolStripStatusUserID.Text = "USerID :- "+s;             
               this.LoginUserId = s;
            }
    

    在另一个类中,(我已经将delagete对象传递给了这个类)我解雇了Delegate

    logDelegate(textBoxUserName.Text);
    
  • 0

    我用 Action<T> 委托来解决问题 . 这是代码,它工作正常 .

    class PrintClass 
    {
        public Action<string> DisplayDelegate;
    
    
        public void printEventHandler(object sender, EventArgs e)
        {
            string text = "Event Handled, and text value is passed";
    
            var copy = DisplayDelegate;
    
            if (copy != null)
            {
               copy(text);
            }
        }
    
    }
    

    在`Form1.cs'中:

    private void Form1_Load(object sender, EventArgs e)
        {
            PrintClass p = new PrintClass();
            BtnPrint.Click += p.printEventHandler;
    
            //subscrite the displayevent method to action delegate
            p.DisplayDelegate += DisplayEvent;
    
        }
    
    
        public void DisplayEvent(string s)
        {
    
            Invoke(new Action(() => TboxPrint.AppendText(s)));
        }
    

    因此文本框中显示文本“事件处理,文本值已传递” .

    我不确定这是否是有效的方式 . 多谢你们 .

相关问题