Just nu i M3-nätverket
Gå till innehåll

MSFlexGrid - redigera post med dubbelklick


Kristianstad

Rekommendera Poster

Vet någon hur jag gör för att man skall kunna ändra på ett fält i en MSFlexGrid?

Alltså, om man dubbelklickar på en viss ruta skall man kunna ändra det som står i rutan.

Tack och hej.

/ Kristoffer

Windows kunde inte hitta något tangentbord. Tryck F1 för att försöka igen eller F2 för att avbryta.

Länk till kommentar
Dela på andra webbplatser

Kristoffer,

 

Följande utdrag har jag i en textfil med namnet MSFlexGrid men källan är okända - troligtvis MSDN:

 

MSFlexGrid does not have a built-in cell editing capability, but it provides

the hooks to make it easy for you to add that capability programmatically.

The advantage of this approach is that you can tailor editing behavior to

your taste. The basic technique involves smoke and mirrors: the editing

occurs not in MSFlexGrid at all, but in a standard Textbox control that is

positioned precisely over the cell being edited.

In this example, we will give the user two ways to get into the edit mode,

either by double-clicking on a cell, or by simply starting to type in the

current cell. The following two routines implement this:

Private Sub MSFlexGrid1_DblClick()

GridEdit Asc(" ")

End Sub

Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)

GridEdit KeyAscii

End Sub

In each case we call a grid edit subroutine and pass it a keystroke. In the

case of double-clicking, we pass the space character as a flag. The GridEdit

routine initializes the edit box and moves it into position:

Sub GridEdit(KeyAscii As Integer)

'use correct font

Text1.FontName = MSFlexGrid1.FontName

Text1.FontSize = MSFlexGrid1.FontSize

Select Case KeyAscii

Case 0 To Asc(" ")

Text1 = MSFlexGrid1

Text1.SelStart = 1000

Case Else

Text1 = Chr(KeyAscii)

Text1.SelStart = 1

End Select

'position the edit box

Text1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left

Text1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top

Text1.Width = MSFlexGrid1.CellWidth

Text1.Height = MSFlexGrid1.CellHeight

Text1.Visible = True

Text1.SetFocus

End Sub

For demonstration purposes, the Case statement in the GridEdit routine shows

two different behaviors when entering the edit mode. In practice you would

probably only use one of them, or a different one of your own creation. If

the edit mode is entered by virtue of a double-click or a control key press,

we copy the contents of the grid cell to the exit box and place the cursor

at the end of the string. If the edit mode is entered by pressing a normal

key, we ignore the original cell contents and insert the pressed key into

the edit box. The positioning of the exit box could be done on one line with

the Move method. Here we have used four lines so that it reads more easily

in this article. Notice that MSFlexGrid conveniently gives us all the

coordinate information we need.

Next, we need a couple of routines that handle housekeeping when the user

moves to a different cell or moves focus back to the grid from another

control. The LeaveCell event is also the place where you would put any data

validation code that might be applicable.

Private Sub MSFlexGrid1_LeaveCell()

If Text1.Visible Then

MSFlexGrid1 = Text1

Text1.Visible = False

End If

End Sub

Private Sub MSFlexGrid1_GotFocus()

If Text1.Visible Then

MSFlexGrid1 = Text1

Text1.Visible = False

End If

End Sub

Next we place some navigation code in the KeyDown event of the edit box so

that, for instance, the user can leave the edit mode by pressing ESC, and

move to a different row by pressing an arrow key:

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode

Case vbKeyEscape

Text1.Visible = False

MSFlexGrid1.SetFocus

Case vbKeyReturn

MSFlexGrid1.SetFocus

Case vbKeyDown

MSFlexGrid1.SetFocus

DoEvents

If MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 Then

MSFlexGrid1.Row = MSFlexGrid1.Row + 1

End If

Case vbKeyUp

MSFlexGrid1.SetFocus

DoEvents

If MSFlexGrid1.Row > MSFlexGrid1.FixedRows Then

MSFlexGrid1.Row = MSFlexGrid1.Row - 1

End If

End Select

End Sub

Finally we need a line of code to suppress the Beep that occurs when ENTER

is pressed in a Textbox:

Private Sub Text1_KeyPress(KeyAscii As Integer)

'noise suppression

If KeyAscii = vbKeyReturn Then KeyAscii = 0

End Sub

In order for the edit box to merge seamlessly into the grid, you need to set

several Textbox properties at design-time: set Appearance = 0 (flat), and

BorderStyle = 0 (none). Also set Visible = False so that the edit box is not

initially visible. To really fine-tune this code, the edit box needs a

slight additional offset to the southeast (with a corresponding reduction in

size) so that the text in it lines up exactly with the text in the cell

beneath. You would probably also want to write some code behind the scroll

event of the grid since clicking on the grid's scroll bar will not cause the

edit box to loose focus.

Note that this technique is not limited to using a Textbox as your edit box.

You could modify the sample code to use a ComboBox, a CheckBox, or even a

calendar control for editing, based on the column being edited.

MSFlexGrid is a very flexible control indeed, and this article just touches

on some of the things you can do with it. As you gain familiarity with it,

it will become a more regular part of your toolbox. Cell merging and

pivoting are two more unique features of the MSFlexGrid that give it

tremendous power and bear investigation.

 

Mvh

Dennis

Besök Sveriges ledande oberoende webbplats om MS Excel: http://www.xldennis.com

 

Länk till kommentar
Dela på andra webbplatser

Tack! Skall testa det när jag har mindre att göra...

 

Ha en trevlig kväll.

/ Kristoffer

Windows kunde inte hitta något tangentbord. Tryck F1 för att försöka igen eller F2 för att avbryta.

 

Tillägg:

Tyvärr kan jag inte ge dig poäng, det är något strul med Eforum idag. (//eforum.idg.se/viewmsg.asp?EntriesId=344966#345282)

[inlägget ändrat 2002-10-12 22:13:10 av Kristianstad]

Länk till kommentar
Dela på andra webbplatser

Arkiverat

Det här ämnet är nu arkiverat och är stängt för ytterligare svar.

×
×
  • Skapa nytt...