|
Sorry about the clearness of the question
what i want to do on the fly is build up some buttons, but apply and addhandler to a specific function
for example
button1 will execute code at function1
button2 will execute code at function2
button3 will execute code at function1
but the names and function names will be determined at runtime.
I hope this is more clear for you and thanks for helping
|
|
|
|
|
I think I get the idea. How about this. Create a class called, say, MyButton like so ,
Private Class MyButton
Private strName As String
Private strFunction As String
Public Property Name() As String
Get
Return strName
End Get
Set(ByVal value As String)
strName = value
End Set
End Property
Public Property MyFunction() As String
Get
Return strFunction
End Get
Set(ByVal value As String)
strFunction = value
End Set
End Property
End Class
on your form declare a list of these to collect your data into, call it buttons. Fill up the list maybe in your form Load event like this
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Buttons = New List(Of MyButton)
For i As Integer = 0 To 4 'obviously get your info from file but i created this for demo
Dim newButton As New MyButton
newButton.Name = "Button " & i
If i Mod 2 = 0 Then newButton.MyFunction = "ClickRoutine1" Else newButton.MyFunction = "ClickRoutine2"
Buttons.Add(newButton)
Next
End Sub
create the functions that are needed by the buttons i.e
Private Sub ClickRoutine1(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Presented to you by ClickRoutine1 through " & CType(sender, Button).Name)
End Sub
Private Sub ClickRoutine2(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Presented to you by ClickRoutine2 through " & CType(sender, Button).Name)
End Sub
then in your Button1 click event you create the buttons like this
that should get you where you want to go I think,
|
|
|
|
|
Thank you Zim,
you've saved me loads of hair pulling it worked
Thanks for taking the time, now go away and grow up and return in a newer, more polite and less shouty and ignorant form. - Dalek Dave
|
|
|
|
|
A pleasure. Glad I could help.
|
|
|
|
|
You can do this a bit more cleanly by storing the delegate in MyButton .
Private Class MyButton
Private strName As String
Private clkHandler As System.EventHandler
Public Property Name() As String
Get
Return strName
End Get
Set(ByVal value As String)
strName = value
End Set
End Property
Public Property Handler() As System.EventHandler
Get
Return clkHandler
End Get
Set(ByVal value As System.EventHandler)
clkHandler = value
End Set
End Property
End Class
Private Sub ClickRoutine1(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Presented to you by ClickRoutine1 through " & CType(sender, Button).Name)
End Sub
Private Sub ClickRoutine2(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Presented to you by ClickRoutine2 through " & CType(sender, Button).Name)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
Buttons = New List(Of MyButton)
For i As Integer = 0 To 4
Dim newButton As New MyButton
newButton.Name = "Button " & i
If i Mod 2 = 0 Then
newButton.Handler = AddressOf ClickRoutine1
Else
newButton.Handler = AddressOf ClickRoutine2
End If
Buttons.Add(newButton)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim index As Integer = 0
Dim intHeightDiff As Integer = Button1.Height + 5
For Each newButton As MyButton In Buttons
Dim btnNew As New Button
btnNew.Name = newButton.Name
AddHandler btnNew.Click, newButton.Handler
btnNew.Parent = Me
btnNew.Visible = True
btnNew.Top = intHeightDiff + (intHeightDiff * index) + Button1.Top
btnNew.Left = Button1.Left
btnNew.Width = Button1.Width
btnNew.Text = btnNew.Name
index += 1
Next
End Sub
|
|
|
|
|
Nice.
I might be hijacking this with a followup question, so sorry in advance if this is a no-no...
Could this be taken a step further by getting rid of the if / case part where the AddressOf is attached?
That way it would become even more robust seeing the code will need no further modification to incorporate new handlers.
It might be simple but I just don't see it yet (mainly because I didn't sleep enough and the whole office is smelling of paint)
|
|
|
|
|
The OP originally stated that he was getting the info from a file. I assumed it was a text file where the name of the button was stored together with it's function name, and that they would be retrieved in one operation, and then assigned on the fly. In the form load event in the examples sited by me, the if i mod 2 ... was used for demo purposes only. In Gregorys example, where he assigns an eventhandler directly to a property, you would still need to decide, when you retrieve the info from the file, what function is the right one, as all you have is a string representing the name of the function, and not an instance of the function itself, if you get me. Therefore you would still need to put in the Select Case On Assignment
. To illustrate this, Take this example
So in essence there is really no difference between the way Gregory and myself implemented this.
Happy Coding
|
|
|
|
|
No discussion about the intent of the OP, or the differences between your example and the one by Gregory.
Where to retrieve the info from wasn't my concern, I might not have made that clear. It was more to find a way to make the Routines configurable as well. Although the paint smell in the office is still heavily present, found that Reflection puts me on the way quite well...
Dim lobj As Object = Activator.CreateInstance(Type.GetType("DummyLib.clsTestingTheStateHandler,DummyLib"))
Dim lass As Reflection.Assembly = Reflection.Assembly.Load("DummyLib")
Dim ltype As Type = lass.GetType("DummyLib.clsTestingTheStateHandler")
Dim lmi As Reflection.MethodInfo = ltype.GetMethod("ThisIsTheHandler")
Dim lblnState As Boolean = DirectCast(lmi.Invoke(lobj, New Object() {phtParameters}), Boolean)
Do you know of possible other alternatives to achieve something along these lines?
Enjoying the coding
|
|
|
|
|
If you want to avoid using AddHandler and RemoveHandler manually, you can create a class for your button which includes appropriate event handlers and one or more delegates supplied by the parent which should be invoked when the button is pushed. These delegates could take parameters beyond what would normally be supplied in a Click event.
|
|
|
|
|
Hi,
have a site login screen to come will have to open a popup.
Directs itself to the main page. "Giriş" button when the login screen comes.
site address is "https://ebeyanname.gib.gov.tr/giris.html"
site source code;
|
|
|
|
|
Hi,
I have a datagrid where I add a checkbox column that is not part of the database structure. the state of the checkbox is true initially, I try to change it to uncheck, nothing happens..How can I change a checkbox inside a datagrid from true to false, vice verca? also when I check a row, the record from that particular row loads to another form for other porcess.
Using Vb.net
Thanks in advance..
modified on Wednesday, April 28, 2010 1:55 PM
|
|
|
|
|
Take a look at the thread containing this[^] answer to see if it matches your situation.
Good luck!
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Assume that you load a table into the datagrid. It has some column like id, name, address etc. You add a checkbox into the datagrid using grid template. So if you want to fill / get bool value you need to do something like:
foreach (GridViewRow row in datagridView.Rows)
{
CheckBox cbYesOrNo = (CheckBox)row.FindControl("cbYesNo");
if (cbYesOrNo.Checked)
{
}
else
{
}
}
Thanks
Md. Marufuzzaman
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
Hi,
Initially the checkbox in the datagrid is set to true..when i click on it nothing happens..i want to be able to change the state of the checkbox on click..that is set to true or false
then do something on the record that is checked..
thanks,
|
|
|
|
|
Experts,
I have a web browser in vb.net, works like a treat
However is there any way that on a button click in the html web page through vb.net, can i then have a button that is invisible in vb.net to be then be visible to the user on the vb page.
e.g the web page is shown but the user doesn't do any thing. - nothing happens
the user types something in - nothing happens on the vb side
the user types something in a presses the submit buttons - a vb button appears allowing them to continue.
Any ideas?
Dan
|
|
|
|
|
Hi,
you could;
1. modify the web page any way you like, it is available as WebBrowser.DocumentText
2. add JavaScript (assuming JS is enabled).
3. add/modify hyperlinks, possibly pointing to your own web site
4. provide an embedded HTTP server, which means your own web site could be embedded inside your app (see e.g. Sample HTTP Server Skeleton in C#[^])
|
|
|
|
|
It sounds like you have a WebBrowser component displaying some HTML embedded in a windows Form, and you want to respond to events that cause a postback in the web page in the windows form. I am assuming that is what you are getting at.
We have a similar thing where html is used to display details inside a windows form and hyperlinks cause a new windows form to pop up instead of navigating in the HTML.
We do this by handling the BeforeNavigate event of the browser and interrogating the URL requested. If the URL requires custom navigation in the windows application then we set e.Cancel = True to stop the default navigation continuing and instead run our own custom code.
It is all a bit messy, but that is the price for a hybrid windows/web solution.
|
|
|
|
|
Is there a way to 'walk' the Document Outline tree and iterate through each control/object displayed? I am working on an in-house project for applying default settings to various controls on a form.
I've tried doing a recursive loop through Me.Controls, identifying if the control as children, and so forth. The problem is that we are using a mixture of standard VB control as well as third-party control (DevExpress, ComponentOne, etc) and the third-party controls are not always a part of the Me.Controls collection. However, they do appear when viewing the Document Outline tree for a given form. So I got the idea to see if I could walk the Document Outline, identify the various controls, and apply settings that way. Of course, I've found no way to walk the Document Outline.
If there's a better way to do this, I'm open to suggestion. Otherwise, I'd appreciate any help or direction that someone could offer. Thanks!
|
|
|
|
|
If you're talking about an ASP.NET webform, move this question to the ASP.NET forum.
If you're talking about a Windows Forms form, there's no such Document Outline concept and you'll have to further explain what you're talking about.
|
|
|
|
|
I'm talking about a Windows Forms project in Visual Basic 2005.
To see the Document Outline panel, open a form in your project, and click on View ->Other Windows -> Document Outline. The keyboard shortcut is Ctrl+Alt+T.
What I want to do is iterate through the objects represented in the Document Outline. Because some objects aren't available through the Me.Controls collection, I need another method to iterate through them. The Document Outline shows all objects, even those that are not available in the Me.Controls collections. I am hoping there is a way to iterate through that tree structure to reference the objects.
Thanks again in advance for any assistance.
|
|
|
|
|
OK. The Document Outline you're talking about is the Controls collection of the Form being designed, and every Controls collection of every control in it.
There is no seperate DOM for Windows Forms other than the Controls collection.
|
|
|
|
|
Can we use the text of a textbox as a query filter in VB? If yes how?
|
|
|
|
|
Yes you want to have a look at the dataview class.
assuming you have data loaded into a datatable, this example i use from the changing of the date in a datetimepicker
dim dv as dataview
dv = new dataview(dt)
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
' MessageBox.Show(DateTimePicker1.Text)
dv.RowFilter = "duedate = '" & DateTimePicker1.Text & "'"
End Sub
Thanks for taking the time, now go away and grow up and return in a newer, more polite and less shouty and ignorant form. - Dalek Dave
|
|
|
|
|
I wanted to ask this way.
I have a query for my data grid view as:
SELECT ID, Name, City, Fee, Technology
FROM [uni list]
WHERE (Name = 'Data')
where data is used as a string and this works fine BUT..
When i refer the filter case to a textbox it doesn't work. It takes this whole thing as a string and not producing the desired results.
SELECT ID, Name, City, Fee, Technology
FROM [uni list]
WHERE (Name = '[text].textbox1')
How can this be resolves
|
|
|
|
|
how do you get your data to the grid? is it via a dataset or datatable?
if datatable then
this bit goes in the event that loads your grid
dim myDataView as new dataview(myDataTable)
Datagridview1.datasource = myDataview
if Dataset then
dim myDataview as new dataview(myDataset.tables(0))
Datagridview1.datasource = myDataview
but the filtering is the same how ever
this bit goes in what you use to fire the filtering, i prefer to use a button to allow filetering
myDataView.Filter = "[Name] = '" & yourTextBox.text & "'"
if you need more help i would need to see your vb.net code as that is where its done and not at the sql statement.
Simon
|
|
|
|