Dynamically add members to action - VB.NET - vb.net

I'm trying to perform a loop action to edit same property with its specific value on multiple controls.
Something like:
Dim action as action
action.add(control1)
action.add(control2)
action.add(control3)
action.run
Then the action will perform something like this:
For each ctrl as control in action.controls
ctrl.text = ctrl.text & "another value"
Next

I ended up creating new tasks of the same action by calling it with different controls. Even better than I wanted to.

Related

vb.net deleting a label and textbox at runtime

So i have a class that creates a new label and textbox when it is called. The names of each label and textbox are in a continues order counting upward using a variable called i:
"Label" & i creates the name Label1 and "Textbox" & i creates the name Textbox1
i += 1
And so on. Now i want to add a procedure that deletes the last label and textbox using i. I have tried to make this procedure using the lines of code below but this doesnt work as the string cannot be converted to system.windows.forms.control :
Form1.Controls.Remove("Label" & i)
Form1.Controls.Remove("Textbox" & i)
i -= 1
Controls is a list of controls using the line of code:
Public controls As List(Of Control)
Basically i need a way to delete a label and textbox using the variable i in the class. Any ideas? Thanks.
Try...
Form1.Controls.RemoveByKey("Label" & i)
Form1.Controls.RemoveByKey("Textbox" & i)
This does not work if you have these controls tucked in GroupBox, Panel, or some other container on your form.
I think you should try adding your controls into a panel, and then delete it as this link suggests (it's quite like what you tried, but inside a panel):
Removing Dynamically created controls in Panel

Passing the data between windows forms

I have a windows application.
In which I want to call one form from another and take Yes/No option from user
and that Yes/No choice again passed to the parent form.
How to do this ?
I have tried by creating object but it dont work.
Please check below image...
I have Call conformation form on click of final button, when user choose ok/Cancel that value passed to the again parent form and will take desired action depends on choice.
how to take input from this child form ?
The easiest way is to set a variable to Public when you instantiate it.
Public myVariable as String = ""
Then you would access it from anywhere.
From your own form
Me.myVariable = "" 'Whatever you would like to set
From another Form
Form1.myVariable = "" 'Whatever you would like to set
'or
FormName.Variable = ""
Dim confirmModal = new ConfirmModal
Dim result = confirmModal.ShowDialog()
If result = OK then resltValue = confirmModal.ResultValue
ResultValue is whatever you want to pass to the parent.
You can use a public variable in a module or in the parent form that is accessed by both forms to store the yes/no value.
public returnCode as boolean
If the variable is in the parent form, it can be referenced in the child form using the parent form's name:
form1.returnCode = True

Control & Tag Properties

I am trying to write a function called HasUnsavedChanges which basically should be called when you are closing the form. i.e after saving the item, it should check the values in the controls against the values in Tag property which are in the same function, e.g. txtFirstName.Tag = .ContactFirstname and txtFirstName.Text = .ContactFirstname. If there is any difference between the two, return True. On closing the form, if this function returns true, then ask if changes should be saved.
I think the right way would be to write a For loop to loop through the controls, but I'm stuck after that.
Assuming you have the .Text and .Tag properties stored in the same control, try something like this:
For Each objControl As Control In frmMain.Controls
If TypeOf objControl is TextBox Then
If objControl.Tag <> objControl.Text Then
'---Changes have been made!---
End if
End if
Next
Obviously, you'll need to replace "frmMain" with your form's name.

Invalid Conversion error

I recently upgraded a VB 6 project to .net. I'm having a problem with this block of code:
Dim CtrlName As System.Windows.Forms.MenuItem
For Each CtrlName In Form1.Controls
'Some code here
Next CtrlName
Now this code compiles but throws the following runtime error:
Unable to cast object of type 'System.Windows.Forms.Panel' to type 'System.Windows.Forms.MenuItem.
I have a panel control on the subject form. How do I resolve this?
Thanks.
You are iterating over all controls that are directly inside the form, not just the MenuItems. However, your variable is of type MenuItem. This is causing the problem.
For normal controls (e.g. Buttons), you’d want to use the following, easy fix; test inside the loop whether the control type is correct:
For Each control As Control In Form1.Controls
Dim btt As Button = TryCast(control, Button)
If btt IsNot Nothing Then
' Perform action
End If
Next
However, this does not work for MenuItems since these aren’t controls at all in WinForms, and they aren’t stored in the form’s Controls collection.
You need to iterate over the form’s Menu.MenuItems property instead.
The items in the Controls property of a form, which may or may not be MenuItem. Assuming that you just want to iterate over MenuItem objects you can change your code to:
For Each menuControl As MenuItem In Me.Controls.OfType(Of MenuItem)
' Some code
Next
Note that the menuControl variable is declared in the For so is only accessible within the block and is disposed automatically.
for each ctrl as control in me.controls
if typeof ctrl is menuitem then
' do stuff here
end if
next
typeof keyword allows you to test the type of control being examined in the control collection.
Found the answer after a bit of research, you need to search for the menu strip first and then loop through the items collection.
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is MenuStrip Then
Dim mnu As MenuStrip = DirectCast(ctrl, MenuStrip)
For Each x As ToolStripMenuItem In mnu.Items
Debug.Print(x.Name)
Next
End If
Next

VB.NET Programmatically add code between tags

I have a custom control that does some work for me on async postbacks. Normally, I'd call the control directly from the presentation side using the following code on the ASPX page:
<mytag:CustomControl runat="server">
html (or other text) goes here
</mytag:CustomControl>
However, in my current application, I need to dymanically create the control from the codebehind, using code similar to the following:
Dim myControl As myClass.CustomControl = New myClass.CustomControl
myControl.ID = "someID"
myControl.?????? = "html (or other text) goes here"
Me.Controls.Add(myControl)
When adding the control to the page dynamically, how do I add info that would normally be between the start and end tags if the control were added the normal, non-dynamic way?
Thanks
Here's the actual control:
Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim scriptmanagerPage As ScriptManager = ScriptManager.GetCurrent(Page)
If scriptmanagerPage Is Nothing Then
'Do nothing
Else
'See if we are in a postback
If scriptmanagerPage.IsInAsyncPostBack Then
'We are in a postback; register the script
Dim stringbuilderWorking As New StringBuilder()
MyBase.Render(New HtmlTextWriter(New StringWriter(stringbuilderWorking)))
Dim stringScript As String = stringbuilderWorking.ToString()
ScriptManager.RegisterStartupScript(Me, GetType(ScanWorkXAJAX), UniqueID, stringScript, False)
Else
'Not in a postback
MyBase.Render(writer)
End If 'In an async postback
End If 'Scriptmanager present
End Sub
What do you mean by data? More controls?
You can use
myControl.Controls.Add(childControlHere);
EDIT After question was clarified:
Add a literal control. i.e.
myControl.Controls.Add(new LiteralControl("<b>hello world</b><script type='text/javascript'>alert('hi');</script>"));
Do you mean FIND and append data to controls inside your dynamic control?
You can use the WebControl.FindControl method to find your control embedded in your custom control and then you can add data via its properties.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.findcontrol.aspx
That depends on the properties of the custom control you are using. You may have to bind to a template to display so that events will be fired, it really just depends on your specific control.
Was able to accomplish this using a literal control.
Code above plus:
Dim myLiteral As LiteralControl = New LiteralControl
myLiteral.ID = "myLiteral"
myLiteral.Text = "html (or some other text) goes here"
myControl.Controls.Add(myLiteral)