Page 1 of 1 1
Topic Options
#203423 - 2011-11-01 03:00 AM dynamically creating textboxes
Dr_Rick Offline
Fresh Scripter

Registered: 2009-07-05
Posts: 38
Loc: Guam
I am interested in dynamically generating textboxes from reading the column names of listview values. I would then like to be able to access the textboxes from the editing form ($frmEdit) and return the contents to the listview. I have been successful in generating the textboxes and successfully populating them on the first execution. However subsequent executions do not increment to the current selected row. First execution of the function is sent a value of false to halt generation of subsequent textboxes once they have initially been created and turned off on exit of the function.

 Code:
$ListViewEx1.SelectedItem.Index is sent to the $pos in the function.

Function Create_Form($pos)
 $col = 181
 $row = 10
  ReDim $aSelected[$ListViewEx1.Columns.Count - 1]
  For $e = 0 to ubound($aSelected)
   $aSelected[$e]=$ListViewEx1.Items($pos).Subitems($e).text
  Next
 For $d = 0 to $ListViewEx1.Columns.Count - 1
  IF $bfrmEdit="False"
   $nc=chr(36)+"Column"+($ListViewEx1.Columns($d).Index)
   $nc = $frmEdit.Controls.Add("TextBox")
   $nc.location = $col, $row
   $nc.Size = 200, 20
   $nc.ToolTip ="Column"+($ListViewEx1.Columns($d).Index)
   $row = $row + 20
   $btnOk.Top=$nc.top + 30
   $btnCancel.Top=$nc.top + 30
   $frmEdit.ClientSize = 400, $nc.bottom + 95
   $frmEdit.MaximumSize = 400, $nc.bottom + 95
   $nc.text = $aSelected[$d] 
   Else
   $nc.text = $aSelected[$d] 
  EndIf
 Next
 $bfrmEdit="True"
EndFunction


Edited by Mart (2011-11-01 09:41 AM)
Edit Reason: Please use code tags when posting code.
_________________________
Dr Rick
Computer Specialist

Top
#203424 - 2011-11-01 06:09 PM Re: dynamically creating textboxes [Re: Dr_Rick]
Dr_Rick Offline
Fresh Scripter

Registered: 2009-07-05
Posts: 38
Loc: Guam
Is there a method for removing the entire control array of the form at once? I could then regenerate it on every call to the function.
_________________________
Dr Rick
Computer Specialist

Top
#203429 - 2011-11-01 10:04 PM Re: dynamically creating textboxes [Re: Dr_Rick]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
I'm having a hard time understanding exactly what you're doing from the function you posted. Do you have a small sample form that better demonstrates the whole process?

I'm imagining a listview with an add/edit button that creates a new form, or a new section of the existing form with a number of textboxes equal to the number of columns in the listview? Info entered into these textboxes are then added to the matching columns in the listview?

Top
#203430 - 2011-11-01 10:23 PM Re: dynamically creating textboxes [Re: ShaneEP]
Dr_Rick Offline
Fresh Scripter

Registered: 2009-07-05
Posts: 38
Loc: Guam
Shane

When the line in the listview is double clicked it launches this function. I am sending the listview index into the Create_Form function. I have a load button in the main form that allows the user to select a different file. Sometimes these files have different numbers of columns when read into the listview. I have successful implementation the first time it executes the function. However subsequent executions appear to be creating more textboxes and I cannot seem to access them after creation. If I could dispose of them and recreate at each execution of the create_form function I think it would work out.

 Code:
Function Open_Item()
$j=$ListViewEx1.SelectedItem.Index
Create_Form($j)
If $ListViewEx1.SelectedItems.Count > 0
  $frmEdit.Text="Edit Selection ["+$Item.text+"]"
  $Form.enabled="False"  
  $frmEdit.center
  $frmEdit.show
 For Each $Item In $ListViewEx1.SelectedItems
   While $frmEdit.Visible And $frmEdit.Tag = 0
     $=Execute($FrmEdit.DoEvents)
    Loop
	If $frmEdit.Tag = 1 ; Save data back to ListViewEx1
     $frmEdit.Hide
     IF Not Instr($Form.text,"*") 
	  $Form.text = $Form.text + "*"
     EndIf
     Else ; Quit and return to main form
     $frmEdit.Hide
	 EndIf
  Next
EndIf
 $Form.enabled="True"  
 $frmEdit.Tag = 0
EndFunction 


Edited by Mart (2011-11-02 09:39 PM)
Edit Reason: Please use code tags when posting code.
_________________________
Dr Rick
Computer Specialist

Top
#203431 - 2011-11-01 11:05 PM Re: dynamically creating textboxes [Re: Dr_Rick]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Here is my attempt at the scenario...Let me know if I'm off base. But it creates the form,listview and the subsequent edit form based off of the $COLUMNS variable I created at the beginning for testing.

 Code:
Break On
$RC=setoption("NoVarsinstrings","on")
$RC=setoption("NoMacrosinstrings","on")

$COLUMNS = 8

$System = CreateObject("Kixtart.System")

$Form = $System.Form()
$Form.width = 100*$columns
$Form.Height = 400
$Form.Center()

$Listview = $Form.Controls.Add("ListView")
$Listview.Width = $Form.Width-100
$Listview.Height = 300
$Listview.Center()
$Listview.Top = 10

PopulateListview()

$EditButton = $Form.Controls.Add("ToolButton")
$EditButton.Text = "Edit"
$EditButton.Center()
$EditButton.Top = $ListView.Bottom + 15
$EditButton.OnClick = "EditListViewItem()"

$Form.Show()
$Form.SetFocus()

While $Form.Visible
   $Nul = Execute($System.DoEvents)
Loop
Exit 0


Function EditListViewItem()
   If $ListView.SelectedItems.Count < 1
      Exit 0
   Endif
   $EditForm = $System.Form()
   $EditForm.Width = 110*$ListView.Columns.Count
   $EditForm.Height = 140
   $EditForm.Center()

   ReDim $aSelected[$ListView.Columns.Count - 1]
   For Each $Item In $ListView.SelectedItems
      For $x = 0 to $ListView.Columns.Count - 1
         $aSelected[$x] = $Item.SubItems($x).Text
      Next
   Next

   For $x=0 to UBound($aSelected)
      $nul = Execute('$Box'+$x+' = $EditForm.Controls.Add("TextBox")')
      $nul = Execute('$Box'+$x+'.Text = $aSelected[$x]')
      If $x=0
         $nul = Execute('$Box'+$x+'.Left = 5')
      Else
         $nul = Execute('$Box'+$x+'.Left = $Box'+($x-1)+'.Right+5')
      Endif
      $nul = Execute('$Box'+$x+'.top = 20')
   Next

   $EditSaveButton = $EditForm.Controls.Add("ToolButton")
   $EditSaveButton.Text = "Save"
   $EditSaveButton.OnClick = "SaveListViewItem()"
   $EditSaveButton.Width = 80
   $EditSaveButton.Center()
   $EditSaveButton.Top = $Box0.Bottom+15

   $Form.Hide()
   $EditForm.Show()
   $EditForm.SetFocus()

   While $EditForm.Visible
      $Nul = Execute($EditForm.DoEvents)
   Loop
   $Form.Show()
   Exit 0
EndFunction

Function SaveListViewItem()
   For Each $Item In $ListView.SelectedItems
      For $x = 0 to $ListView.Columns.Count
         $nul = Execute('$info = $Box'+$x+'.Text')
         $Item.SubItems($x).Text = $info
      Next
   Next
   $EditForm.Hide()
EndFunction

Function PopulateListView()
   For $x=1 to $columns
      $nul = Execute('$Col'+$x+' = $Listview.Columns.Add("Col "+$x,$ListView.Width/$columns-1,2)')
   Next
   For $x=0 to 10
      $row = $ListView.Items.Insert($x)
      For $y=0 to $ListView.Columns.Count-1
         $row.SubItems($y).Text = "Test "+$x
      Next
   Next
   $ListView.SelectedItems = ""
EndFunction

Top
#203432 - 2011-11-02 02:49 AM Re: dynamically creating textboxes [Re: ShaneEP]
Dr_Rick Offline
Fresh Scripter

Registered: 2009-07-05
Posts: 38
Loc: Guam
Shane once again you have gone way above and beyond in your efforts to help me. My hats off to you and your quick response to my problem. I have successfully executed the code that you provided. When I evaluated it and tried to modify it to meet my script it produced an error when it tried to create the textbox. If you have a moment I have posted the code to the internet where you can perhaps assist me in determining the source of my error. I am new to the execute concept and I am sure that this is where the trouble arises. I added the debug command where I think it is failing. This script will be used to parse large csv log files that contain a variety of columns etc. Thanks again for your help in this matter. iReport II
_________________________
Dr Rick
Computer Specialist

Top
#203433 - 2011-11-02 02:56 AM Re: dynamically creating textboxes [Re: Dr_Rick]
Dr_Rick Offline
Fresh Scripter

Registered: 2009-07-05
Posts: 38
Loc: Guam
Shane
If the code is changed to $ListViewEx1.OnDoubleClick = "Open_Item()" I think you will see what it is I am trying to accomplish. The messagebox is going to be replaced with populating the form I just got frustrated with it not working and wanted to see where it was when I executed it. Thanks again.
_________________________
Dr Rick
Computer Specialist

Top
#203434 - 2011-11-02 04:06 AM Re: dynamically creating textboxes [Re: Dr_Rick]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
The Execute statements are tricky, but I use it whenever I need to create dynamic variables/objects. The problem may simply be with the setoption(). The way I have them written will only work if the novarsinstrings is turned on...
$nul = SetOption("NoVarsInStrings","On")

Top
#203435 - 2011-11-02 04:06 AM Re: dynamically creating textboxes [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
But as soon as I get a little more time, I will look at your uploaded code more thoroughly.
Top
#203436 - 2011-11-02 04:12 AM Re: dynamically creating textboxes [Re: ShaneEP]
Dr_Rick Offline
Fresh Scripter

Registered: 2009-07-05
Posts: 38
Loc: Guam
Thanks that makes sense I will go through my code and verify it using the no variables in strings statement. I would appreciate the look at my script when you get the time.
_________________________
Dr Rick
Computer Specialist

Top
#203437 - 2011-11-02 05:20 AM Re: dynamically creating textboxes [Re: Dr_Rick]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Here is your script with just a few minor changes, mostly to keep the console from ever opening, and with the novars option on. It took me forever to realize that there was a space in the "On" portion of the setoption that was killing me haha. But it seems to function now! Just needs the formatting done to match the parent form.

Attachments
iReport-II.kix (148 downloads)
Description:



Top
#203438 - 2011-11-02 05:26 AM Re: dynamically creating textboxes [Re: ShaneEP]
Dr_Rick Offline
Fresh Scripter

Registered: 2009-07-05
Posts: 38
Loc: Guam
Shane thanks again for your help. I have been working on your suggestion ever since you posted it and see that it was as you suspected with the novar section. I appreciate your assistance and I will now be able to focus more on the form and complete this project. I am anxious to review your changes in my script.
_________________________
Dr Rick
Computer Specialist

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.063 seconds in which 0.023 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org