|
The first link shows how to get a list of (Access) files. The second link how to work with a listbox.
It is general documentation.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hello !
I've used this code to open a form :
Dim frm1 As New Myform1
frm1.Show(Me)
How can I do the same thing , when I doesn't know the form's name at design , but only on runtime.
I have a variable that contains the form's name as string :
Dim frmname as string
What can I do ?
Thank you !
|
|
|
|
|
You can use reflection to search for the Type of the Form and then create an instance of it with Activator (Activator has bad performance compared to other methods but for Form-instantiation it won't matter):
Dim formName As String = "Form1"
Dim formType As Type = Assembly.GetExecutingAssembly().GetTypes().[Single](Function(t) t.Name = formName)
Dim form As Form = DirectCast(Activator.CreateInstance(formType), Form)
In case the Form-class is defined in a different assembly than the assembly in which that code is executing you would have to replace GetExecutingAssembly():
Dim formType As Type = Assembly.GetAssembly(someType).GetTypes().[Single](Function(t) t.Name = formName)
..where "someType" is any Type (static notation) that is defined in the same assembly as the Form.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
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
|
|
|
|
|