VB.NET 中的剪贴板数据排序 [英] Clipboard data sort in VB.NET

查看:22
本文介绍了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:

公司:TEST LTD

Company: TEST LTD

客户姓名:乔·约翰逊

部门:IT 运营经理

客户电话:012345678910

Customer's phone: 012345678910

客户的电子邮件:JOE.JOHNSON@TEST.COM

Customer's email: JOE.JOHNSON@TEST.COM

仪器:SUP

序列号:EM2PC2938C

Serial Number: EM2PC2938C

覆盖范围:计划无保修

所以,我的问题是:

我需要能够直接从电子邮件中复制此文本,单击将查看剪贴板中文本的按钮,然后将不同的信息放入与电子邮件中标记相同的不同文本框中,但没有标识符(例如电子邮件:"或覆盖范围"),因此冒号进入文本框之后的任何内容基本上都是如此.

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.

推荐答案

代替 .Substring.IndexOf,我会使用 String.Split.

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

从剪贴板上获取字符串并将其拆分为行,删除所有空行.获取结果数组的每个元素(一行)并用冒号分割.我们希望此数组 (1) 的第二个元素去除任何前导或尾随空格并分配给适当的文本框.然后重复每一行.

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

编辑

如果前后有随机文字...

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

我刚刚添加了一个 For 循环来查找从哪里开始读取数据.通过直接从您的问题中复制数据前后的几个段落进行测试.我更改了文本框的名称以匹配我可用的内容.

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.

这篇关于VB.NET 中的剪贴板数据排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆