Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

Clipboard data sort in VB.NET

I have a form in which I need to sort text from the clipboard into the appropriate fields. Essentially, I copy a customer email, click a button on the form, and it will read the data and put the info into the fields. There are several fields, such as the customer's name, the company's name, the telephone number, whether they have a support plan.

All emails are formatted in the same way (apart from the random spaces which I have no control of) and are used by several employees:

Company: TEST LTD

Customer's name: Joe Johnson

Department: IT Operations Manager

Customer's phone: 012345678910

Customer's email: [email protected]

Instrument: SUP

Serial Number: EM2PC2938C

Coverage: Plan No Warranty

So, my question is:

I need to be able to copy this text straight from an email, click on a button that will look through the text in the clipboard, and put the different pieces of information into different text boxes labelled the same as in the email, but without the identifier (such as 'Email:' or 'Coverage'), so essentially anything after the colon goes into the textbox.

I have some code to get the data and put into into a rich text box which I planned to use to sort the data (I know I can do it straight from code, but didn't know how to)

rtbx_ClipboardData.text = Clipboard.GetText

And some code to remove any text before the colon:

tbx_Data_Company.Text = rtbx_ClipboardData.Text.Substring(rtbx_ClipboardData.Text.IndexOf(":") + 1)

This code works but I need to do it several times and put data into the appropriate fields which is where I am struggling.

If anyone could suggest anything/provide some sample code I'd greatly appreciate it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of .Substring and .IndexOf, I would use String.Split.

Get the string from the clip board and split it into lines removing any empty lines. Take each element of the resulting array (one line) and split that by the colon. We want the second element of this array (1) trimmed of any leading or trailing spaces and assigned to the appropriate text box. Then repeat with each line.

Private Sub GetFieldsFromEmail()
    Dim eText As String = Clipboard.GetText()
    Dim lines = eText.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
    txtCompany.Text = lines(0).Split(":"c)(1).Trim
    txtName.Text = lines(1).Split(":"c)(1).Trim
    txtDepartment.Text = lines(2).Split(":"c)(1).Trim
    txtPhone.Text = lines(3).Split(":"c)(1).Trim
    txtEmail.Text = lines(4).Split(":"c)(1).Trim
    txtInstrument.Text = lines(5).Split(":"c)(1).Trim
    txtSerNum.Text = lines(6).Split(":"c)(1).Trim
    txtCoverage.Text = lines(7).Split(":"c)(1).Trim
End Sub

EDIT

If there is random text before and after...

Private Sub GetFieldsFromEmail()
    Dim eText As String = Clipboard.GetText()
    Dim lines = eText.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
    Dim StartLine As Integer
    For i = 0 To lines.Count - 1
        If lines(i).StartsWith("Company") Then
            StartLine = i
            Exit For
        End If
    Next
    TextBox18.Text = lines(StartLine).Split(":"c)(1).Trim
    TextBox19.Text = lines(StartLine + 1).Split(":"c)(1).Trim
    TextBox20.Text = lines(StartLine + 2).Split(":"c)(1).Trim
    TextBox21.Text = lines(StartLine + 3).Split(":"c)(1).Trim
    TextBox22.Text = lines(StartLine + 4).Split(":"c)(1).Trim
    TextBox23.Text = lines(StartLine + 5).Split(":"c)(1).Trim
    TextBox24.Text = lines(StartLine + 6).Split(":"c)(1).Trim
    TextBox25.Text = lines(StartLine + 7).Split(":"c)(1).Trim
End Sub

I just added a For loop to find where to start reading the data. Tested by copying several paragraphs before and after your data directly from your question. I changed the names of the text boxes to match what I had available.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...