我需要一个有Windows窗体应用程序才能的人来回答我的问题 [英] I need someone talented with windows form application to answer my question

查看:68
本文介绍了我需要一个有Windows窗体应用程序才能的人来回答我的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我一直在做一个关于继承和组合的精疲力尽的项目.完成类编码后,我开始制作Windows窗体,在这里,我真的不知所措,因为我找不到如何向列表框中添加对象属性(object.name).
我已经尝试了很多次,但是无论如何,我总是在添加列表框对象时遇到问题,要么只添加第一个对象,然后重复其他元素,要么根本不显示列表框.

这是Form1.cs的代码段:

Hey to all of you.

I''ve been doing a really exhausting project about inheritence and composition; and after I finished coding classes, I started doing the windows form, and here, I really got overwhelmed, as I couldn''t find out how to add an object property (object.name) to the listbox.
I''ve tried and tried, but no matter what I do, I always get stuck having some problem with listbox object adding, either It only adds the 1st object, then other elements are duplicated, or the listbox is never shown at all.

Here''s a code snippet for Form1.cs :

namespace Map
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       public  ArrayList countries = new ArrayList();
        private void button1_Click(object sender, EventArgs e)
        {

            Country c = new Country(cntrynm.Text, comboBox1.Text, Convert.ToDouble(areatxt.Text), Convert.ToInt32(numericUpDown1.Value), nibor1.Text, nibor2.Text, nibor3.Text, Convert.ToDouble(xcontxt.Text), Convert.ToDouble(ycontxt.Text), Convert.ToDouble(zcontxt.Text));

            countries.Add(c);       
            listBox1.Items.Add(countries.Add(c.Country_Name));       //This Approach is totally wrong



            //OR  
            //this one below:


           listBox1.Items.Clear(); //this clear method is used to not have any remaining duplicates from previous clicks
   for (int i = 0; i < countries.Count; i++)
   {                                    
 
             countries.Add(c);     //we insert the object in ArrayList "countries"

             countries[i]= new Country(cntrynm.Text, comboBox1.Text, Convert.ToDouble(areatxt.Text), Convert.ToInt32(numericUpDown1.Value), nibor1.Text, nibor2.Text, nibor3.Text, Convert.ToDouble(xcontxt.Text), Convert.ToDouble(ycontxt.Text), Convert.ToDouble(zcontxt.Text));
                listBox1.Items.Add((Country)countries[i]);   //After all this thinking, it was totally not working
                 
       //OR this third way of approach:

           (Country)countries[i] = c;
           listBox1.Items.Add((Country)countries[i].);
                                                              //totally nothing
                
                }
          

          

        }



这是为那些想要帮助我的人提供的下载链接:
http://www.ziddu.com/download/19354518/Map.zip.html"&gt;http://www.ziddu.com/download/19354518/Map.zip.html[^]
(仅66 KB)

P.S:有很多类和编码,所以一切都设置好了,您只需要为我更正
Form1.cs编码,特别是列表框.

因此,如果您知道谁将对象和对象的属性与列表框一起使用,请向我们提供帮助

您可以将更正的版本发送给我到我的电子邮件:obzajd@mail.com

感谢您的收听 ^ ]



here''s the download link for those of you who are intrested in giving me a hand:
http://www.ziddu.com/download/19354518/Map.zip.html"&gt;http://www.ziddu.com/download/19354518/Map.zip.html[^]
(only 66 KB)

P.S: there are lots of classes and coding, so everything''s set well, and you only have to correct for me the
Form1.cs coding, specificly the listbox.

so, please if you have any idea who to use objects and object''s properties with listbox adding, thanks for the help forwards

you can send me the corrected version to my email: obzajd@mail.com

Thanks for listening^]

推荐答案

除非对象的ToString()方法返回正确的值,否则不应将对象传递给listBox1.Items.Add().这很容易检查您的类定义.或者,只需添加c.CountryName属性(或任何称为它的属性).有关更多信息,请参见文档 [ ^ ].
You should not be passing objects to the listBox1.Items.Add() unless their ToString() method returns the correct value. That is easy to check in your class definition. Alternatively, just add c.CountryName property (or whatever it is called). For more information take a look at the documentation[^].


尝试一下:
Try this:
private void button1_Click(object sender, EventArgs e) {
 
            Country c = new Country(cntrynm.Text, comboBox1.Text, Convert.ToDouble(areatxt.Text), Convert.ToInt32(numericUpDown1.Value), nibor1.Text, nibor2.Text, nibor3.Text, Convert.ToDouble(xcontxt.Text), Convert.ToDouble(ycontxt.Text), Convert.ToDouble(zcontxt.Text));
 
            countries.Add(c);  // insert new country object before looping so it only goes in one time
 
            listBox1.Items.Clear(); //clear your list

            for (int i = 0; i < countries.Count; i++)   {
                listBox1.Items.Add((Country)countries[i].Country_Name);                                                                    
            }

        }


列表框并不是要用作多列组件,因此最好将其替换为listview组件,无论如何,如果要将项目添加到列表框中,请尝试正在关注

//instance对象,并假设您的对象在其中具有name属性
国家/地区c =新国家/地区()
c.Name =美国"
ListBox1.Items.Add(c.Name)
listbox is not meant to be used as a multi column component, so its better for you to swap for listview component, anyway if you want to add items to your listbox try the following

//instance object and lets suppose your object have a name property there
Country c = new Country()
c.Name="USA"
ListBox1.Items.Add(c.Name)


这篇关于我需要一个有Windows窗体应用程序才能的人来回答我的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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