2016-12-16 158 views
0

下面是代碼列表增加其大小

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存儲在前一個組合框項目的索引處。但每次調用插入方法時,都會增加列表大小。這是爲什麼發生?除此之外,當我更改組合框中的選定項目時,它沒有按照它應該做的那樣去做,它不顯示正確的值,這意味着列表的大小不會從最後擴展而是從插入點擴展?

也許在這裏rtf的處理是錯誤的,我應該檢查格式與空字符串,然後清除內容之前存儲,但我無法想象這可能會對我面臨的問題有任何影響。

我在這裏讀到https://www.dotnetperls.com/list-insert列表類是爲add方法優化的,如果想要使用insert代替,應該考慮使用linkedlist類,但是如果我要這樣做,那麼我將不得不調整我的代碼在許多其他地方。

+0

嗯,我很愚蠢......在這種情況下不應該使用插入......我應該直接將值放在我的列表中以前的索引......難怪沒有人想陳述明顯。 – kokotas

回答

0

改變這種

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

tab1RTBtext(comboboxprevious) = RichTextBox1.Rtf.ToString 

,這是當你做出一個方法是如何工作的假設會發生什麼。我希望你有一個好笑,或者至少咯咯地笑。 :)