我有一个困扰我很长时间的问题,但是我无法解决这个问题。
所以我有一个程序,其中包含8个选项卡,当我单击此页面上的按钮时,所有选项卡都使用相同的公式进行计算,但基本值取决于选择的抽动选项卡和单击的按钮
变量a1和a2是访问完全相同的数据库的相同值,就像b1&b2和c1&c2作为组合框一样
Private Sub Calculate() Handles button1.Click
value1 = 0.5
sum1 = a1.selectedValue*b1.SelectedValue*c1.SelectedValue*value1
textbox1.value = sum1.value
Private Sub Calculate2() Handles button2.Click
value2 = 0.55
sum2 = a1.selectedValue*b2.selectedValue*c2.selectedValue*value2
texbox2.value = sum2.value
是否有任何方法可以根据所关注的选项卡来实现价值并对其进行简化?喜欢
Private Sub Calculate(sender As Object, e As EventArgs) Handles button1.Click, button2.Click, button3.Click
if tabpage1 is focus then
value = 0.05
elseif tabpage2 is focus then
value = 0.55
end if
if a1 is selected then
a=a1.value
elseif a2.is selected then
a=a2.value
......
endif
.......
sum = a*b*c*value
if tabpage1 is focus then
textbox1.value = sum.value
else
....
endif
?? 我知道上面的代码远非“简单”,实际上每个子控件中的处理按钮可能都是hmmm ...更好?只是从其他角度来看,只需一个子页面即可完成这8个标签页
答案 0 :(得分:0)
您可以将方法Focused
或ActiveControl
或SelectedTab/Index
用作:
Private Sub Calculate(sender As Object, e As EventArgs) Handles button1.Click, button2.Click, button3.Click
If sender Is button1 Then '' Button1 is clicked
If TabControl1.SelectedTab Is tabpage1 Then
'' If tabpage1.Focused OrElse Me.ActiveControl Is tabpage1 Then '' Tabpage1 if focused Or Tabpage1 is Active Control
'' Your Code
End If
End If
If sender Is button2 Then '' Button2 is clicked
If TabControl1.SelectedTab Is tabpage2 Then
'' If tabpage2.Focused OrElse Me.ActiveControl Is tabpage2 Then '' Tabpage2 if focused or Tabpage2 is Active Control
'' Your Code
End If
End If
End Sub
答案 1 :(得分:0)
我建议最好的解决方案是这样的:
Dim selectedTab = TabControl1.SelectedTab
Dim tb = selectedTab.Controls.OfType(Of TextBox)().First()
Dim value = CInt(selectedTab.Tag)
'Use tb and value here.
您只需要将每个Tag
的{{1}}属性设置为适当的值,并且如果每页上还有其他TabPage
或位于其中,则可能需要调整第二行另一个容器。每个页面上显然有多个TextBoxes
,因此该部分可能看起来像这样:
ComboBoxes