繁体   English   中英

Add()方法中的if语句?

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

我正在尝试将文本文件的每一行添加到listView不同列中。 但是,我遇到了一个问题。

这是我做方法的方法:

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;
                }
            }
        }

如您所见,index只是一个方便的标志(变量),用于按顺序插入行,而不会在同一控件中重叠多行。

的问题是我想检查index是否是Add()方法的一个值,以便可以将新的textFile行添加到列表中的同一行但不同列。

更新:

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 : "";

不是在Add()-Method中而是在ItemProperties的对象初始化器中进行此操作。 那里是不可能的!

最好的解决方案是编写一个额外的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"});
}

接着

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

首先,您应该考虑使用switch ,因为在这种情况下,它看起来是理想的用法。 多个if并不能真正满足您的需求。

其次,您可以在分配值时使用简单的三元运算符,如下所示:

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

忘记循环

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