【问题标题】:how to efficiently simplify repetitive code {Objects/Strings}如何有效地简化重复代码 {Objects/Strings}
【发布时间】:2020-01-18 07:32:09
【问题描述】:

首先,我为这篇文章的篇幅道歉,但这是我开始时所知道的一切。现在,我正在尝试使用 foreachList<t>TreeView 类以避免 SO 社区推荐的重复。

表单将通过文本框收集信息,允许通过文件对话框添加文件附件,并将所有信息整理成简洁的 HTML 正文电子邮件。我们出售平板......我的原始代码看起来有点像这样:

        private void PrepareReturnEmailTwoSlabs()
        {
            LoadSettings();
            string Fname = Properties.Settings.Default.FabricatorName;
            string Facc = Properties.Settings.Default.FabricatorAccountNo;
            string Fadd1 = Properties.Settings.Default.FabricatorAddress1;
            string Fadd2 = Properties.Settings.Default.FabricatorAddress2;
            string Ftown = Properties.Settings.Default.FabricatorTown;
            string Fcounty = Properties.Settings.Default.FabricatorCounty;
            string Fpostcode = Properties.Settings.Default.FabricatorPostcode;
            string Fphoneno = Properties.Settings.Default.FabricatorPhone;
            string Femail = Properties.Settings.Default.FabricatorEmail;
            string Fclient = Properties.Settings.Default.ClientManagerEmail;
            string Fcentre = Properties.Settings.Default.CentreEmail;
            string FQt = Properties.Settings.Default.QTEmail;
            string Dateofinv = dateTimePicker1.Value.ToShortDateString();
            string Inv = textBox13.Text;
            string Material1 = textBox14.Text;
            string Thick1 = comboBox8.SelectedValue.ToString();
            string Batch1 = textBox44.Text;
            string Reason1 = comboBox1.SelectedValue.ToString();
            string Notes = textBox18.Text;
            string Thick2 = comboBox7.SelectedValue.ToString();
            string Material2 = textBox15.Text;
            string Batch2 = textBox45.Text;
            string Reason2 = comboBox2.SelectedValue.ToString();

            if (Thick2 == null)
            {
                Thick2 = "not selected";
            }
            if (Material2 == null)
            {
                Material2 = "not selected ";
            }
            if (Batch2 == null)
            {
                Batch2 = "not selected ";
            }
            if (Reason2 == null)
            {
                Reason2 = "not selected ";
            }

            GenerateUniqueRefReturn();



            //construct email
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("************", "***************"));
            message.To.Add(new MailboxAddress("**********", "********"));
            message.Subject = "Return" + "  " + Returnid;

            //different message bodies dependant on how many slabs are chosen
            TextPart body2 = new TextPart("html")
            {
                Text = @"Please See Below Information" + "<br/>" +
                      "<h4>Return ID: " + "  " + Returnid + "</h4>" + "<br/>" +
                      "<b>Fabricator Name:</b>" + "  " + Fname + "<br/>" + Environment.NewLine +
                      "<b>Account Number:</b>" + "  " + Facc + "<br/>" + Environment.NewLine +
                      "<b>Address Line 1:</b>" + "  " + Fadd1 + "<br/>" + Environment.NewLine +
                      "<b>Address Line 2:</b>" + "  " + Fadd2 + "<br/>" + Environment.NewLine +
                      "<b>Town:</b>" + "  " + Ftown + "<br/> " + Environment.NewLine +
                      "<b>County:</b>" + "  " + Fcounty + "<br/>" + Environment.NewLine +
                      "<b>Postcode:</b>" + "  " + Fpostcode + "<br/>" + Environment.NewLine +
                      "<b>Phone:</b>" + "  " + Fphoneno + "<br/>" + Environment.NewLine +
                      "<b>Email:</b>" + "  " + Femail + "<br/>" + Environment.NewLine + "<br/>" +
                      "<br/>" +
                      "<b>Date Of Invoice: </b>" + "  " + DoI + "<br/>" +
                      "<b>Invoice: </b>" + "  " + Inv + "<br/>" +
                      "<b>Material Information:</b>" + "<br/>" +
                      //slab 1
                      "<b>Thickness: </b>" + "  " + Thick1 + "mm" + "<br/>" +
                      "<b>Material Name: </b>" + "  " + Material1 + "<br/>" +
                      "<b>Batch No: </b>" + "  " + Batch1 + "<br/>" +
                      "<b>Reason for Return: </b>" + "  " + Reason1 + "<br/>" + "<br/>" +
                      //slab 2
                      "<b>Thickness: </b>" + "  " + Thick2 + "mm" + "<br/>" +
                      "<b>Material Name: </b>" + "  " + Material2 + "<br/>" +
                      "<b>Batch No: </b>" + "  " + Batch2 + "<br/>" +
                      "<b>Reason for Return: </b>" + "  " + Reason2 + "<br/>" + "<br/>" +
                      "<br/>" +
                      "<b>Notes:" + "  " + Notes
            };

            var builder = new BodyBuilder();
            //check for return attachment and if found, assign attachment to message via bodybuilder
            if (checkBox5.Checked)
            {
                builder.TextBody = body2.Text;
                builder.HtmlBody = body2.Text;
                builder.Attachments.Add(ReturnAttachment1);
                message.Body = builder.ToMessageBody();
            }
            if (checkBox7.Checked)
            {
                builder.TextBody = body2.Text;
                builder.HtmlBody = body2.Text;
                builder.Attachments.Add(ReturnAttachment1);
                builder.Attachments.Add(ReturnAttachment2);
                message.Body = builder.ToMessageBody();
            }
            if (checkBox6.Checked)
            {
                builder.TextBody = body2.Text;
                builder.HtmlBody = body2.Text;
                builder.Attachments.Add(ReturnAttachment1);
                builder.Attachments.Add(ReturnAttachment2);
                builder.Attachments.Add(ReturnAttachment3);
                message.Body = builder.ToMessageBody();
            }
            else
            {
                message.Body = body2;
            }

            //Connection to SMTP and Criteria to Send
            using (var client = new SmtpClient())
            {
                // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                client.Connect("smtp.gmail.com", 587, false);

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate("***************@********.com", "*********");

                client.Send(message);
                client.Disconnect(true);

此代码一直重复到五个平板。所以现在,我有一个类:

        {
            public string Thickness { get; set; }
            public string Material { get; set; }
            public string Batch { get; set; }

            public Slab(string Thick, string Mat, string Batchno)
            {
                Thickness = Thick;
                Material = Mat;
                Batch = Batchno;
            }
        }

包含此对象的列表:

        private void button1_Click(object sender, EventArgs e)
        {
            string t = comboBox1.SelectedValue.ToString();
            string m = comboBox2.SelectedValue.ToString();
            string b = textBox6.Text;

            Slab S = new Slab(t, m, b);

            allSlabs.Add(S);
            PaintTree();
        }

        public void PaintTree()
        {
            int ParentIndex; 
            TreeSlabs.Nodes.Clear();

            foreach (Slab slab in allSlabs)
            {
                TreeSlabs.BeginUpdate();
                TreeSlabs.Nodes.Add("Slab" + " " + (allSlabs.IndexOf(slab) + 1).ToString());
                ParentIndex = allSlabs.IndexOf(slab);
                TreeSlabs.Nodes[ParentIndex].Nodes.Add("Thickness:  " + $"{slab.Thickness}");
                TreeSlabs.Nodes[ParentIndex].Nodes.Add("Material:  " + $"{slab.Material}");
                TreeSlabs.Nodes[ParentIndex].Nodes.Add("Batch:  " + $"{slab.Batch}");
                TreeSlabs.EndUpdate();


            }
        }

现在我想创建一个 foreach.. 遍历每个节点.. 并从父级及其子级 foreach 节点收集信息,并以某种方式为每个节点实例化新的 HTML 方法..

Foreach 节点: 编写 HTML 行 - 新 HTML

板 1: 厚度: 材料 批次

平板 2:...等

如果有 8 个节点,它会生成 8 个这样的实体。我可以想办法……但我知道,根据我在那里阅读和看到的内容,它们肯定不是正确的方法。

非常感谢

【问题讨论】:

  • var builder = new BodyBuilder(); 让我开心 ;)
  • 我想补充一点,我错过了。我将平板对象存储在 List 中,TreeView 从中提取数据。但是我 +1 了索引..这使得将其映射回该列表有点棘手。所以我想我会从树视图生成电子邮件,而不是写回更改。我可以实例化一个 html 脚本来生成每个平板并将其自身添加到正文中吗?
  • 我不知道为什么我是那个菜鸟????????‍♂️
  • 旁注,Add($"Batch: {slab.Batch}") 更接近于 Add("Batch: " + $"{slab.Batch}")

标签: c# winforms


【解决方案1】:

您无需遍历树并尝试从 Slab 列表中回收您放入其中的数据;枚举列表会更简单:

顺便说一下,还有一些提示:

  • 不要在循环内开始更新/结束更新,在外面做
  • 将节点添加到树的 Add 方法返回它添加的节点;您不必通过索引再次找到它,只需捕获它var justAdded = rootnode.Add("parent node");,然后将您的厚度等添加到它justAdded.Nodes.Add("thickness...")。唯一不返回添加模式的添加命令是采用树节点类型对象的命令;它不需要退回,因为你已经拥有它了
  • 考虑向您的平板添加一个方法以返回一些 html,以简化事情
  • 您可以像使用 $"{slab.thickness}" 等那样使用字符串插值来整理所有 html

【讨论】:

  • 您好 Caius,非常感谢您的回复,我会听取您的建议并整理 html 等,并按照您的建议获取所有内容,然后重试。
猜你喜欢
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多