简体   繁体   English

Add()方法中的if语句?

[英]If-statement inside Add() method?

I'm trying to add each line of a textfile to different columns in a listView . 我正在尝试将文本文件的每一行添加到listView不同列中。 However, I've run into a problem. 但是,我遇到了一个问题。

This is how I did a method: 这是我做方法的方法:

public void OpenFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            string line = "";
            int index = 0;
            if (openFileDialog.ShowDialog() == true)
            using (StreamReader sr = File.OpenText(openFileDialog.FileName))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    index++;
                    if (index == 1)
                        InvoiceNumbertxt.Text = line;
                    else if (index == 2)
                        InvoiceDatetxt.Text = line;
                    else if (index == 3)
                        DueDatetxt.Text = line;
                    else if (index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9)
                        PersonInfolst.Items.Add(line);
                    else if (index == 10)
                    {
                        Items.Add(new ItemProperties 
                        {     
                            Item = line
                            if(index == 11)// <---- If-statement inside Add?
                            Description = line;


                        });
                        itemlst.ItemsSource = Items;
                    }
                    else
                        break;
                }
            }
        }

As you can see index is just a convenient flag (variable) to insert the lines in order, and not overlap multiple lines into the same control. 如您所见,index只是一个方便的标志(变量),用于按顺序插入行,而不会在同一控件中重叠多行。

The problem I have is I want to check if index is a value inside the Add() method so that I can add the new textFile line to the same row but different column in the list. 的问题是我想检查index是否是Add()方法的一个值,以便可以将新的textFile行添加到列表中的同一行但不同列。

UPDATE: 更新:

public partial class MainWindow : Window
    {
        ObservableCollection<ItemProperties> Items =
        new ObservableCollection<ItemProperties>();
        public MainWindow()
        {
            InitializeComponent();
        }
        public ObservableCollection<ItemProperties> GameCollection
        {
            get
            {
                if (Items == null)
                {
                    Items = new ObservableCollection<ItemProperties>();
                }
                return Items;
            }
        } 

        private void btnOpenFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFile();
        }

        public void OpenFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            string line = "";
            int index = 0;
            if (openFileDialog.ShowDialog() == true)
            using (StreamReader sr = File.OpenText(openFileDialog.FileName))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    index++;
                    if (index == 1)
                        InvoiceNumbertxt.Text = line;
                    else if (index == 2)
                        InvoiceDatetxt.Text = line;
                    else if (index == 3)
                        DueDatetxt.Text = line;
                    else if (index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9)
                        PersonInfolst.Items.Add(line);
                    else if (index == 10)
                    {
                        Items.Add(new ItemProperties { Item = line });
                        itemlst.ItemsSource = Items;
                    }
                    else if (index == 11)
                    {
                        //??

                    }
                    else
                        break;
                }
            }
        }

        private void btnOpenImage_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDialog();
            openfile.DefaultExt = "*.jpg";
            openfile.Filter = "Image Files|*.jpg";
            Nullable<bool> result = openfile.ShowDialog();
            if (result == true)
            {
                imagefile.Source = new BitmapImage(new Uri(openfile.FileName));
            }
        }

        public class ItemProperties
        {
            public string Item { get; set; }
            public string Description { get; set; }
            public string Quantity { get; set; }
            public string UnitPrice { get; set; }
            public string Tax { get; set; }
        }
    }

您可以使用三元运算符执行内联条件检查并设置值

Description = (index == 11) ? line : "";

You are doing it not in the Add()-Method but inside of the object initializer of ItemProperties . 不是在Add()-Method中而是在ItemProperties的对象初始化器中进行此操作。 It is not possible there! 那里是不可能的!

The best solution is to write an extra Add method and create the new ItemProperties object there. 最好的解决方案是编写一个额外的Add方法并在那里创建新的ItemProperties对象。

private MyAdd(string line, int index)
{
   if(index == 11)
      Items.Add(new ItemProperties {Item = line, Description = "line11"});
   else
      Items.Add(new ItemProperties {Item = line, Description = "other"});
}

and then 接着

else if (index == 10)
{
    MyAdd(line, index);
    itemlst.ItemsSource = Items;
}

Firstly, you should consider using switch , because it looks an ideal use in this case. 首先,您应该考虑使用switch ,因为在这种情况下,它看起来是理想的用法。 Multiple ifs aren't really great for your needs. 多个if并不能真正满足您的需求。

Secondly, you can use simple ternary operator on assigning the value, some like this: 其次,您可以在分配值时使用简单的三元运算符,如下所示:

Description = id == 11 ? line : null;
else if (index == 10)
{
    Items.Add(new ItemProperties{Item = line});
     itemlst.ItemsSource = Items;
}
else if (index == 11)
{
     Description = line;
}

forget the loop 忘记循环

InvoiceNumbertxt.Text = sr.ReadLine();
InvoiceDatetxt.Text = sr.ReadLine();
...
Items.Add(new ItemProperties 
                        {     
                            Item = sr.ReadLine();
                            Description = sr.ReadLine();
                        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM