VSTO Excel,保存组合框文本,打开工作簿时重置组合框?

发布于 2024-12-10 12:40:57 字数 293 浏览 0 评论 0原文

我的 Excel VSTO 解决方案中有几个组合框。这些组合是窗口形式的组合。

问题是我希望当用户重新打开电子表格时重新填充组合中的选定值。目前,我知道这些值是与电子表格一起保存的,我知道由于在同一台计算机上进行开发和测试,我遇到了 ClickOnce 安装程序问题,我可以看到电子表格打开,安装程序去获取新的版本。在那段时间里,我看到了我想要的组合值。

但是,一旦安装了自定义并且电子表格初始化,组合将重置为默认文本值。我可以想出几种方法来解决这个问题,包括使用缓存数据...有谁知道是否有一种简单的方法可以防止组合在电子表格打开时重置?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-12-17 12:40:57

我不知道这是否一定是最好的解决方案,但这就是我最终所做的。

我首先将我的组合嵌入到单元格中。

Friend WithEvents cmbType

Private Sub EmbedCombo()
   cmbType = Me.Controls.AddComboBox(Me.Range("A7"), "cmbType")
End Sub

然后我创建了一个子程序来将组合文本的值写回到控件所在的单元格中。

Private Sub SaveComboText()
   Me.Range("A7").Value = Me.cmbType.Text
End Sub

最后一个例程将单元格值写回到组合中。

Private Sub LoadComboText()
   Me.cmbType.Text = Me.Range("A7").Value  
End Sub

为了使其全部正常工作,我使用了 WorkBoook BeforeSave 事件和 WorkSheet.StartUp 事件。

首先

Private Sub ThisWorkbook_BeforeSave() Handles Me.BeforeSave
    Globals.Sheet1.StoreComboValues()
End Sub

然后

Private Sub Sheet1_Startup() Handles Me.Startup
  Call LoadComboText()
End Sub

如果其他人知道避免这种情况的更好方法,请告诉我:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文