【问题标题】:How do I limit the values I can save?如何限制我可以保存的值?
【发布时间】:2017-08-29 06:17:41
【问题描述】:

我目前正在制作一个注册表单,其中学生的所有详细信息都保存在一个文本文件中。

在我的一个领域,我有一个combobox 列表,列出了他/她可以选择的所有学校。

我使用textfile 填充了combobox

这些值的格式例如:(code~school name) SCH001~Saint Thomas College

问题 - 如何限制我可以保存的值? 示例 - 我只想保存学校代码而不保存学校名称:SCH001

以下是我将字段保存在文本文件中的方法:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
    Dim firstname, lastname, email, mobile, level, currentschool, currenttrack, institution1, institution2, institution3, institution4, institution5, institution6, courses1, courses2, courses3 As String
    firstname = txtFName.Text
    lastname = txtLName.Text
    email = txtEmail.Text
    mobile = txtMobile.Text
    level = cmbLevel.Text
    currentschool = cmbCurrentSchool.Text
    currenttrack = cmbCurrentTrack.Text
    institution1 = cmbInstitution1.Text
    institution2 = cmbInstitution2.Text
    institution3 = cmbInstitution3.Text
    institution4 = cmbInstitution4.Text
    institution5 = cmbInstitution5.Text
    institution6 = cmbInstitution6.Text
    courses1 = cmbCourse1.Text
    courses2 = cmbCourse2.Text
    courses3 = cmbCourse3.Text

    Using sw As StreamWriter = File.AppendText("C:\Users\jmrosales\Documents\RegistrationForm\Registered.txt")
        sw.WriteLine(firstname & "~" & lastname & "~" & email & "~" & mobile & "~" & level & "~" & currentschool & "~" & currenttrack & "~" & institution1 & "~" & institution2 & "~" & institution3 & "~" & institution4 & "~" & institution5 & "~" & institution6 & "~" & courses1 & "~" & courses2 & "~" & courses3)
    End Using
    MsgBox("Registration Complete!")
End Sub

我希望你们明白我想说什么。 我是新来的。

【问题讨论】:

    标签: string vb.net visual-studio


    【解决方案1】:

    您可以通过两种方式做到这一点:

    1. 使用.Split()
    2. 使用.Substring()

    1. 使用.Split()
      您的代码如下所示:

      currentschool = cmbCurrentSchool.Text.Split("~")

    这将返回两个字符串,"SCH001""Saint Thomas College"
    但是由于您只需要代码,因此请将(0) 放在上面代码的末尾:

    currentschool = cmbCurrentSchool.Text.Split("~")(0)

    您还可以在上面的 sn-p 中添加c 像:

    currentschool = cmbCurrentSchool.Text.Split("~"c)(0)

    这将指定~ 是一个字符


    1. 使用.Substring()
      这只有在确定您的代码始终为 6 个字符(SCH 为 3,数字代码为 3)时才有效

      currentschool = cmbCurrentSchool.Text.Substring(0,6)

    这里,0 表示从 第一个 位置开始选择字符,6 表示获取 六个字符 以获取新字符串。

    希望这会有所帮助:)

    【讨论】:

    • 感谢您更正我的代码,这是我的错字,我什至没有意识到!
    • +1 用于深入解释答案,我没有。你也可以在("~") 中做("~"c) 来指定它是一个字符
    • 谢谢大家的回答。真的帮了大忙。我的问题终于解决了。
    • 不客气 :) 别忘了投票给答案 xD
    • 我并不是要粗鲁或任何事情,虽然您确实纠正了没人的错字,但您也几乎写下了我们答案的精确副本。 -- 不要误会我的意思......我很感激你参与社区的帮助者方面,但是当已经有其他答案时,写另一个只有微小改进的东西是没有必要的(不是说你是故意这样做的,只是想提醒你一下)。快乐编码(和回答)!
    【解决方案2】:

    假设学校代码将遵循 正好 6 个字符,就像在这个 SCH001~Saint Thomas College 中一样,您可以使用 Substring

    currentschool = cmbCurrentSchool.Text.Substring(0,6) '6 means 6 characters to be cut from cmbCurrentSchool to currentschool 
    

    如果这里currentschool 存储学校代码。

    否则,您可以使用Split

    currentschool = cmbCurrentSchool.Text.Split("~")(0)
    

    【讨论】:

      【解决方案3】:

      最简单的答案是在~ 处拆分字符串,然后只返回您需要的部分:

      If cmbCurrentSchool.Text.Contains("~") Then 'A check to avoid possible errors.
          currentschool = cmbCurrentSchool.Text.Split("~"c)(0) '0 means that we should get the first item of the array, thus "SCH001".
      Else
          currentschool = cmbCurrentSchool.Text
      End If
      

      cmbCurrentSchool.Text.Split("~"c) 将返回一个包含两个字符串的数组:

      "SCH001~Saint Thomas College"  ->  {"SCH001", "Saint Thomas College"}
      

      文档:

      【讨论】:

        猜你喜欢
        • 2014-02-14
        • 1970-01-01
        • 2010-10-11
        • 2013-03-15
        • 1970-01-01
        • 2011-06-18
        • 1970-01-01
        • 2014-07-09
        • 1970-01-01
        相关资源
        最近更新 更多