|
Thank you friend !
This is working , but on my question I forget something that I should do :
On my original code ( when i know the Form), before I call the
frm1.Show(Me)
I have this instruction :
frm1.MyButton1.Enabled=True
Now , if I use your code , this instruction doesn't work.
Shows an error :
Mybutton1 is not a member of System.Windows.forms.Form
What can I do ?
Thank you !
|
|
|
|
|
Do all Forms which you will create through reflection have such a button?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
Then I would pass an "enableButton"-flag as constructor argument to the forms. Activator.CreateInstance(..) has an overload where you can specify an array for the constructor arguments as second argument after the type argument:
Dim enableButton As Boolean = True
Dim form As Form = DirectCast(Activator.CreateInstance(formType, New Object() {enableButton}), Form)
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
ok , but there are cases when I need to access a control on this form , and this control also is a variable , and its name is known only on runtime.
Is there any way to do this ?
|
|
|
|
|
How would you know which control has to be accessed?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
the control name is generated in a string variable.
I just want to get this control , and after I just want to use the enabled property ( so doesn't matter what kind of control is because all have the enabled property ).
But I don't know how to get this control by the name as a string.
|
|
|
|
|
By control name do you mean the member name like MyForm.SomeControl or the Name-property of the control MyForm.SomeControl.Name ?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I mean I have a variable with the control's name for example
cname="MyTextBox1"
|
|
|
|
|
Suggested solution: A custom base class for your forms:
Imports System.Reflection
Imports System.Windows.Forms
Public Class MyBaseForm
Inherits Form
Public Function IsControlEnabled(controlMemberName As String) As Boolean
Dim control As Object = Me.GetType().GetProperty(controlMemberName, BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public).GetValue(Me, Nothing)
Return CBool(GetType(Control).GetProperty("Enabled").GetValue(control, Nothing))
End Function
End Class
In case you already have a custom base class for your forms you can just copy that method in there.
In the previously posted code you have to change accordingly from Form to MyBaseForm:
Dim form As MyBaseForm = DirectCast(Activator.CreateInstance(formType), MyBaseForm)
And then you can simply call form.IsControlEnabled("MyControlName") to get the value of the Enabled-property of the control with the specified member-name.
edit: Don't know why I didn't think of that in the first place - it would make sense to implement it as an extension method for the Form-class instead.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
modified 5-Jun-15 3:36am.
|
|
|
|
|
I am asking this question since this is the first time that I have been asked to work on a vb.net 2010 desktop application.
I have been asked to accomplish the following tasks:
- Add a dropdown list box to ask the user what vendor they want to work with. There will be a default vendor always selected.
- Add a dropdown list box to ask the user what access 2013 database file they would like to work with. A default Access 2013 name will be in the dropdown list box.
Thus can you tell me how to accomplish this goal and/or point me to urls (links) that will tell me how to complete the complete these requests?
|
|
|
|
|
This is such a wide open, basic question that it cannot be answered in a forum post. So...
Create a winforms applciation.
add 2 listboxes to the main form
create a collection of vendors and database locations in code behind
set the collections to the datasource of the list boxes
deal with the selected item and or save event
close the form.
I suggest you look for some very basic tutorials on vb development or buy a book and work through the examples.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hello !
I have a vb.net program with sql server 2008r2 database
I'm using Entity Framework 6.
On each database's table there's a column "ToDelete" that may have the value true or false.
What is the shortest way to " Delete all records from all tables that have the ToDelete=True"
Thank you !
|
|
|
|
|
A statement like the following will yield the appropriate DELETE statements:
SELECT 'DELETE FROM ['
+ A.[name] + '].['
+ B.[name] + '] WHERE ['
+ C.[name] + ']=1'
FROM [sys].[schemas] A
INNER JOIN [sys].[objects] B
ON A.[schema_id]=B.[schema_id]
INNER JOIN [sys].[columns] C
ON B.[object_id]=C.[object_id]
INNER JOIN [sys].[types] D
ON C.[system_type_id]=D.[system_type_id]
WHERE B.[schema_id]!=4
AND C.[name]='ToDelete'
AND D.[name]='boolean'
In a procedure, you could use a cursor to iterate the results and use an EXECUTE statement on each.
Or in code, you could pass each to an ExecuteNonQuery call.
|
|
|
|
|
Hah that picks up views as well
You have that in your model database don't you! I've seen some weird utilities and that one is right up there.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Ok, that's a small whoops, but easily remedied. No, I don't use that, I just whipped it up in response to the question.
|
|
|
|
|
Sorry friend but I'm using Entity framework. I mean shortest way using entity framework.
|
|
|
|
|
Use the right tool for the right job.
|
|
|
|
|
you are saying that no way to do this with entity Framework ?
|
|
|
|
|
No. In fact I don't know enough about Entity Framework to know what it can and can't do, but I understand that it can execute a procedure.
|
|
|
|
|
Haven't we had this conversation recently!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
Something like this should work:
Using transaction As DbContextTransaction = yourContext.Database.BeginTransaction()
Const query As String = "SELECT N'DELETE FROM ' + QUOTENAME(A.[name]) + N'.' + QUOTENAME(B.[name]) + N' WHERE ' + QUOTENAME(C.[name]) + N' = 1' As CommandText FROM sys.schemas As A INNER JOIN sys.objects As B ON B.schema_id = A.schema_id INNER JOIN sys.columns As C ON C.object_id = B.object_id INNER JOIN sys.types As D ON D.system_type_id = C.system_type_id WHERE A.[schema_id] != 4 And B.[type] = 'U' And C.[name] = 'ToDelete' And D.[name] = 'bit';"
Dim commandsToExecute As List(Of String) = context.Database.SqlQuery(Of String)(query).ToList()
For Each command As String In commandsToExecute
yourContext.Database.ExecuteSqlCommand(command)
Next
transaction.Commit()
End Using
Entity Framework : Raw SQL Queries[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
In the link on your post , I red that this can be applied on Entity Framework with Code First.
I'm using Database First ???
|
|
|
|
|
Does your context class inherit from DbContext or ObjectContext ? If it's DbContext , then the code I posted should work.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|