|
There are many ways you can do this, such as keeping an array or collection of specific controls you're interested in, or checking the type of the Control in the Controls property of the container Control (like a Form , UserControl , Panel , etc.) like so:
foreach (Control c in Controls)
{
if (c is Button)
{
Button b = (Button)c;
b.Visible = true;
}
} If you only want specific names, make sure the Name property of the Button is assigned and compare that with "B1" through "B5" to make sure it's a Button you want to control.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath and Collin, thanks for the reply.
One of the issue is that I am trying to generate a switchboard menu. I have a total of 15 buttons. As users click on the items in the switchboard, the number of buttons dynamically grow or shrink depending on the sub-items under the item being clicked. So, I do not have a static number of buttons that I need to work with.
For example the main menu may look like this:
* Main
* InventoryControl
* ProductionControl
* Exit
This will be represented by 4 buttons (where * represents a button) with corresponding text.
If a user clicks InventoryControl, this window form will be repainted with a different menu, which may have 10 items. So, then I have to make those 10 buttons visible.
Thanks for the help
|
|
|
|
|
Two ways:
1) You could add all the buttons to a collection then iterate over the collection.
2) Alternatively, if these are the only buttons on your form then you could just iterate over the existing Controls collection. (You will notice in the "Windows Form Designer generated code" section there is a section that has lots of this.Controls.Add(...);
Example using existing controls collection on the form:
Button btn;
foreach(Control ctrl in this.Controls)
{
btn = ctrl as Button;
if(btn == null)
continue;
btn.Visible=true;
}
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
I am trying to update a table from a datatable. The datatable is populated first from a text file and then I would like to get that data into a database datatable. However the code below does not work, part of it is I do not know how to associate the connection string to the data adapter. Also, is there anything else I need to do? Do I need to have separate Update, Insert, and Delete commands or is it possible to just call the Update command?
ConnectToDb connection = new ConnectToDb().conn; //this returns a SqlConnection
SqlDataAdapter da = new SqlDataAdapter();
da.Update(ds.Tables["TradeOrderFillsPre"]);
|
|
|
|
|
Reading the table into a DataTable and adding that to a DataSet is what I would recommend, but iterating over the list of values in the text file and using a simple SqlCommand with SqlParameter s (so you declare the command one and parameterize it to simply change the values and call ExecuteNonQuery ) would also be easy and more efficient.
If you want to use the SqlDataAdapter , you must at least have the SelectCommand assigned to with a SqlCommand for your SELECT statement. In order to insert, update, or delete rows, however, you must have the InsertCommand , UpdateCommand , and DeleteCommand assigned on the SqlDataAdapter . You can use the SqlCommandBuilder with the SelectCommand if the command is simple enough (i.e., JOINs and other advanced statements are not supported). Also, the SelectCommand must be parameterized (see the SqlCommand.Parameters property).
See the SqlDataAdapter class documentation in the .NET Framework SDK (installed by default with VS.NET, and available online from http://msdn.microsoft.com/netframework[^]) for more information and an example of how to use some of these classes.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a list view with 3 subitems. I am handling the mouse down event and I would like to find out which subitem was clicked. I can find no easy way to get the subitem rectangle.
How do I do this?
Gary Kirkham
A working Program is one that has only unobserved bugs
I thought I wanted a career, turns out I just wanted paychecks
|
|
|
|
|
One way is to use ListView.GetItemAt to get the ListViewItem that was clicked, then enumerate the ListView.Columns collection and compare the x-coordinate with the incremental ColumnHeader.Width property (i.e., incrementally add the width of the ColumnHeader s as you enumerate and see if the x-coordinate falls within the start and end x-coordinates of the column.
Another way is to override WndProc in a derivative class, define the NMITEMACTIVATE structure, and use the iItem and iSubItem members of the struct. For more information, see the Platform SDK for the LVN_CLICK notification message and the NMITEMACTIVATE structure (includes the NMHDR structure, which you can simply merge the members of into the NMITEMACTIVATE struct if you don't plan on using the NMHDR struct elsewhere).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the reply.
Heath Stewart wrote:
compare the x-coordinate with the incremental ColumnHeader.Width property (i.e., incrementally add the width of the ColumnHeaders as you enumerate and see if the x-coordinate falls within the start and end x-coordinates of the column.
I thought of that, but it just seemed so kludgey
Gary Kirkham
A working Program is one that has only unobserved bugs
I thought I wanted a career, turns out I just wanted paychecks
|
|
|
|
|
Yeah, but it's certainly a heck of a lot easier than the second way (which encapsulates notification messages and structures for the List-View common control like most of the controls in the .NET Framework base class library do)!
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I ran into some serious bugs in the adobe reader 6.0 activex control. (pdf.ocx has problems when trying to load .xfdf file) I've been at Adobes forums and posted about this topic. People say that the control has zero support. What is a good pdf viewer control that has support for .xfdf ?
(auto merge with linked pdf and no need to save the result. Just needs to have basic viewing features and has to be able to print)
|
|
|
|
|
I have a C# com object that I'm creating which I'm testing in a VB environment. This COM object is also calling other COM objects which were written in VB.
I have a VB COM object with a method who's signature looks like this:
Public Function MyFunction(varNames() As String) As String()
I'm trying to send in a string array when calling this guy in my C# code like this:
string[] sNames = { "ProjectName" };
COMInt.ComClass oCDS = new COMInt.ComClass();<br />
<br />
sValues = oCDS.MyFunction(ref sNames);
I get type mismatch on the "ref sNames". This is just one of many combinations I've tried, but have not quite figured out the correct type match.
Here is the error I receive in VS.Net:
D:\CSS\Dev\ProjName\ProjClass.cs(255): The best overloaded method match for 'COMInt.ComClass.MyFunction(ref System.Array)' has some invalid arguments
Can anybody help me out and explain this?
P.S. Sorry for all the "MyFunction" and "ProjClass", etc.... but this is at work and I could get in trouble just for posting the names of things (they are VERY picky).
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
Check the signature of MyFunction . If it doesn't have a ref or out keyword before the param, don't use either ref or out to call the method. String s in .NET (as well as most other classes) are already reference Types, so you rarely need to use ref or out (sometimes it's necessary, like for pointers to pointers, but it's typically only necessary for value types).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yeah, I know what you're saying. The signature was the first code section/line.
I tried it first without the ref, assuming that was the correct way (most of my experience being a C programmer).
That just gave me the same error. This one shows up too:
D:\CSS\Dev\ProjName\ClassName.cs(255): Argument '1': cannot convert from 'string[]' to 'ref System.Array'.
BTW...also to note, the new C# must match the existing VB signature because that is a long time used COM object so interface can't change.
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
No, what was the signature in the CCW (the interop assembly) that was generated from the VB COM object? Did it use a ref ? The original error message you got indicates that it does not.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Oh...sorry..I got ya now. And no, it didn't use ref, I didn't think it did. I was only trying that after it didn't work originally without the ref thinking maybe I needed that to send it in.
This is the interop assembly (the type lib you mean I hope...this is my first taste in COM).
SAFEARRAY(BSTR) MyFunction([in, out] SAFEARRAY(BSTR)* varNames);
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
No, the assembly generated after running tlbimp.exe on the typelib, or using VS.NET to do the same. Judging by the method declaration, though, you should end up with something like the following:
void MyFunction(
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_BSTR)]
string[] varNames,
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_BSTR), Out]
string[] retVal);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Sorry...I'm lost. I found the command line version of the tlbimp.exe but didn't have much success. I ran it on the vb COM dll (it was created such that type library is built in I'm told), and it created an "imported" version of the dll in the program files\common files\....1033\nt directory. When I tried viewing that type library it couldn't load it.
I don't see the option to do what you are saying VS.Net.
If you (or anybody) and just mention that a bit more, I'd appreciate it. I have to run now, but I'll check again 1st thing in am.
Thanks for all your help so far.
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
You can't just use a COM control from .NET - a wrapper, or Runtime Callable Wrapper (RCW), has to be created first. This wrapper is in an COM interop assembly. You use tlbimp.exe to create a COM interop assembly and reference that assembly in your project. If you don't, you have to resort to many excrutiatingly painful methods of redefining interfaces and creating instances of COM objects at runtime and you don't get any marshaling or Type safety for free. You definitley DO NOT want to do it this way, and it's recommended that you don't.
For more information on creating interop assemblies, please read Exposing COM Components to the .NET Framework[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
According to that link (and another article I've found), when you add the COM reference to the project in VS.Net, it automatically converts it for you so that you are "on the same page".
Anyway....I found the assembly info you were talking about....I had to use a VS.Net command line tool called ildasm (which I didn't know about before).
But anyway...here it the info it generates:
<br />
.method public hidebysig newslot virtual <br />
instance string[] <br />
marshal( safearray bstr) <br />
MyFunction([in][out] string[]& marshal( safearray bstr) varNames) runtime managed internalcall<br />
{<br />
.custom instance void [mscorlib]System.Runtime.InteropServices.DispIdAttribute::.ctor(int32) = ( 01 00 15 00 03 60 00 00 )
.override COMInterface._COMClass::MyFunction <br />
}
I'm still trying to match the C# datatype for the argument for that VB6 COM function.
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
Yes, VS.NET does create the interop assembly but you never said you added such a COM reference and I've learned to assume nothing in this forum. After all, you did give the VB6 method signature and said you were trying to call it.
The data type (not C#, but .NET) is a String[] array, as well as is the return value, just as I mentioned previously. The param indeed takes a ref as designated by the address operator, & . More easily, however, would've been to look at the signature that IntelliSense returned, which would've shows the method signature as it is declared. It should look something like this:
string[] MyFunction(ref string[] varNames); When you call it, you must pass an initialized array:
string[] varNames = new string[] {"One", "Two" };
string[] retNames = obj.MyFunction(ref varNames);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
Yes, VS.NET does create the interop assembly but you never said you added such a COM reference and I've learned to assume nothing in this forum. After all, you did give the VB6 method signature and said you were trying to call it.
The data type (not C#, but .NET) is a String[] array, as well as is the return value, just as I mentioned previously. The param indeed takes a ref as designated by the address operator, &. More easily, however, would've been to look at the signature that IntelliSense returned, which would've shows the method signature as it is declared. It should look something like this:
Yes...you are right...sorry ".Net", not C#.
Sorry I didn't mention the reference...this is my 1st time playing with COM and that is the only way I knew how to do it. So I just assumed that was assumed. If that makes sense.
Thanks for your answer, but if you go back to my original post, you'll see from my code examples that was exactly where I started. So...full circle?
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
I suppose we have. Is there any possibility you can send me the VB COM library? I don't need the source. If you have configured CodeProject to send email when someone replies to you, my email address will be above.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the offer. It is sent.
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
Ok...I got it (what a pain)! I want to post the answer here for posterity in case somebody else has a similar problem in the future. For the record, I found the answer in a book a co-worker lent me (COM and .NET Interoperability by Andrew Troelsen).
The VB function signature:
<code>Public Function MyFunction(varNames() As String) As String() showed up as this in the assembly like this:
.method public hidebysig newslot virtual
instance string[]
marshal( safearray bstr)
MyFunction([in][out] string[]& marshal( safearray bstr) varNames) runtime managed internalcall
{
.custom instance void [mscorlib]System.Runtime.InteropServices.DispIdAttribute::.ctor(int32) = ( 01 00 15 00 03 60 00 00 )
.override COMInterface._COMClass::MyFunction
}
The problem was this assembly (and intellisense) was telling it it had to be a safearray and apparently it just wouldn't take a string[]! Not even if it was instantiated on initializaation like:
string[] varNames = new string[] {"One", "Two" };
The intellisense was telling me it had to be a "System.Array", not a string[]!
What needs to be done is not a "cast", but intantiating a System.Array and set it equal to the string array that was initialized and then pass in the ref to that array.
This was the way to get it working was:
COMInterface.COMObj oCOMObject = new COMInterface.COMObj();
string[] sNames = new string[] { "ProjectName" };
System.Array oTemp = sNames;
string[] sValues = new string[1];
System.Array oTempVals = sValues;
oTempVals = oCOMObject.TheCOMFunction( ref oTemp );
It wasn't intuitive....but now, in retrospect, it makes sense. Apparently you have to use the "System.Array" type for the "safearray" that it expects in the assembly, and since it doens't like you to cast it from string[] to System.Array, you have to just create an instance and set it equal.
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
Hi guys,
I have one user control implemented in .net (c#) and im trying to call it from a form in visual basic. the big problem is i cant add that control to vb designer, i just can add it as reference, so when the vb form comes up i cant see my control. i've done all the steps in this link (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp).
does anybody any hint, thanx in advance.
|
|
|
|
|