KiXforms Forum Index KiXforms
The Forum for the KiXforms Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Quick Links 
Site News
Downloads
Documentation
Donations
Script Archive
Tracking Systems

Embed ComboBox in ListView - Revisited (SubItems support)

 
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion
View previous topic :: View next topic  
Author Message
pearly
KiXforms Aficionado
KiXforms Aficionado


Joined: 27 Jan 2004
Posts: 332

PostPosted: Wed Oct 31, 2007 6:57 pm    Post subject: Embed ComboBox in ListView - Revisited (SubItems support) Reply with quote

I was trying to look for a topic regarding ComboBox auto-completion and came across this topic about embedding ComboBoxes in ListViews. After reviewing the article here:

http://www.dotnet247.com/247reference/msgs/14/71810.aspx

I came up with an idea using the existing functionality that allows support for SubItems.

There is a small problem with the ListView method GetItemAt. First and foremost, it doesn't support SubItems. Secondly, it has a problem retrieving the Item if the user doesn't click the Item's Text. The example Shawn posted in

http://www.kixforms.org/forum/viewtopic.php?t=1236

had default Column widths of 60. GetItemAt method would not retrieve the item when clicking anywhere between 38-60 (or 1) of the x-coordinate.

Therefore, I came up with an idea to create my own GetItemAt method function called ListViewGetItemAt().

It basically hardcodes the x coordinate to 2 (for some reason 1 didn't work) to locate the ListViewItem object for that row. Then I calculate the y range for all Columns. After that, it's simple to set the Bounds for the ComboBox and set the value of the Item's text or Item's Subitem's text.

Here is the example. Feedback welcome Very Happy

Code:

Break On

$System = CreateObject("Kixforms.System")

$Form = $System.Form()
$Form.Text = "Form1"
$Form.Width = 640
$Form.Height = 480

$ListView = $Form.Controls.Add($System.ListView())
$ListView.name = "ListView"
$ListView.Dock = $System.DockStyle_Fill
$ListView.View = $System.View_Details
$ListView.MouseUp = "ListView_MouseUp()"
$ListView.GridLines = -1

$Column1 = $ListView.Columns.Add($System.ColumnHeader())
$Column1.Text = "Column1"
$Column1.Width = 125
$Column2 = $ListView.Columns.Add($System.ColumnHeader())
$Column2.Text = "Column2"
$Column2.Width = 125
$Column3 = $ListView.Columns.Add($System.ColumnHeader())
$Column3.Text = "Column3"
$Column3.Width = 125
$Column3 = $ListView.Columns.Add($System.ColumnHeader())
$Column3.Text = "Column4"
$Column3.Width = 125

For $i = 1 to 10
 $ListViewItem = $ListView.Items.Add($System.ListViewItem("Item$i"))
 $ListViewSubItem = $ListViewItem.SubItems.Add($System.ListViewSubItem("Item" + $i + "_Subitem1"))
 $ListViewSubItem = $ListViewItem.SubItems.Add($System.ListViewSubItem("Item" + $i + "_Subitem2"))
 $ListViewSubItem = $ListViewItem.SubItems.Add($System.ListViewSubItem("Item" + $i + "_Subitem3"))
Next

$ComboBox = $Form.Controls.Add($System.ComboBox())
$ComboBox.BringToFront
$ComboBox.Hide
$ComboBox.KeyUp = "ComboBox_KeyUp()"
$ComboBox.SelectedIndexChanged = "ComboBox_SelectedIndexChanged()"

$= $ComboBox.Items.Add("Value1")
$= $ComboBox.Items.Add("Value2")
$= $ComboBox.Items.Add("Value3")
$= $ComboBox.Items.Add("Value4")
$= $ComboBox.Items.Add("Value5")

$Form.Center
$Form.Show

While $Form.Visible
 $= Execute($Form.DoEvents)
Loop

Exit 0

Function ListViewGetItemAt($x, $y)
   
   ;? "[x,y] = " + $x + "," + $y
   
   $ListViewItem = $ListView.GetItemAt(2, $y)   
   If $ListViewItem
      ReDim $arr[$ListView.Columns.Count - 1, 1]
      $iCounter = 0
     
      ;\\ Get All Column Bounds and store in array
      For $i = 0 to $ListView.Columns.Count - 1
         If $iCounter = 0
            $arr[$iCounter,0] = 1   
            $arr[$iCounter,1] = $ListView.Columns.Item($i).Width
         Else
            $arr[$iCounter,0] = $arr[$iCounter-1,1] + 1
            $arr[$iCounter,1] = $arr[$iCounter,0] + $ListView.Columns.Item($i).Width - 1
         EndIf
         $iCounter = $iCounter + 1
      Next
   
      $ClickedItem = $ListViewItem.Bounds   
   
      ;\\ Set ComboBox Bound for Clicked ListView Item
      For $j = 0 to UBound($arr)
         If $arr[$j,0] <= $x And $x <= $arr[$j,1]
            ;? "Column Clicked = " + $ListView.Columns.Item($j).Text
            ;Item Column
            If $j = 0               
               $ClickedItem.X = $ClickedItem.X + $ListView.Left + 1
               $ClickedItem.Y = $ClickedItem.Y + $ListView.Top
               $ComboBox.Bounds = $ClickedItem
               $ComboBox.Text = $ListViewItem.Text
            $ActiveListViewItem = $ListViewItem
            ;SubItem Column   
            Else
               $ClickedItem.X = $arr[$j,0] + $ListView.Left + 1
               $ClickedItem.Y = $ClickedItem.Y + $ListView.Top           
               $ComboBox.Bounds = $ClickedItem           
               $ComboBox.Text = $ListViewItem.SubItems.Item($j).Text               
            $ActiveListViewItem = $ListViewItem.SubItems.Item($j)
            EndIf
            $ComboBox.Width = $ListView.Columns.Item($j).Width
         EndIf
      Next
        $ComboBox.Visible = 1
        $ComboBox.BringToFront()
        $ComboBox.Focus()     
   EndIf
EndFunction

Function ListView_MouseUp

 ; Get the item that was clicked

 $e = $System.EventArgs

 ListViewGetItemAt($e.X, $e.Y)

EndFunction

Function ComboBox_KeyUp()

 $e = $System.EventArgs

 ;// Check if user types "ESC"

 If $e.KeyCode = $System.Keys_Escape
  ;// Hide the combobox
  $ComboBox.Visible = 0;
 EndIf

EndFunction

Function ComboBox_SelectedIndexChanged()

 ; Set text of listview item

 $ActiveListViewItem.Text = $ComboBox.Text
 $ComboBox.Text = ""

 ; Hide the combobox

 $ComboBox.Visible = 0

EndFunction
Back to top
View user's profile Send private message
pearly
KiXforms Aficionado
KiXforms Aficionado


Joined: 27 Jan 2004
Posts: 332

PostPosted: Tue Nov 06, 2007 1:27 am    Post subject: Reply with quote

Shawn,

I discovered GetItemAt method does not return a ListViewItem if any of the following is enabled:

$System.EnableVisualStyles

OR

$ListView.CheckBoxes = -1


Is this a bug or by design? Crying or Very sad
Back to top
View user's profile Send private message
pearly
KiXforms Aficionado
KiXforms Aficionado


Joined: 27 Jan 2004
Posts: 332

PostPosted: Tue Nov 06, 2007 2:08 am    Post subject: Reply with quote

Adding

Code:
$ListView.FullRowSelect = -1


seems to have resolved the problem, although cosmetically, it's slightly not sound. I'm just glad it's working Very Happy
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Wed Nov 07, 2007 4:56 am    Post subject: Reply with quote

Looks like a common dotnet problem. Did a quick search and found this:

http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.windowsforms/2004-10/0670.html

Strange one.

-Shawn
Back to top
View user's profile Send private message
pearly
KiXforms Aficionado
KiXforms Aficionado


Joined: 27 Jan 2004
Posts: 332

PostPosted: Wed Nov 07, 2007 6:44 pm    Post subject: Reply with quote

Thanks Shawn!

The combobox is working out really well in my listview. The code I posted above seems to hold out pretty good. Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group