Dr_Rick
(Fresh Scripter)
2011-11-01 03:00 AM
dynamically creating textboxes

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


Dr_Rick
(Fresh Scripter)
2011-11-01 06:09 PM
Re: dynamically creating textboxes

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.

ShaneEP
(MM club member)
2011-11-01 10:04 PM
Re: dynamically creating textboxes

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?


Dr_Rick
(Fresh Scripter)
2011-11-01 10:23 PM
Re: dynamically creating textboxes

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 


ShaneEP
(MM club member)
2011-11-01 11:05 PM
Re: dynamically creating textboxes

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


Dr_Rick
(Fresh Scripter)
2011-11-02 02:49 AM
Re: dynamically creating textboxes

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
(Fresh Scripter)
2011-11-02 02:56 AM
Re: dynamically creating textboxes

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.


ShaneEP
(MM club member)
2011-11-02 04:06 AM
Re: dynamically creating textboxes

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")


ShaneEP
(MM club member)
2011-11-02 04:06 AM
Re: dynamically creating textboxes

But as soon as I get a little more time, I will look at your uploaded code more thoroughly.

Dr_Rick
(Fresh Scripter)
2011-11-02 04:12 AM
Re: dynamically creating textboxes

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.

ShaneEP
(MM club member)
2011-11-02 05:20 AM
Re: dynamically creating textboxes

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.

Dr_Rick
(Fresh Scripter)
2011-11-02 05:26 AM
Re: dynamically creating textboxes

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.