VSTO Excel, Save Combo Box Text, ComboBoxes Reset Upon Workbook Opening?
I have several ComboBoxes in an Excel VSTO Solution. The combos are windows forms combos.
The issue is that I'd like selected value in the 开发者_如何学编程combo to be repopulated when the user reopens the spreadsheet. Currently, I know that the values are saved with the spreadsheet, I know this I've ran into ClickOnce installer problems due to developing and testing on the same machine, and I can see the spreadsheet open, and the installer go to get the new version. During that time, I see the combo values I want.
However, once the customization installs and the spreadsheet initializes, the combos reset to default text values. I can think of a few ways to get around this, involving using cached data... Does anyone know if there is a simple way to keep the combos from resetting when the spreadsheet opens?
I don't know if this is necessarily the best solution, but this is what I ended up doing.
I first embedded my combos into cells.
Friend WithEvents cmbType
Private Sub EmbedCombo()
cmbType = Me.Controls.AddComboBox(Me.Range("A7"), "cmbType")
End Sub
Then I created a sub to write the value of the Combo Text, back into the cell that the control resided in.
Private Sub SaveComboText()
Me.Range("A7").Value = Me.cmbType.Text
End Sub
Finally a routine that writes the Cell Value back to the Combo.
Private Sub LoadComboText()
Me.cmbType.Text = Me.Range("A7").Value
End Sub
To make it all work, I use the WorkBoook BeforeSave Event, and the WorkSheet.StartUp Event.
First
Private Sub ThisWorkbook_BeforeSave() Handles Me.BeforeSave
Globals.Sheet1.StoreComboValues()
End Sub
Then
Private Sub Sheet1_Startup() Handles Me.Startup
Call LoadComboText()
End Sub
If someone else knows of a better way to avoid this, let me know:)
精彩评论