在 windows 窗体文本框中显示代码结果

Posted

技术标签:

【中文标题】在 windows 窗体文本框中显示代码结果【英文标题】:display code results in windows form textbox 【发布时间】:2014-09-07 13:34:00 【问题描述】:

我正在尝试获取代码生成的值,以使用 Windows 窗体文本框显示。这是生成值的代码:

我想使用表单设计器文本框(私有 System.Windows.Forms.TextBox textBox1;)显示“console:writeline”

public static void TestGetRunSummaryResult()
    

        // create project
        string requestCreateProjectUri = OpenApiUri + string.Format(CreateProjectUri, "xml");
        HttpWebResponse responseCreateProject =
            (HttpWebResponse)
            _CallPostApi(requestCreateProjectUri, typeof(NewProjectItem), _GetProjectItem("TestGetRunSummaryResultAPI"));
        int newProjectId = DeserializeHttpWebResponse(responseCreateProject);
        projectListToBeDeleted.Add(newProjectId);

        //Create a Base Run
        string requestCreateBaseRunUri = OpenApiUri + string.Format(CreateBaseRunUri, "xml");
        HttpWebResponse responseCreateBaseRun =
            (HttpWebResponse)
            _CallPostApi(requestCreateBaseRunUri, typeof(NewRunItem), _GetNewRunItem(newProjectId));
        int newRunId = DeserializeHttpWebResponse(responseCreateBaseRun);


        string requestGetRunStatusUri = OpenApiUri + string.Format(GetRunStatus, newRunId, 0, "json");

        int percentStatus = 0;

        while (percentStatus < 100)
        
            HttpWebResponse response = (HttpWebResponse)_CallGetApi(requestGetRunStatusUri);
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            string result = reader.ReadToEnd();
            RunStatus runStatus = DataContractJsonDeserialize<RunStatus>(result);
            percentStatus = runStatus.StatusPercentDone;
            MessageBox.Show(String.Format("0% - 1", percentStatus, runStatus.DetailedStatus));
        

        string requestGetRunSummaryResultsUri = OpenApiUri +
                                             string.Format(GetRunSummaryResultsUri, newRunId, 0, "json");
        HttpWebResponse response2 = (HttpWebResponse)_CallGetApi(requestGetRunSummaryResultsUri);
        Stream responseStream2 = response2.GetResponseStream();
        StreamReader reader2 = new StreamReader(responseStream2);
        string resultSummary = reader2.ReadToEnd();
        RunResultSummary runResultSummary = DataContractJsonDeserialize<RunResultSummary>(resultSummary);

        MessageBox.Show("Building Type: " + runResultSummary.BuildingType);

        //Console.WriteLine("Summary run result for: " + runResultSummary.Runtitle);
        //Console.WriteLine("Location: " + runResultSummary.Location);
        //Console.WriteLine("Building Type: " + runResultSummary.BuildingType);
        //Console.WriteLine("Project Template: " + runResultSummary.ProjectTemplateApplied);
        //Console.WriteLine(String.Format("Floor Area: 0 1", runResultSummary.FloorArea.Value, runResultSummary.FloorArea.Units));
        //Console.WriteLine(String.Format("Electriciyt Cost: 0 1", runResultSummary.ElectricCost.Value, runResultSummary.ElectricCost.Units));
        //Console.WriteLine(String.Format("Annual Energy Cost: 0 ", runResultSummary.RunEnergyCarbonCostSummary.AnnualEnergyCost));
        //Console.WriteLine(String.Format("Lifecycle Cost: 0 ", runResultSummary.RunEnergyCarbonCostSummary.LifecycleCost));
        //Console.WriteLine(String.Format("Annual CO2 Emissions Electric: 0 1", runResultSummary.RunEnergyCarbonCostSummary.AnnualCO2EmissionsElectric.Value, runResultSummary.RunEnergyCarbonCostSummary.AnnualCO2EmissionsElectric.Units));
        //Console.WriteLine(String.Format("Annual CO2 Emissions On site Fuel: 0 1", runResultSummary.RunEnergyCarbonCostSummary.AnnualCO2EmissionsOnsiteFuel.Value, runResultSummary.RunEnergyCarbonCostSummary.AnnualCO2EmissionsOnsiteFuel.Units));
        //Console.WriteLine(String.Format("Annual CO2 Emissions Large SUV Equivalent: 0 1", runResultSummary.RunEnergyCarbonCostSummary.AnnualCO2EmissionsLargeSUVEquivalent.Value, runResultSummary.RunEnergyCarbonCostSummary.AnnualCO2EmissionsLargeSUVEquivalent.Units));


        //Assert.AreEqual(runResultSummary.Runtitle, "box model");

    

【问题讨论】:

出了什么问题,您是否收到任何错误或您没有获得预期值? “生成值的代码”TestGetRunSummaryResult() 正在返回 void。如果我理解正确,您需要从 TestGetRunSummaryResult() 方法返回您想要的任何内容,以便您的 textBox1 显示。 我可以使用消息框获得正确的值。例如。使用消息框时返回“建筑物 ID:办公室”。我希望在 windows 窗体中的文本框中显示相同的结果。 但我无法使用 this.texbox 显示,因为它是一个静态方法 【参考方案1】:

你有几个选择:

1.重组你的代码,使你的方法不是静态的。这可能不是一个糟糕的选择,因为静态通常是构建代码的一种糟糕方式。

2.创建一个静态文本框,说类似

static Textbox myTextBox;

然后在表单加载时将其设置为您的文本框,并使用它代替 Console.WriteLine()

3.传入一个TextBox作为参数

public static void TestGetRunSummaryResult(TextBox y)

    t.Text = "whatever";

4.使用回调方法

如果您使用的是多线程,那么不要忘记也使用 TextBox.Invoke()。 (-:

【讨论】:

以上是关于在 windows 窗体文本框中显示代码结果的主要内容,如果未能解决你的问题,请参考以下文章

用java如何实现 在文本框中显示图片

第一章 C#入门 (Windows窗体应用程序)

如何通过在 C# windows 窗体中的文本框中输入来更改图像的大小?

在MS访问中的子表单上显示查询结果

如何让虚拟键盘在 WinForms 应用程序的文本框中输入文本?

C# windows窗体中的文本框到数组声明