Kirjautuminen

Haku

Tehtävät

Keskustelu: Ohjelmointikysymykset: VB.NET: [VB.Net] ListBoxin solun valintatila

novice [02.03.2010 22:12:04]

#

Miten saisin tietoon MultiSelect ListBoxin viimeksi klikatun solun valintatilan, eli siis klikattinko se päälle vai pois päältä? Saman solun järjestysnumero olisi myös kiva tietää.

groovyb [03.03.2010 07:59:18]

#

Public Class Form1

' tässä esimerkissä luo formille multiselect listbox (ListBox1) sekä nappi (Button1)

Private clickorder As New List(Of Integer)

Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown

   Dim itemIndex As Integer = Me.ListBox1.IndexFromPoint(e.Location)
   If itemIndex <> ListBox.NoMatches Then
      If Me.ListBox1.GetSelected(itemIndex) Then
         clickorder.Add(itemIndex)
      Else
         clickorder.Remove(itemIndex)
      End If
   End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim builder As New System.Text.StringBuilder
    builder.AppendLine("Tässä järjestyksessä valittiin:")
     For Each ix As Integer In clickorder
        builder.AppendFormat("index {0}: {1}" & vbNewLine, ix, Me.ListBox1.Items(ix))
     Next
     MessageBox.Show(builder.ToString)
End Sub

End Class

novice [03.03.2010 21:35:11]

#

Kiitos groovyb... sain tuosta kaiken mitä tarvitsin...luulisin ;)

neau33 [08.03.2010 12:34:34]

#

Morjens taas novice!

tässä hauska pikku leikki samasta aiheesta...

'väännetty SharpDevelop 3.2'lla
'MainForm.vb
Public Partial Class MainForm

   Public Shared CtlS As New CtlState
   Public Shared FrmC As New FormsCollection

   Public Sub New()
      Me.InitializeComponent()
   End Sub

   Sub MainFormLoad(sender As Object, e As EventArgs)
      FrmC.SetForm(sender)
   End Sub

   Sub ListBox1MouseUp(sender As Object, e As MouseEventArgs)
      CtlS.SetState(sender, e)
      'Me.Text = CtlS.GetState(sender)(0)
   End Sub

   Sub ListBox2MouseUp(sender As Object, e As MouseEventArgs)
      CtlS.SetState(sender, e)
   End Sub

   Sub Button1Click(sender As Object, e As EventArgs)

      If  Not CtlS.LastChangedCtl Is Nothing _
      And CtlS.HasControls Then
         Me.Text = CtlS.GetState(CtlS.LastChangedCtl)(0)
      Else
         Me.Text = "No selection done yet"
      End If
   End Sub

End Class
'CtlState.vb
Imports System.Drawing
Imports System.Windows.Forms

Public Class CtlState

   Private Structure SelectionStruct
      Dim Ctl As Control
      Dim Index As Integer
      Dim State As String
   End Structure

   Private LastChange() As SelectionStruct
   Private LastCtl As Object
   Private StructHasControls As Boolean

   Public Sub SetState(ctl As Object, e As MouseEventArgs)

      Using bmp As New Bitmap(e.x, e.y)

         Using g As Graphics = Graphics.FromImage(bmp)
            g.CopyFromScreen(Windows.Forms.Cursor.Position, _
            New Point(0, 0), New Size(1, 1))
         End Using

         Dim c, bc  As New Color
         c = bmp.GetPixel(0, 0)
         bc = ctl.BackColor
         Dim index As Integer
         For i As Integer = 0 To _
         LastChange.GetUpperBound(0)
            If LastChange(i).Ctl Is ctl Then
               index = i: Exit For
            End If
         Next
         If c.R = bc.R And c.G = bc.G And c.B = bc.G Then
            LastChange(index).Index = _
            ctl.IndexFromPoint(e.Location)
            LastChange(index).State = "removed"
         Else
            LastChange(index).Index = _
            ctl.IndexFromPoint(e.Location)
            LastChange(index).State = "selected"
         End If

         LastChangedCtl = ctl

      End Using

   End Sub

   Public Function GetState(ctl As Control) As Object

      Dim retObj(3) As Object
      For i As Integer = 0 To _
      LastChange.GetUpperBound(0)
         If LastChange(i).Ctl Is ctl Then
            retObj(0) = "Prent control=" + _
            LastChange(i).Ctl.Parent.Name + _
            "  Control=" + LastChange(i).Ctl.Name _
            + "  Last changed index=" + _
            LastChange(i).Index.ToString _
            + "  Status=" + LastChange(i).State
            retObj(1) = LastChange(i).Ctl
            retObj(2) = LastChange(i).Index
            retObj(3) = LastChange(i).State
            Return retObj: retObj = Nothing
            Exit Function
         End If
      Next

      Return Nothing

   End Function

   Public Sub SetControls(frm As Form)

      Dim cnt As Integer = -1

      For Each ctl As Control In frm.Controls
         With ctl
            If TypeOf(ctl) Is ListBox Then
               Dim ctlx As ListBox = ctl
               ctlx.Height = ctlx.ItemHeight * _
               ctlx.Items.Count + ctlx.ItemHeight
               cnt += 1: ctlx = Nothing
               ReDim Preserve  LastChange(cnt)
               LastChange(cnt).Ctl = ctl
               StructHasControls = True
            End If
         End With
      Next
   End Sub

   Public ReadOnly Property HasControls() As Boolean
       Get
          Return StructHasControls
       End Get
   End Property

   Public Property LastChangedCtl() As Object
      Get
         Return LastCtl
      End Get
      Set(value As Object)
         LastCtl = value
      End Set
   End Property

End Class

Public Class FormsCollection

   Shared Forms As ArrayList = New ArrayList

   Public Sub SetForm(frm As Form)

      Dim IsExisting As Boolean = False

      For Each fctl As Form In FormsCollection.Forms
         If frm Is fctl Then
            IsExisting = True
         End If
      Next

      If Not IsExisting Then
         FormsCollection.Forms.Add(frm)
         MainForm.CtlS.SetControls(frm)
      End If

   End Sub

End Class

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta