Calling Functions with parameters
I am wanting to create a function that will add a textbox or label to a form, my problem is that I need my user to be able to access all of the properties of that object.
Is there any way to give the calling program access to those properties without having to list every single one of them?
Thanks,
Squirrelly1
Re: Calling Functions with parameters
Hi,
Something like;
Dim txtTextbox As New textBox (form scope at least)
Calling event
txtTextBox = AddtextBox(txtTextBox)
Me.Controls.Add(txtTextBox)
Private Function AddtextBox(ByVal txtTemp As TextBox) As TextBox
txtTemp.Name = "TestText1"
txtTemp.Width = 40
txtTemp.Top = 15
txtTemp.Left = 24
txtTemp.Text = "New text Box"
txtTextBox.Visible = True
Return txtTemp
End Function
Re: Calling Functions with parameters
HI,
I may have misunderstood you. If you want the user to have access to the properties, you must list them somewhere. Otherwise, how is the user going to select the property wants to access? If you mean you want to create several TextBoxes/labels and don't want to list all the properties every time in your code, then yes you can.
Perhaps you can elaborate a little.
Re: Calling Functions with parameters
well, i wanted the calling event to be able to set the parameters...
but I guess that would defeat the purpose of having a seperate function to add the textbox, huh?
ok... well nvm on this one I guess
I'll just have to list just the parameters that I want to use in the function and live with it :p
Thanks,
Squirrelly1
Re: Calling Functions with parameters
Hi,
The calling event CAN set the parameters and pass them to the function.
The purpose of having a function would be if you wanted to use it from other forms - make it Public and preferably put it in the Module.
Re: Calling Functions with parameters
Actually I'm going to put it into a DLL so that I only have to change one file when I want to make changes to the user interface. :D :D :D I doubt I'm the first one who thought of this, but I'm still pretty proud of it. LOL
Re: Calling Functions with parameters
You can set and get all of the properties via Reflection.
VB Code:
'setting
Dim txt As New TextBox
txt.GetType.GetProperty("Text").SetValue(txt, "I set this via reflection by name!", Nothing)
MsgBox(txt.Text)
'getting
Dim txt As New TextBox
For Each pi As Reflection.PropertyInfo In txt.GetType.GetProperties()
MsgBox(pi.Name)
Next
Re: Calling Functions with parameters
if I were to do all that, why wouldn't I just go
txt.Text = "I set this with a lot less code!"
???
Squirrelly1
Re: Calling Functions with parameters
I don't know why you originally said "I need my user to be able to access all of the properties of that object" but that is how you can do it. The code you wrote couldn't be done at runtime whereas the property name and value in the reflection example can be passed in as text at anytime. I just wrote the example to show you the basic code of how to use reflection to get or set properties at runtime. Also reflection doesn't care what typeof object you pass it as long as you use correct property names.
So you could script or have the user type in a property name and value at runtime and have it set or retrieved at runtime on any object. There is also the option of the PropertyGrid control which you could use to get/set properties of an object.
I have an example related to Reflection that demonstrates what I mean but it also has other junk and I'm afraid it is not well commented. Let me know if you want me to post it.
Re: Calling Functions with parameters
This isn't exactly what I was talking about, but it seems like it could be very useful. Could you post that example for me?
Thanks,
Squirrelly1
1 Attachment(s)
Re: Calling Functions with parameters
Here you go. Select a control from the left then the 'Refresh' link on the right. That will fill the treeview with all the properties of the last selected control. Then just pick a property and change it using the set/get links on the bottom right. Some properties can not be sent because I didn't work out how to translate the more complex types from a string.
Re: Calling Functions with parameters
thanks a mil.. i'll have to take a look at this when I have time
Later
Re: Calling Functions with parameters
Actually, I downloaded the file before I posted that I would take a look when I had time, and I happen to be working and unable to devote the attention required to learn from this example right now.
erm... joke?
Re: Calling Functions with parameters
Quote:
Originally Posted by squirrelly1
Actually, I downloaded the file before I posted that I would take a look when I had time, and I happen to be working and unable to devote the attention required to learn from this example right now.
erm... joke?
I don't think he's being offensive. English is not his native language. I have had one or two misunderstandings with his meanings but he really sounds friendly :wave:
1 Attachment(s)
Re: Calling Functions with parameters
Since your wanting to post screen snapshots... take a look at this one and call me liar... It is not my fault if the system is messed up... it was still showing 0 views when you posted your little comment.
Re: Calling Functions with parameters
Hi,
I think there is a genuine misunderstanding here. Kulrom, you are assuming that the forum clock etc is infalible. It is not as I have noticed occasions when the time goes haywire (i.e. gets confused). I've seen posts timed earlier that the previous post by several hours. Squirrelly1 has no reason to make a false claim on this. It would be better to accept that it is a system fault rather than a personal one.
It's a bit like line calls in tennis. We all call what we believe we see but often see calls differently. It's not that someone is dishonest but the way the human brain and eyes work.
Can we call a let please?
Re: Calling Functions with parameters
Fine by me... As far as I'm concerned, Thread Closed
Re: Calling Functions with parameters
Hey not to beat a dead horse or anything but the File Create and Modify dates are the times that I made the zip otherwise it would be before even my post.
Anyway I don't want to get you started again, I just had to throw that in there and definately the forum clock has some issues from time to time.
1 Attachment(s)
Re: Calling Functions with parameters
Teeheehee... Sry guys, I've been at my gf's all weekend. Erm? Maybe I misunderstood you, which is completely likely, but did you say that the created and modified date/time for a file that was downloaded onto my computer is based upon the date/time that you created the zip file on your computer??? I thought that sounded a little funny so I checked it out... here you go...
I promise to leave the damned horse alone now... :D
Squirrelly1