Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
What's wrong with my class
Message
De
02/09/2005 05:21:00
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Conception classe
Titre:
What's wrong with my class
Divers
Thread ID:
01046121
Message ID:
01046121
Vues:
61
I am creating a class that displays student names on a contextmenu. The class exposes a funtion GetStudid, which displays the contextmenu and must return the corresponding studid of the selected student. The problem is I can't get the correct studid. What is returned is the studid of the student that was previously clicked.

I have atteched my code for clarity.
Public Class myStudent
    Public student_name, studid As String

    Private DS As New DataSet

    Sub New()
        ' POPULATE THE DATASET.  In my program, these data come from an SQL table
        DS.Tables.Add("students")
        DS.Tables("students").Columns.Add("studid")
        DS.Tables("students").Columns.Add("studname")

        Dim dt As DataRow

        dt = DS.Tables("students").NewRow
        dt.Item("studid") = "2001-00234"
        dt.Item("studname") = "DOE, JOHN"
        DS.Tables("students").Rows.Add(dt)

        dt = DS.Tables("students").NewRow
        dt.Item("studid") = "2002-00230"
        dt.Item("studname") = "DORRIS, PAUL"
        DS.Tables("students").Rows.Add(dt)

        dt = DS.Tables("students").NewRow
        dt.Item("studid") = "2002-00418"
        dt.Item("studname") = "DJANGO, MANDRID"
        DS.Tables("students").Rows.Add(dt)

        dt = DS.Tables("students").NewRow
        dt.Item("studid") = "2001-00994"
        dt.Item("studname") = "DARRYL, JOSEPH"
        DS.Tables("students").Rows.Add(dt)

    End Sub

    Public Function GetStudid(ByVal pTextBox As TextBox) As String
        'Shows a context menu and returns the STUDID of the selected student
        Dim cm As New ContextMenu
        Dim mnuItem As MenuItem
        For Each dr As DataRow In DS.Tables("students").Rows
            mnuItem = New MenuItem(dr.Item("studname"))
            AddHandler mnuItem.Click, AddressOf cmEventhandler
            cm.MenuItems.Add(mnuItem)
        Next
        cm.Show(pTextBox, New Point(0, 0))
    End Function

    Private Sub cmEventhandler(ByVal sender As Object, ByVal e As EventArgs)
        Dim cm_index = CType(sender, MenuItem).Index
        studid = DS.Tables("students").Rows(cm_index).Item("studid")
    End Sub
End Class

---------------------------------------------------------------------
This is the main form
---------------------------------------------------------------------

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Dim mystud As New myStudent
    'A Textbox and a Command Button was placed at design time

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mystud.GetStudid(TextBox1)

        'MsgBox("delay")

        MsgBox(mystud.studid)
    End Sub
End Class
But if I uncomment the line ['MsgBox("delay")], the code works fine.

Any help would be very much appreciated
OMAR C. IBRAHIM
----------
Programmer
STUDENT RECORDS & INFORMATION SYSTEM (SRIS)
Western Mindanao State University, Philippines
----------
Programmer
SSI Engineering & Architectural Designs and IT Center (SENARD ITC)
Zamboanga City, Philippines
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform