【问题标题】:How to pass value from one cs-file (void!) to another?如何将值从一个 cs 文件(无效!)传递到另一个?
【发布时间】:2014-03-26 07:12:08
【问题描述】:

我的小程序运行得很好,读取具有 3 个配置值的 xml 文件,读取 json-http-page 等等。 现在我想做一件小事:在我的设置对话框中显示 xml 中的值。我得到了这个(Program.cs):

using ...

//*****************************************************************************
namespace t2bl
{
abstract class TANSS2BL
{



    public static NotifyIcon notico;


    //==========================================================================


    public static void Main(string[] astrArg)
    {
        ContextMenu cm;
        MenuItem miCurr;

        cm = new ContextMenu();

        miCurr = new MenuItem();
        miCurr.Index = 0;
        miCurr.Text = "&Settings";
        miCurr.Click += new System.EventHandler(SettingsClick);
        cm.MenuItems.Add(miCurr);

        miCurr = new MenuItem();
        miCurr.Index = 1;
        miCurr.Text = "Beenden";
        miCurr.Click += new System.EventHandler(ExitClick);
        cm.MenuItems.Add(miCurr);

        notico = new NotifyIcon();
        notico.Icon = new Icon("tanss.ico");
        notico.Text = "TANSS Busylight Connector";
        notico.Visible = true;
        notico.ContextMenu = cm;
        notico.DoubleClick += new EventHandler(NotifyIconDoubleClick);




        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerAsync();





        Application.Run();




    }

    public static void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("C://bll.config.xml");

        XmlNode xmlurl = doc.SelectSingleNode("/settings/url");
        string url = xmlurl.FirstChild.Value;
        XmlNode xmluid = doc.SelectSingleNode("/settings/userID");
        string userid_string = xmluid.FirstChild.Value;
        XmlNode xmlrefresh = doc.SelectSingleNode("/settings/refresh");
        string refresh_string = xmlrefresh.FirstChild.Value;

        int userid = Convert.ToInt32(userid_string);
        int refresh = 1000 * (Convert.ToInt32(refresh_string));

        var controller = new BusylightUcController();


        while (true)
        {
            WebClient client = new WebClient();
            var downloadString = client.DownloadString(url + "/module/busylight.php?    user=" + userid);

            JObject colors = JObject.Parse(downloadString);
            //Console.WriteLine(colors["color"]); //Debug
            var colorStatus = Convert.ToInt32(colors["color"]);

            switch (colorStatus)
            {
                check the colors and set the light
            }
            Thread.Sleep(refresh);
            GC.Collect();
        }
    }



    //==========================================================================
    protected static void ExitClick(Object sender, EventArgs e)
    {
        notico.Dispose();
        Application.Exit();
    }

    //==========================================================================
    protected static void SettingsClick(Object sender, EventArgs e)
    {

        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>

        // This should open the "Settings"-Popup, containing 3 textboxes and a button to    save them to xml.
        SettingsDiag form1 = new SettingsDiag();
        form1.Show();
    }

    //==========================================================================
    protected static void NotifyIconDoubleClick(Object sender, EventArgs e)
    {
        // ...
    }



}
}

另一方面(Form1.Designer.cs):

namespace Settingsdialog
{
    partial class SettingsDiag
    {

    /// <summary>
    /// Erforderliche Designervariable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Verwendete Ressourcen bereinigen.
    /// </summary>
    /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Vom Windows Form-Designer generierter Code

    /// <summary>
    /// Erforderliche Methode für die Designerunterstützung.
    /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
    /// </summary>
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(50, 28);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(163, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "TANSS Busylight connector v1.0";
        this.label1.Click += new System.EventHandler(this.label1_Click);
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(53, 65);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(159, 20);
        this.textBox1.TabIndex = 1;
        this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(271, 117);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.label1);
        this.Name = "Form1";
        this.Text = "TANSS Busylight connector";
        this.ResumeLayout(false);
        this.PerformLayout();

    }


    #endregion

    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;

}

}

如何将值“url”、“userid_string”、“refresh_string”从 backgroundworker void 传递到“textbox1.Text=”输出?

谢谢!

【问题讨论】:

  • 不清楚你真正想做什么......

标签: c# xml json winforms


【解决方案1】:

有很多方法可以做到这一点,您在其他答案中有一种方法,我可以建议另一种方法: 因为您的工作人员设置为异步运行,所以您可以例如使用工作人员事件报告更新进度,然后在处理程序中更新文本框

bw.WorkerReportsProgress = true;
bw.ProgressChanged += (o, args) => { textBox1.Text = args.UserState.ToString();}

很明显,在您的bw.DoWorkevent 处理程序中,您需要报告将 UserState 作为您要报告和更新的字符串值的进度操作,因此在事件处理程序中您需要调用

bw.ReportProgress(0, "you value to report");

希望对你有帮助

【讨论】:

  • Mh 没有让它按预期工作......我现在停止了这个工作。首先要使用 bg_worker 报告做一些测试应用程序。谢谢!
【解决方案2】:

如果您想将后台工作人员的结果传达给您的表单,您可以将您的表单定义为 TANSS2BL 的成员(并在 Main 中对其进行初始化)。 然后在 bw_DoWork 中,您可以在表单上调用一个方法,并使用您想要显示为参数的信息。

由于 bw_DoWork 在另一个线程上,请不要忘记在设置 textBox 值之前在表单中测试 InvokeRequired 和 BeginInvoke。

【讨论】:

    猜你喜欢
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多