Re: [.NET] 關於listbox與 textbox已刪文

看板Visual_Basic作者 (wilson)時間15年前 (2010/06/24 00:11), 編輯推噓0(007)
留言7則, 3人參與, 最新討論串2/2 (看更多)
這是我的程式碼... 現在卡在input跟output中... output的時候陣列有存取到數值 但無法存到.txt檔裡在fileput的時候卡住了>< Button4_Click也怪怪的... 我if ListBox1.Items.Count <> 0 的時候enable才是true =0時enable是false 但如果listbox是0時 要先點執行一次button4以後 再點選他才會變灰色的無法點選 而且變完就算我再加條目進Listbox 我的button4還是灰色的無法點取 Public Class Form1 Dim dial_save As New SaveFileDialog Dim dial_open As New OpenFileDialog Dim result, run, open As Integer Dim n, m As Integer Dim SelectedIndexChanged As Integer = 0 Dim num As Integer = 0 Dim temp() As phonebook Dim wilson As counter Dim i As String = 0 Structure phonebook Dim name As String Dim phone As String Dim mobile As String End Structure Structure counter Dim number As String End Structure Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If SelectedIndexChanged = 1 Then result = MessageBox.Show("隢鲳薈脣궘瑼鯏ꆈ!", "霅血醊", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) If result = DialogResult.Cancel Then Me.Close() Else Exit Sub End If Else TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() FileClose() End If Me.Close() End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim i As Integer = 0 SelectedIndexChanged = 1 For i = 0 To n TextBox1.Text = ListBox1.SelectedItem If temp(i).name = TextBox1.Text Then TextBox2.Text = temp(i).phone TextBox3.Text = temp(i).mobile End If Next End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Label5.Text = CStr(TimeOfDay) Timer1.Enabled = True End Sub Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click wilson.number = 0 ReDim Preserve temp(wilson.number) temp(wilson.number).name = TextBox1.Text temp(wilson.number).phone = TextBox2.Text temp(wilson.number).mobile = TextBox3.Text ListBox1.Items.Add(TextBox1.Text) wilson.number += 1 TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Dim a, b As Object Dim num As String b = ListBox1.Items.Count num = b a = MsgBox("冽鲉" & num & "雿菝末鲳辋券肚閮简貄銝괠!.", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "record Count") End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim j, File3 As Object Dim sel As Object Dim t As Short sel = ListBox1.Text ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) File3 = My.Application.Info.DirectoryPath & "\N.saf" FileOpen(2, File3, OpenMode.Output) WriteLine(2) FileClose(2) t = ListBox1.Items.Count - 1 ListBox1.SelectedIndex = 0 For j = 0 To t ListBox1.SelectedIndex = j FileOpen(2, File3, OpenMode.Append) WriteLine(2, ListBox1.Text) FileClose(2) Next j TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim a, b, c, d As Object Dim num As String d = ListBox1.Items.Count num = d If d <> 0 Then c = MsgBox("券们芷妚??", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Deleting All ? ?") If c = MsgBoxResult.No Then Exit Sub FileOpen(2, My.Application.Info.DirectoryPath & "\N.saf", OpenMode.Output) WriteLine(2) FileClose(2) ListBox1.SelectedIndex = 0 b = ListBox1.Items.Count - 1 For a = 0 To b If a < b Then ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1 End If Next a ListBox1.Items.Clear() TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() Else Button4.Enabled = False End If End Sub Private Sub newToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click If SelectedIndexChanged = 1 Then result = MessageBox.Show("隢鲳薈脣궘瑼鯏ꆈ!", "霅血醊", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) If result = DialogResult.Cancel Then Me.Close() Else Exit Sub End If Else TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() FileClose() End If Me.Close() End Sub Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click dial_save.Filter = "枞궗乐辣(*.txt)|*.txt|胦鲉瑼鯏ꆈ(*.*)|*.*" result = dial_save.ShowDialog() If result = Windows.Forms.DialogResult.OK Then FileOpen(1, dial_save.FileName, OpenMode.Random, OpenAccess.ReadWrite, , 50) FilePut(1, wilson.number) For m = 0 To wilson.number FilePut(1, temp(m)) Next End If FileClose(1) End Sub Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click dial_open.Filter = "枞궗乐辣(*.txt)|*.txt|胦鲉瑼鯏ꆈ(*.*)|*.*" result = dial_open.ShowDialog() If result = Windows.Forms.DialogResult.OK Then FileOpen(1, dial_open.FileName, OpenMode.Random, OpenAccess.Read, , 50) FileGet(1, wilson.number) For m = 1 To wilson.number FileGet(1, temp(m)) ListBox1.Items.Add(temp(m).name) Next End If FileClose(1) End Sub End Class 有請眾vb大神提點... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.114.196.121

06/24 00:12, , 1F
btw 我是用vb2008...
06/24 00:12, 1F

06/24 10:43, , 2F
從你的描述來看,似乎是因為你將 item.count 這個判斷放入了
06/24 10:43, 2F

06/24 10:44, , 3F
button4的Click事件里面。建議用 listbox的change 事件試試
06/24 10:44, 3F

06/24 10:45, , 4F
而且建議如果要一開始就變灰的話,應在 form_load里添加判斷
06/24 10:45, 4F

06/24 16:09, , 5F
建議原po學習使用f8 逐步執行,來看你程式運作的流程
06/24 16:09, 5F

06/24 16:11, , 6F
搭配中斷點 來看你程式跑的流程,以及變數的變化
06/24 16:11, 6F

06/24 16:13, , 7F
一句 怪怪的...並不是解決程設的好方式- -
06/24 16:13, 7F
文章代碼(AID): #1C8Z8Rxc (Visual_Basic)
文章代碼(AID): #1C8Z8Rxc (Visual_Basic)