在子例程之间传输字符串值

时间:2015-01-04 20:59:06

标签: vb.net byref byval

我有变量Month1&我需要以某种方式从cb_CheckedChanged转移到frmMain_Load的Month2。如果没有任何方式传递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

1 个答案:

答案 0 :(得分:1)

真的不确定你的目标是什么,但似乎你需要这样的东西:

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