如何访问WinForm应用程序中的对象数组以显示在文本框中? [英] How to access array of objects in a WinForm app for display in a textbox?

查看:147
本文介绍了如何访问WinForm应用程序中的对象数组以显示在文本框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先感谢你花时间阅读,我知道我是N00B,但代码中丢失了N00B。这是一个学校作业,我可以在控制台应用程序中,因为它的工作原理,但我想在WinForm应用程序中看起来很漂亮。



核心问题我需要使用LINQ来查询和显示基于过滤器的对象集合,我可以在控制台应用程序中执行此操作。



当我试图让它在Winform中看起来很漂亮时,这个数组无法通过按钮点击事件访问。很确定我只是以错误的方式创建对象。我吮吸WinForm应用程序。



::我的命名空间全部匹配。

::此代码将在consol中正确运行LINQ查询,所以我知道var sortDescription ..可能很好。



首先有这个代码创建我的对象数组:



 {
public partial class DisplayForm:Form
{
public DisplayForm()
{
InitializeComponent();
QueryClass QClasses = new QueryClass();
Invoice [] invoiceItems = new 发​​票[ 8 ];
invoiceItems [ 0 ] = new 发​​票( 83 电动砂光机 7 57 .98M);
invoiceItems [ 1 ] = new 发​​票( 24 Power Saw 18 99 .99M);
invoiceItems [ 2 ] = new 发​​票( 7 大锤 11 21 .50M);
invoiceItems [ 3 ] = new 发​​票( 77 Hammer 76 11 .99M);
invoiceItems [ 4 ] = new 发​​票( 39 割草机 3 79 .50M);
invoiceItems [ 5 ] = new 发​​票( 68 螺丝刀 106 6 .99M);
invoiceItems [ 6 ] = new 发​​票( 56 曲线锯 21 11 .00M);
invoiceItems [ 7 ] = new 发​​票( 3 扳手 34 7 .50M);

}

// 这是我的按钮点击代码
private void queryA_Click( object sender,EventArgs e)
{
var sortDescription =
from 元素 invoiceItems
orderby element.PartDescription
选择元素;

foreach var 元素 in invoiceItems)
{
displayHere.AppendText(element.ToString()+ Environment.NewLine);
}

}





....我在Publc DisplayFrom中试过这个()阻止并创建我自己的块netiher作品。





现在错误和我的问题。

::在某些测试代码中,我使用它的工作方式,AppendText ......



::调用的ojbect invoiceItems位于initialzeComponent()正下方的标准Winform public DisplayForm()中。按钮对象无法看到对象''invoiceItems''。我猜这是因为两个对象的范围是我的问题。



::我试过在公共空类中创建我的对象数组但是也没有用(告诉你我是N00B)



如何获取Button Action中的对象以查看项目的instatiated数组对象?



最后,感谢任何帮助,我想提前感谢你。是的,我已经谷歌了几个小时并且放弃了所以我在外面除草。



Scott

解决方案

InvoiceItems数组变量仅在 DisplayForm()过程中可见。

请阅读:范围(C#) [ ^ ]


 QueryClass QClasses = new QueryClass(); 
Invoice [] invoiceItems = new Invoice [8];





在DisplayForm()之外写这个;

范围有问题变量

 invoiceItem 



将其声明为类级变量。

currentlu这是<$ p的局部变量$ p> DisplayForm()


我只想感谢Mr.Priyank(聪明的化名)和Maciej Los的帮助。当然问题解决了我希望我得到一个关于项目的A.



Scott


Thank you first for taking the time to read, I know I''m N00B but a N00B lost in code. This is a school assignment and I can turn in the console app, because it works, but I would like to make this look pretty in a WinForm app.

Core problem I need to use LINQ to query and display a collection of objects based on the filter, this I can do in a Console Appliction.

When I tried to make it look pretty in a Winform the array is not accessable from the buton click event. Pretty sure I am just creating the objects in the wrong way. I suck with WinForm apps.

:: My namespaces all match.
:: This code will correctly run the LINQ query in a consol so I know that the var sortDescription.. is probably good.

First there is this code which creates my array of objects:

{
  public partial class DisplayForm : Form
    {
        public DisplayForm()
        {
            InitializeComponent();
            QueryClass QClasses = new QueryClass();
            Invoice[] invoiceItems = new Invoice[8];
            invoiceItems[0] = new Invoice(83, "Electric sander", 7, 57.98M);
            invoiceItems[1] = new Invoice(24, "Power Saw", 18, 99.99M);
            invoiceItems[2] = new Invoice(7, "Sledge Hammer", 11, 21.50M);
            invoiceItems[3] = new Invoice(77, "Hammer", 76, 11.99M);
            invoiceItems[4] = new Invoice(39, "Lawn Mower", 3, 79.50M);
            invoiceItems[5] = new Invoice(68, "Screwdriver", 106, 6.99M);
            invoiceItems[6] = new Invoice(56, "Jig saw", 21, 11.00M);
            invoiceItems[7] = new Invoice(3, "Wrench", 34, 7.50M);

        }

//This is my button click code
private void queryA_Click(object sender, EventArgs e)
{
   var sortDescription =
    from element in invoiceItems
    orderby element.PartDescription
    select element;

   foreach (var element in invoiceItems)
    {
      displayHere.AppendText(element.ToString() + Environment.NewLine);
    }

}



.... I have tried this in both the Publc DisplayFrom() block and created my own block netiher works.


Now the error and my problem.
:: When in some test codes I utilize the method of display it works, the AppendText...

:: The ojbect called invoiceItems is in the standard Winform public DisplayForm() right below the initialzeComponent(). And the button object can''t see the object ''invoiceItems''. I am guessing it''s because the scope of the two objects is my problem.

:: I have tried creating my array of objects in a public void class but that didn''t work either (Told you I was a N00B)

How do I get the objects in the Button Action to see the instatiated array object of items?

Lastly, any help is appreciated and I want to thank you in advance. Yes I have been googleing for hours and gave up so I am outside weeding.

Scott

解决方案

InvoiceItems array variable is visible only inside DisplayForm() procedure.
Please, read about: Scopes (C#)[^]


QueryClass QClasses = new QueryClass();
           Invoice[] invoiceItems = new Invoice[8];



write this outside the DisplayForm();
There is a problem with scope of the variable

invoiceItem

.
Declare it as a class level variable.
currentlu this is local variable to

DisplayForm()


I just want to thank both Mr.Priyank (clever pseudonym) and Maciej Los for the help. Of course problem resolved I hope I get an A on the project.

Scott


这篇关于如何访问WinForm应用程序中的对象数组以显示在文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆