473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Functions within Button_Click

I am trying to call a recursive subroutine within a
Button_Click event. How can I do this?

I tried to create a seperate Function() but it will not
allow me to use any variables that I created in the
Button_Click event.

Any ideas?

Thanks!

Nov 20 '05 #1
13 1952
In article <94**********************************@microsoft.co m>,
mr********@hormel.com says...
I am trying to call a recursive subroutine within a
Button_Click event. How can I do this?

I tried to create a seperate Function() but it will not
allow me to use any variables that I created in the
Button_Click event.


Can you pass the variables to the recursive function? The GOSUB command
was not implemented in VB.NET so you can't use that. If there's too
many variables to pass, you could try placing them in a structure.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #2
Matt,
Is this a homework assignment?
I tried to create a seperate Function() but it will not
allow me to use any variables that I created in the
Button_Click event. You would pass "Variables" to the function as parameters, just like any
other function or sub. Alternatively you can declare the "variables" within
the Class itself.

Have you tried something like:

Private Sub Button_Click(...)
Dim x As Integer = 1000
Dim y As String
MyFunction(x, y)
End Sub

Private Function MyFunction(ByVal x As Integer, ByVal y As String)
If x <= 0 Then Exit Function
MyFunction(x - 1, y)
End Function

Hope this helps
Jay

"Matt" <mr********@hormel.com> wrote in message
news:94**********************************@microsof t.com... I am trying to call a recursive subroutine within a
Button_Click event. How can I do this?

I tried to create a seperate Function() but it will not
allow me to use any variables that I created in the
Button_Click event.

Any ideas?

Thanks!

Nov 20 '05 #3
"Matt" <mr********@hormel.com> wrote...
I am trying to call a recursive subroutine within a
Button_Click event. How can I do this?

I tried to create a seperate Function() but it will not
allow me to use any variables that I created in the
Button_Click event.


Matt: A word is worth 1/1000th of a picture :-) Could you post an example?
It should make it easier to come up with a solution.

Tom
Nov 20 '05 #4
Public Sub B_Lookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_Lookup.Clic
Dim objUser, objDomain, objShell, objRootDSE, objTrans, objGroupList, objGrou
Dim strUserDN, strDNSDomain, strNetBIOSDomain, strUserNTNam
Dim strText, intConstants, intAns, strtitl
Dim firstname, lastname As Strin
' Constants for the NameTranslate object
Const ADS_NAME_INITTYPE_DOMAIN =
Const ADS_NAME_TYPE_NT4 =
Const ADS_NAME_TYPE_1179 =

objShell = CreateObject("Wscript.Shell"

' Request user sAMAccountName
strtitle = "User Admin Console
strUserNTName = Txt_Input_Username.Tex
Txt_Input_Username.Text = "
If strUserNTName = "" The
L_Result.Text = "Please Enter a Username
Txt_Input_Username.Focus(
Exit Su
End I

' Retrieve DNS domain name
objRootDSE = GetObject("LDAP://RootDSE"
strDNSDomain = objRootDSE.Get("defaultNamingContext"

' Convert DNS domain name to NetBIOS domain name
objTrans = CreateObject("NameTranslate"
objTrans.Init(ADS_NAME_TYPE_NT4, strDNSDomain
objTrans.Set(ADS_NAME_TYPE_1179, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4
' Remove trailing backslash
strNetBIOSDomain = Microsoft.VisualBasic.Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1

' Convert user NT name to Distinguished Name
objTrans.Init(ADS_NAME_INITTYPE_DOMAIN, strNetBIOSDomain
On Error Resume Nex
Err.Clear(
objTrans.Set(ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strUserNTName
If Err.Number <> 0 The
Err.Clear(
L_Result.Text = "No User Found
Txt_Firstname.Text = "
Txt_Initial.Text = "
Txt_Lastname.Text = "
txt_Location.Text = "
Txt_Input_Username.Focus(
Exit Su
End I
On Error GoTo
strUserDN = objTrans.Get(ADS_NAME_TYPE_1179

' Bind to user object
On Error Resume Nex
Err.Clear(
objUser = GetObject("LDAP://" & strUserDN
If Err.Number <> 0 The
Err.Clear(
Txt_Input_Username.Text = "User does not exist
Txt_Firstname.Text = "
Txt_Initial.Text = "
Txt_Lastname.Text = "
txt_Location.Text = "
Txt_Input_Username.Focus(
Exit Su
End I
On Error GoTo

' Bind to domain
objDomain = GetObject("LDAP://" & strDNSDomain
L_Result.Text = "User Found!
Txt_Firstname.Text = objUser.Get("givenname"
Txt_Initial.Text = objUser.Get("initials"
Txt_Lastname.Text = objUser.Get("sn"
txt_Location.Text = objUser.Get("description"

**This is where I want to call a seperate subroutine called GroupEnum (the code for this group is below)**
Call GroupEnum(objUser

B_Return_HFC_Main.Focus(

End Su

Sub GroupEnum(objADObject
' Recursive subroutine to enumerate user group memberships. Includes nested group memberships
Dim colstrGroups, objGroup,
objGroupList.CompareMode = vbTextCompar
colstrGroups = objADObject.memberO
If IsEmpty(colstrGroups) The
Exit Su
End I
If TypeName(colstrGroups) = "String" The
Set objGroup = GetObject("LDAP://" & colstrGroups
If Not objGroupList.Exists(objGroup.sAMAccountName) The
objGroupList(objGroup.sAMAccountName) = Tru
Wscript.Echo objGroup.distinguishedNam
Call GroupEnum(objGroup
End I
Set objGroup = Nothin
Exit Su
End I
For j = 0 To UBound(colstrGroups
Set objGroup = GetObject("LDAP://" & colstrGroups(j)
If Not objGroupList.Exists(objGroup.sAMAccountName) The
objGroupList(objGroup.sAMAccountName) = Tru
Wscript.Echo objGroup.distinguishedNam
Call GroupEnum(objGroup
End I
Nex
Set objGroup = Nothin
End Su
***This script works if I write just a vbscript. But when I try to put it into VB.NET it doesn't...**

Thanks for your help

Nov 20 '05 #5
No this is not a homework assignment. I am a systems admin trying to set up a User Admin Console which links into active directory. I am trying to list out all the groups a user is a member of

I posted code right above this post as a reply. Maybe this will show you more of what I am trying to do
Nov 20 '05 #6
Matt,
Yes I saw both of your postings, and I retract the question. I hope you
understand & appreciate why I asked the question! :-)

Looking at your code, either or both of my suggestions should work, with
effort.

Either make some of these parameters to GroupEnum, or move then outside of
B_Lookup_Click (class level):

Dim objUser, objDomain, objShell, objRootDSE, objTrans, objGroupList,
objGroup

I say "with effort" as when I cut & pasted the code into VS.NET, IsEmpty &
WScript came up undefined...

Hope this helps
Jay

"Matt" <an*******@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
No this is not a homework assignment. I am a systems admin trying to set up a User Admin Console which links into active directory. I am trying to
list out all the groups a user is a member of.
I posted code right above this post as a reply. Maybe this will show you

more of what I am trying to do
Nov 20 '05 #7
"Matt" <mr********@hormel.com> wrote...
Public Sub B_Lookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_Lookup.Click Dim objUser, objDomain, objShell, objRootDSE, objTrans, objGroupList, objGroup Dim strUserDN, strDNSDomain, strNetBIOSDomain, strUserNTName
Dim strText, intConstants, intAns, strtitle
Dim firstname, lastname As String


Sorry Matt :-) Perhaps somebody else can wade their way through this. I
thought you were having a problem with recursion in VB.Net. But you've
posted a complex example that isn't in VB.Net.

Nov 20 '05 #8
Cor
Hi Tom,

Matt is redirected by Alex K. Angelopoulos[MVP] to this newsgroup telling
that his code was VB.net so do not blame him.

I asked in a previous thread if he was using VB.net or VS.studio.net to know
what the best way was to help him, but I saw Jay B has already pasted it in
his IDE so he has an answer from Jay B.

If he is not using VB.net we can always try it with all options off.

Cor
Nov 20 '05 #9
Tom,
It "is" VB.NET! ;-) As it contains the Handles keyword...

However! I believe it was cut & pasted from VBScript, hence the lack of
types on the variables.

As Cor suggested, you need to use Option Strict Off & Option Explicit Off,
and you still have a few errors you can "clean up" and a few errors that I'm
not sure what to do with.

I really don't know enough about LDAP to help, plus I don't have immediate
access to an LDAP server...

I think I will suggest he consider using the classes in the
System.DirectoryServices namespace, rather then attempt to converting the
VBScript code...

Just a thought
Jay

"Tom Leylan" <ge*@iamtiredofspam.com> wrote in message
news:OP**************@tk2msftngp13.phx.gbl...
"Matt" <mr********@hormel.com> wrote...
Public Sub B_Lookup_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles B_Lookup.Click
Dim objUser, objDomain, objShell, objRootDSE, objTrans,

objGroupList, objGroup
Dim strUserDN, strDNSDomain, strNetBIOSDomain, strUserNTName
Dim strText, intConstants, intAns, strtitle
Dim firstname, lastname As String


Sorry Matt :-) Perhaps somebody else can wade their way through this. I
thought you were having a problem with recursion in VB.Net. But you've
posted a complex example that isn't in VB.Net.

Nov 20 '05 #10
Matt,
Thinking about this, and reviewing the other comments in this thread.

Have you looked at using classes in the System.DirectoryServices namespace
to do your Active Directory searches?

http://msdn.microsoft.com/library/de...ryServices.asp

Contains the classes in the namespace as well as links to "how to" articles.

Hope this helps
Jay
"Matt" <an*******@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
No this is not a homework assignment. I am a systems admin trying to set up a User Admin Console which links into active directory. I am trying to
list out all the groups a user is a member of.
I posted code right above this post as a reply. Maybe this will show you

more of what I am trying to do
Nov 20 '05 #11
Jay,

Thanks for your advice.

I am pretty new to VB.NET. How do I utilize the System.DirectoryServices Namespace?

Thanks again

Matt
Nov 20 '05 #12
Matt,
The page I gave you has a link to classes you need, predominately it is the
DirectoryEntry & DirectorySearcher classes. Normally when you review those
class topics on MSDN, they contain samples on how to use them... There are
links to the DirectoryEntry & DirectorySearcher topics on the page I gave
you.

Also in about the middle of the page is a link to "Introduction to Active
Directory Objects", which brings you to the section of MSDN that explains at
a slightly higher level.

I would recommend you read through the following section (which contains the
above "Introduction to Active Directory Objects"):

http://msdn.microsoft.com/library/de...oryObjects.asp

It also contains a "Searching Active Directory Hierarchies" topic that
should be what you need...

Hope this helps
Jay

"Matt" <an*******@discussions.microsoft.com> wrote in message
news:03**********************************@microsof t.com...
Jay,

Thanks for your advice.

I am pretty new to VB.NET. How do I utilize the System.DirectoryServices Namespace?
Thanks again

Matt

Nov 20 '05 #13
Thanks so much for all your help Jay! You have been a huge help
Nov 20 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul Aspinall | last post by:
Hi I have a form with a button on it. It seems that when I click the button, it causes Form_load to fire, before the Button_Click procedure Does anyone know why?? Thanks
5
by: Darren Smith | last post by:
Hi There, I have a shopping cart app that displays products along with a textbox (to enter quantity) and an image button to add the item to the shopping cart. Please explain why my below...
3
by: I am Sam | last post by:
Please help. I'm dying here and pulling out the last few remaining elements of my hair over this. I have built a form that will identify and authenticate users. I keep getting the following...
9
by: gaddoznntp | last post by:
Hi all, I've a server button handling a button_click procedure. If the client user clicks the button, the page causes the postback and the button_click procedure is fired. Then, if the user...
3
by: charles | last post by:
Hi: In my page I need to re-start a thread. I put the code into a Button_Click(sender, e) handler. When I click the button, it works. Then I call this handler inside a timer's handler...
1
by: Gary | last post by:
Hello, I am currently new to .NET, as you can probably tell by my last two or three threads!!! Anyway, I am using code behind as much as possible - and am curious about the way in which...
8
by: Gary | last post by:
Hello, I am currently new to .NET, as you can probably tell by my last two or three threads!!! Anyway, I am using code behind as much as possible - and am curious about the way in which...
0
by: John | last post by:
I am loading my controls dynamically into my asp.net page. When I click on a button on one of those user controls then page reloads and I need to reload the page based on what has happened in the...
4
by: =?Utf-8?B?U3ZlbiBXLg==?= | last post by:
Hi Newsgroup, I am developing a C# Windows Forms application that launches processes within a background worker.. The processes seem to have a memory limit of about 278mb. Some proccesses...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.