跨子程序传输字符串值
Transferring string values across Subroutines
我有变量 Month1 和 Month2,我需要以某种方式从 cb_CheckedChanged 转移到 frmMain_Load。如果没有任何方法可以传输 String 值,我是否可以更改一些代码以使其成为可能?
Public Class frmMain
Private Sub cb_CheckedChanged(sender As Object, e As EventArgs) Handles chckJan.
CheckedChanged,
chckFeb.CheckedChanged,
chckMar.CheckedChanged,
chckApr.CheckedChanged,
chckMay.CheckedChanged,
chckJun.CheckedChanged,
chckJul.CheckedChanged,
chckAug.CheckedChanged,
chckOct.CheckedChanged,
chckNov.CheckedChanged,
chckDec.CheckedChanged
'get all checkboxes
Dim Months = Controls.OfType(Of CheckBox)().ToArray()
'Get the number of checked CheckBoxes.
Dim checkedBoxCount = Months.Count(Function(cb) cb.Checked)
'Unchecked CheckBoxes should be enabled if and only if the number of checked CheckBoxes is less than the maximum number allowed.
Dim enableUncheckedBoxes = checkedBoxCount < 2
'Get the unchecked CheckBoxes.
Dim uncheckedBoxes = Months.Where(Function(cb) Not cb.Checked)
'Enable or disable the unchecked CheckBoxes as appropriate.
For Each uncheckedBox In uncheckedBoxes
uncheckedBox.Enabled = enableUncheckedBoxes
Next
Dim Month1 As String
Dim Month2 As String
Dim CheckBoxArray1 As CheckBox() = {chckJan, chckFeb, chckMar, chckApr, chckMay, chckJun, chckJul, chckAug, chckSep, chckOct, chckNov, chckDec}
For Each CheckBox In CheckBoxArray1
If CheckBox.Checked = True And checkedBoxCount = 1 Then
Month1 = CheckBox.Name.ToString
End If
Next
Dim CheckBoxArray2 As CheckBox() = {chckJan, chckFeb, chckMar, chckApr, chckMay, chckJun, chckJul, chckAug, chckSep, chckOct, chckNov, chckDec}
For Each CheckBox In CheckBoxArray2
If CheckBox.Checked = True And checkedBoxCount = 2 Then
Month2 = CheckBox.Name.ToString
End If
Next
End Sub
Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
真的不确定你的最终目标是什么,但看起来你需要这样的东西:
Public Class frmMain
Private Month1 As String
Private Month2 As String
Private Sub cb_CheckedChanged(sender As Object, e As EventArgs) Handles _
chckJan.CheckedChanged, chckFeb.CheckedChanged, chckMar.CheckedChanged,
chckApr.CheckedChanged, chckMay.CheckedChanged, chckJun.CheckedChanged,
chckJul.CheckedChanged, chckAug.CheckedChanged, chckSep.CheckedChanged,
chckOct.CheckedChanged, chckNov.CheckedChanged, chckDec.CheckedChanged
Dim CheckBoxes As CheckBox() = {
chckJan, chckFeb, chckMar, chckApr,
chckMay, chckJun, chckJul, chckAug,
chckSep, chckOct, chckNov, chckDec}
Dim checkedBoxes = CheckBoxes.Where(Function(cb) cb.Checked)
For Each curCB In CheckBoxes.Where(Function(cb) Not cb.Checked)
curCB.Enabled = (checkedBoxes.Count < 2)
Next
If checkedBoxes.Count = 2 Then
Month1 = checkedBoxes.First.Text
Month2 = checkedBoxes.Last.Text
End If
End Sub
End Class
我有变量 Month1 和 Month2,我需要以某种方式从 cb_CheckedChanged 转移到 frmMain_Load。如果没有任何方法可以传输 String 值,我是否可以更改一些代码以使其成为可能?
Public Class frmMain
Private Sub cb_CheckedChanged(sender As Object, e As EventArgs) Handles chckJan.
CheckedChanged,
chckFeb.CheckedChanged,
chckMar.CheckedChanged,
chckApr.CheckedChanged,
chckMay.CheckedChanged,
chckJun.CheckedChanged,
chckJul.CheckedChanged,
chckAug.CheckedChanged,
chckOct.CheckedChanged,
chckNov.CheckedChanged,
chckDec.CheckedChanged
'get all checkboxes
Dim Months = Controls.OfType(Of CheckBox)().ToArray()
'Get the number of checked CheckBoxes.
Dim checkedBoxCount = Months.Count(Function(cb) cb.Checked)
'Unchecked CheckBoxes should be enabled if and only if the number of checked CheckBoxes is less than the maximum number allowed.
Dim enableUncheckedBoxes = checkedBoxCount < 2
'Get the unchecked CheckBoxes.
Dim uncheckedBoxes = Months.Where(Function(cb) Not cb.Checked)
'Enable or disable the unchecked CheckBoxes as appropriate.
For Each uncheckedBox In uncheckedBoxes
uncheckedBox.Enabled = enableUncheckedBoxes
Next
Dim Month1 As String
Dim Month2 As String
Dim CheckBoxArray1 As CheckBox() = {chckJan, chckFeb, chckMar, chckApr, chckMay, chckJun, chckJul, chckAug, chckSep, chckOct, chckNov, chckDec}
For Each CheckBox In CheckBoxArray1
If CheckBox.Checked = True And checkedBoxCount = 1 Then
Month1 = CheckBox.Name.ToString
End If
Next
Dim CheckBoxArray2 As CheckBox() = {chckJan, chckFeb, chckMar, chckApr, chckMay, chckJun, chckJul, chckAug, chckSep, chckOct, chckNov, chckDec}
For Each CheckBox In CheckBoxArray2
If CheckBox.Checked = True And checkedBoxCount = 2 Then
Month2 = CheckBox.Name.ToString
End If
Next
End Sub
Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
真的不确定你的最终目标是什么,但看起来你需要这样的东西:
Public Class frmMain
Private Month1 As String
Private Month2 As String
Private Sub cb_CheckedChanged(sender As Object, e As EventArgs) Handles _
chckJan.CheckedChanged, chckFeb.CheckedChanged, chckMar.CheckedChanged,
chckApr.CheckedChanged, chckMay.CheckedChanged, chckJun.CheckedChanged,
chckJul.CheckedChanged, chckAug.CheckedChanged, chckSep.CheckedChanged,
chckOct.CheckedChanged, chckNov.CheckedChanged, chckDec.CheckedChanged
Dim CheckBoxes As CheckBox() = {
chckJan, chckFeb, chckMar, chckApr,
chckMay, chckJun, chckJul, chckAug,
chckSep, chckOct, chckNov, chckDec}
Dim checkedBoxes = CheckBoxes.Where(Function(cb) cb.Checked)
For Each curCB In CheckBoxes.Where(Function(cb) Not cb.Checked)
curCB.Enabled = (checkedBoxes.Count < 2)
Next
If checkedBoxes.Count = 2 Then
Month1 = checkedBoxes.First.Text
Month2 = checkedBoxes.Last.Text
End If
End Sub
End Class