如何遍历文本框并验证是否存在重复值?

时间:2019-05-11 11:27:04

标签: vb.net

我正在尝试设置一个将邮件发送到已定义的收件人列表的功能。 每个收件人都是从文本框中获取的。问题是我想确定是否存在重复值,并在发送电子邮件时将其排除。

例如,如果textbox1与textbox2具有相同的值,则不要在收件人列表中包括textbox1的值。

我尝试了以下子项

Private Sub CheckDuplicates()
        Dim x As Long
        Dim y As Long
        Dim User() As TextBox = {Mail_user1, Mail_user2, Mail_user3, Mail_user4, Mail_user5, Mail_user6, Mail_user7, Mail_user8, Mail_user9, Mail_user10, Mail_user11, Mail_user12, Mail_user13, Mail_user14, Mail_user15, Mail_user16, Mail_user17, Mail_user18, Mail_user19, Mail_user20, Mail_user21, Mail_user22, Mail_user23, Mail_user24, Mail_user25, Mail_user26, Mail_user27, Mail_user28, Mail_user29, Mail_user30}

        For x = 1 To 30 - 1
            For y = x + 1 To 30
                If User(x).Text = User(y).Text Then
                    User(y).Text = ""
                End If
            Next
        Next

    End Sub

问题是当我要发送邮件时出现以下错误:

Index was outside the bounds of the array.

邮件子看起来像这样:

Public Function AddRecipients(mail As outlook.MailItem) As Boolean
        Dim retValue As Boolean = False
        Dim recipients As outlook.Recipients = Nothing
        Dim recipientTo As outlook.Recipient = Nothing
        Dim recipientCC As outlook.Recipient = Nothing
        Dim recipientBCC As outlook.Recipient = Nothing

        Try
            recipients = mail.Recipients
            ' check if there are any recipients and remove them
            While recipients.Count > 0
                recipients.Remove(1)
            End While
            ' new recipients list

            CheckDuplicates()

            '------------------CC section---------------------------
            recipientCC = recipients.Add("someemail@test.com")
            recipientCC.Type = outlook.OlMailRecipientType.olCC

            'hidden recipients section
            ' recipientBCC = recipients.Add("")
            ' recipientBCC.Type = outlook.OlMailRecipientType.olBCC
            retValue = recipients.ResolveAll()
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            If Not IsNothing(recipientBCC) Then Marshal.ReleaseComObject(recipientBCC)
            If Not IsNothing(recipientCC) Then Marshal.ReleaseComObject(recipientCC)
            If Not IsNothing(recipientTo) Then Marshal.ReleaseComObject(recipientTo)
            If Not IsNothing(recipients) Then Marshal.ReleaseComObject(recipients)
        End Try
        Return retValue
    End Function

    Private Sub MailTime()
        Dim OTmail As outlook.MailItem
        Dim AppOutlook As New outlook.Application
        Try
            OTmail = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
            'add users from AddRecipients
            AddRecipients(OTmail)
            OTmail.Subject = "Test OT mail"

            OTmail.Body = "Test Ot mail"
            OTmail.BodyFormat = outlook.OlBodyFormat.olFormatHTML
            OTmail.Display()
        Catch ex As Exception
            MessageBox.Show("Could not send, resolve the errors !")
            MessageBox.Show(ex.ToString)
        Finally
            OTmail = Nothing
            AppOutlook = Nothing
        End Try
    End Sub

3 个答案:

答案 0 :(得分:3)

这将遍历所有文本框,并为您提供一个不同的列表。

Private Function uniqueRecipients() As List(Of String)

    Dim recipients As List(Of String) = New List(Of String)
    For Each ctrl As TextBox In Me.Controls.OfType(Of TextBox)
        recipients.Add(ctrl.Text)
    Next

    Return recipients.Distinct.ToList

End Function

Private Sub Button26_Click(sender As Object, e As EventArgs) Handles Button26.Click

    Try

        Dim myRecips As List(Of String) = uniqueRecipients()
        Dim oneLine As String = Strings.Join(myRecips.Where(Function(s) Not String.IsNullOrEmpty(s)).ToArray(), ";")
        'send mail

    Catch ex As Exception
        MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
    End Try

End Sub

答案 1 :(得分:1)

为作业使用正确的工具类型-HashSet(Of String)Enumerable.ToHashSet Method

Private Function GenerateMailRecipientsFrom(textboxes As IEnumerable(Of TextBox)) As String
    Dim uniqueRecipients = textboxes.
        Select(Function(textbox) textbox.Text).
        Where(Function(text) String.IsNullOrWhiteSpace(text) = False).
        ToHashSet()

    Return String.Join(";", uniqueRecipients)
End Function

HashSet仅接受唯一值。

然后使用表单上所有文本框的集合

Dim mailTo As String = GenerateMailRecipientsFrom(Me.Controls.OfType(Of TextBox))

在预定义了文本框集合之后,您仍然可以使用相同的方法

Dim userMailTextBoxes As textBox() = { Mail_user1, Mail_user2, .. }
Dim mailTo As String = GenerateMailRecipientsFrom(userMailTextBoxes)

答案 2 :(得分:0)

好极了! @Fabrio感谢您的代码和解释。附带说明,我尝试将唯一值加载到列表框中,然后在使用此方法的同时将其插入到Outlook电子邮件中:

 Dim x As Long
            For x = 0 To ListBox1.Items.Count - 1
                If ListBox1.Items.Item(x) <> "" Then
                    recipientTo = recipients.Add(ListBox1.Items.Item(x))
                    recipientTo.Type = outlook.OlMailRecipientType.olTo
                End If
            Next 

像魅力一样工作:)