String Insert方法列表增加了它的大小

时间:2016-12-16 10:58:47

标签: vb.net list linked-list insert

这是代码

    Public Class FrmPatientFolder 

Dim tab1RTBtext As List(Of String)
Dim comboboxprevious As Integer
Dim comboboxcurrent As Integer

Private Sub FrmPatientFolder_Load(sender As Object, e As EventArgs) Handles MyBase.Load

'this contains 12 string which are too long to include in code
tab1combolist = New List(Of String)(New String() {"1",..to..,"12"}

ComboBox1.Items.AddRange(tab1combolist.ToArray)
ComboBox1.SelectedIndex = 0

comboboxprevious = -1
comboboxcurrent = 0

'this performs a select query that either returns a list of 12 strings or nothing
tab1RTBtext = selectFromTable(New List(Of String)(New String() {"*"}), "chronicle", "amka", Me.Text)


If tab1RTBtext Is Nothing Then
tab1RTBtext = New List(Of String)
tab1RTBtext.Add(Me.Text)

For Each item In tab1combolist
tab1RTBtext.Add("")
Next

insertNewPatientIntoTable("chronicle", tab1RTBtext)
tab1RTBtext.RemoveAt(0)

Else         
tab1RTBtext.RemoveAt(0)
RichTextBox1.Rtf = tab1RTBtext(0)

End If

End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

     If comboboxprevious <> -1 Then
        comboboxprevious = comboboxcurrent
        comboboxcurrent = ComboBox1.SelectedIndex

    Else
        comboboxcurrent = ComboBox1.SelectedIndex
        comboboxprevious = comboboxcurrent
    End If

    If comboboxcurrent <> comboboxprevious And comboboxprevious <> -1 Then

        Console.WriteLine(tab1RTBtext.Count)
        Console.WriteLine(comboboxprevious & tab1RTBtext(comboboxprevious))

        tab1RTBtext.Insert(comboboxprevious, RichTextBox1.Rtf.ToString)
        Console.WriteLine(tab1RTBtext.Count)
        Console.WriteLine(comboboxprevious & tab1RTBtext(comboboxprevious))
        RichTextBox1.Rtf = tab1RTBtext(comboboxcurrent).ToString
    End If

End Sub


End Sub

如果在运行时选择第二项组合框

会发生这种情况
12
0
13
0{\rtf1\ansi\ansicpg1253\deff0\deflang1032{\fonttbl{\f\fnil\fcharset161 Trebuchet MS;}}
\viewkind4\uc1\pard\f0\fs24\par
}

所以我告诉列表,如果组合框的索引发生了变化,则将richtextbox.rtf存储在前一个组合框项的索引处。然而,每次调用insert方法时,都会增加列表大小。为什么会这样?除此之外,当我更改组合框中的选定项目时,它没有按预期执行,它没有显示正确的值,这意味着列表的大小不会从结束但是从插入点扩展?

也许rtf的处理在这里是错误的,我应该用空字符串检查格式,然后在存储之前清除内容,但我无法想象这对我面临的问题有什么影响。

我在这里阅读https://www.dotnetperls.com/list-insert列表类是针对add方法进行了优化的,如果想要使用insert,则应该考虑使用linkedlist类,但如果我这样做那么我将不得不在许多其他地方调整我的代码。

1 个答案:

答案 0 :(得分:0)

更改此

tab1RTBtext.Insert(comboboxprevious, RichTextBox1.Rtf.ToString)

tab1RTBtext(comboboxprevious) = RichTextBox1.Rtf.ToString

这就是当您对方法如何工作做出假设时会发生的事情。我希望你好笑,或者至少傻笑。 :)