How to "short-circuit" an event?
Quote:
> So what you want is the ability to reject the change if you don't like
> what the user typed. Seems to me that you could save the old contents
> of the cell in a property of the window then on the CellAction event
> see if you approve the change. If you do, copy the cell contents to
> the "old value" property. If you don't like the change, copy the "old
> value" back into the cell and the user's change disappears.
> Steve
Yes. That's what I done (not exactly, but what I've done is similar). But it
The listbox hold 3 columns. The 2nd and the 3rd are editable (number 1 and
2, so).
In the "Open" event of the listbox:
listBox1.columnType(1)=3 //make the cells of the 2nd column editable
listBox1.columnType(2)=3 //make the cells of the 3rd column editable
erreur=false //see after...
In the "CellLostFocus" event of the listbox (as soon as the user clicks
elsewer):
if len(listBox1.cell(row,column))<>4 then //if the text isn't 4
caracters in lenght...
beep
erreur=true //there is an error with the cell which lost the focus...
colonne=column //write the number of the column of the cell which lost
the focus in the property "colonne"
ligne=row //write the number of the row of the cell which lost the focus
in the property "ligne"
end if
Now, in the "change" event (I want the selected cell to be the one which
previously lost the focus):
if erreur=true then //to know if the text of the previously selected
cell had a length different from 4 caracters
listBox1.editCell(ligne,colonne) //select the previously selected cell
listBox1.activeCell.selstart=len(listBox1.activeCell.text) //put the
cursor to the end of the text
erreur=false //reinitialisation of "erreur"
end if
Try this code. There is a problem: when the user clicks outside of the cell
he was editing and if the length of text of this cell is different from 4
caracters, he can hear the beep, but then the cell takes the text of the
cell where he clicked (that seems complicated, but try it, you'll understand
immediatly).
So I modified my code:
In "CellLostFocus":
if len(listBox1.cell(row,column))<>4 then
beep
erreur=true
if column=1 then
typetext=listBox1.cell(row,column) //Copy the text of the cell which
lost the focus if the cell was in the 2nd column in the property "typetext".
elseif column=2 then
creatortext=listBox1.cell(row,column) //Copy the text of the cell
which lost the focus if the cell was in the 3rd column in the property
"creatortext".
end if
colonne=column
ligne=row
listBox1.columnType(1)=1 //makes all the cells of the 2nd column
uneditable (temporarily) in order to solve the problem cited
listBox1.columnType(2)=1 //ditto for the 3rd column
end if
In "Change":
if erreur=true then
listBox1.columnType(1)=1
listBox1.columnType(2)=1
listBox1.editCell(ligne,colonne)
if colonne=1 then
listBox1.Cell(ligne,colonne)=typetext //Paste the text of the cell
which lost the focus if the cell is in the 2nd column.
elseif colonne=2 then
listBox1.Cell(ligne,colonne)=creatortext //Paste the text of the cell
which lost the focus if the cell in in the 3rd column.
end if
listBox1.activeCell.selstart=len(listBox1.activeCell.text)
erreur=false
listBox1.columnType(1)=3//makes all the cells of the 2nd column editable
again
listBox1.columnType(2)=3 //ditto for the 3rd column
end if
That works sometimes and sometimes not. In fact, the text of the cell which
lost the focus becomes the text of the one where the user clicked only if
the latter has been previously edited. Moreover, in this case, the cursor
becomes the cursor of text edition when it pass over a cell different from
the one the user is editing and if this one has been already edited.
But I don't understand why... Maybe it's a bug.
It could seem difficult to understand (because I don't speak English very
well), but try it, you'll understand immediatly.