Subroutine 'sender' queries vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alcestis
    New Member
    • Aug 2007
    • 13

    Subroutine 'sender' queries vb.net

    I understand that if i call a subroutine from a control the subroutine refers to the control that called it using sender. What i don't understand however is how exactly to implement this...

    Say for instance i have the code below, which creates a dynamic textbox or a dynamic picturebox with the handler of check_click.

    Code:
    Public Sub doall_click(ByVal sender As Object, ByVal e As EventArgs)
     
    If Tool = "Text" Then
         'define varibles
                intelement = intelement + 1
                CursorOffset = Windows.Forms.Cursor.Position
                intheight = (Me.Height - dynpan(1).Height) / 2
                intwidth = (Me.Width - dynpan(1).Width) / 2
                sintxtsize = txtsize.Text
    
                'make space
                ReDim Preserve dynlabel(intelement)
    
                'create new label and define propeties
                Me.Controls.Add(dynlabel(intelement))
                dynlabel(intelement) = New TextBox
                dynlabel(intelement).Location = New Point((CursorOffset.X), (CursorOffset.Y))
                dynlabel(intelement).BorderStyle = BorderStyle.None
                dynlabel(intelement).BackColor = Pan2color.BackColor
                dynlabel(intelement).ForeColor = Pan1color.BackColor
                dynlabel(intelement).Parent = dynpan(intlayeron)
                dynlabel(intelement).Text = "TYPE HERE"
                dynlabel(intelement).Tag = intelement
    
                'add events
                AddHandler dynlabel(intelement).Click, AddressOf check_click
    
    
      If Tool = "Image" Then
    
                'define varibles
                intelement = intelement + 1
                CursorOffset = Windows.Forms.Cursor.Position
    
                'make space
                ReDim Preserve dynimage(intelement)
    
                'create new label and define propeties
                Me.Controls.Add(dynimage(intelement))
                dynimage(intelement) = New PictureBox
                dynimage(intelement).Location = New Point((CursorOffset.X - intwidth), (CursorOffset.Y - intheight))
    
                'add events
                AddHandler dynimage(intelement).Click, AddressOf check_click
    
            End If
    end sub
    and in check click i need to be be to gather information on the sender in order to resize and manipulate it, how would i go about it?

    Below is what i have in check_click...( not much)

    Code:
     
    Public Sub check_click(ByVal sender As Object, ByVal e As EventArgs)
    
    
            'Sender?
            
            If Tool = "Hand" Then
                ' ADD STUFF HERE YOU DIG ABOUT RESIZING AND MOVING
            End If
    
     end sub
  • Munawar
    New Member
    • May 2007
    • 12

    #2
    Hi,
    Here sender means the source of object (control from where event is called) .. its handy to get details of the caller ... like if the event is fired from dropdownlist .. then the sender is Dropdownlist .. you can get detailes about the caller on run time from Sender..
    e holds information about the event data.. like in case of Datagrid it provide information about the curent row you are going to edit or other operation.
    here is detailed example ..



    epecially check this code segment

    Code:
    protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
       DropDownList list = (DropDownList)sender;
    
       TableCell cell = list.Parent as TableCell;
       DataGridItem item = cell.Parent as DataGridItem;
    
       int index = item.ItemIndex;
       string content = item.Cells[0].Text;
    
       Response.Write(
             String.Format("Row {0} contains {1}", index, content)
          );
       
    }

    Thanks,
    Munawar

    Comment

    • Alcestis
      New Member
      • Aug 2007
      • 13

      #3
      Thank you Munawar. I'm looking at it now and it seems very helpful.

      Comment

      Working...