Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where is Serialport, Timer?

Status
Not open for further replies.

redeagle68

Programmer
Jul 9, 2007
3
PT
Hi,
I'm pretty new with vb.net 2005, so it's possible that someone wrote something about my question.

My question is if i want to browse controls in a form or another control, i can do somthing like this:

Dim ctrlX As Control = Nothing

For Each ctrlX In MyForm.Controls
if ctrlX.Name = "MyControlName" then Exit For
Next

But, for any reason that i don't know why SerialPort, Timer, StatusStrip and ContextMenuStrip could not be found on Form.Controls. How to do?

Thanks in advance

Rui

"To be is to do"--Socrates.
"To do is to be"--Jean-Paul Sartre.
"Yabba Dabba Doo"--Fred Flintstone.
 
Being new to VB.NET, here are some initial tips.

First, you don't have to Dim the ctrlX var prior to the loop. This is ok below:
Code:
    For Each ctrlX As Control In MyForm.Controls
        if ctrlX.Name = "MyControlName" then Exit For
    Next

Second, When searching for a control in a controls collection it is better to first search for TypeOf and then type it prior to working on it so that you have all of the proper properties and methods. You can search by name if you want, but eventually you are going to want to do something like set the selected item in a DropDownList and you can't do that from an object typed as a Control.
Code:
    For Each ctrlX As Control In MyForm.Controls
        if TypeOf(ctrlX) Is DropDownList then 
            Dim ddX as DropDownList = ctrlX
            ddX.SelectedIndex = 0
        End if
    Next

Now to get to your question, you should be able to find the StatusStrip and ContextMenuStrip objects in the Controls Collection, but keep in mind that they could be contained inside of another container on your form (i.e.: Panel, GroupBox, TabControl) and you would have to also search through those control collections as well.

As for the SerialPort and Timer controls, they are not members of the controls collection. So you can not find them there. Is there a problem with just calling them by name like Timer1.Start?


Senior Software Developer
 
Hi Sirius,

Thanks for your help and tips.

My (big) problem is that i have about 11 serialports to send and receive data. And why? Because each serial port connect to a barcode printer applicator on a factory, ok?

Every time i send command to print labels or to know printer status, printer returns some info about web level, ribbon level, air pressure level, an so one.

So, i created 1 Tabcontrol with one tabpage for each printer/serialport where i show some info about production orders. I would like get serialports a little bit dynamically. Something like this:

Code:
'i know i can't use a control for serialport :)

    For Each ctrlX As Control In MyForm.Controls
        if TypeOf(ctrlX) Is SerialPort then
            ctrlX.WriteLine("MyStautsString")
        End if
    Next

In VB6 every object got an index property, right? I know that index property ended in .Net, but it was very useful.

Sorry for my english.

Regards



"To be is to do"--Socrates.
"To do is to be"--Jean-Paul Sartre.
"Yabba Dabba Doo"--Fred Flintstone.
 
Maybe what you need is a Collection of SerialPorts.

Collections are not very hard to create and implement. You need to write a collection class with things like Add, Remove and so on. Do a Google search on Creating a VB.NET Collection. That way you could create an instance of your SerialPort Collection and then every time you add a tab you also Add a SerialPort to the SerialPort Collection in the dynamic way that you want to. Then you can loop through that collection the way you want to. The Add Method might even have the ComPort #, or anything else you might need, as a parameter.

It's an idea anyway.

Senior Software Developer
 
Sirius,
Collections sounds good. I'll keep your tip and try to find something about,
Thanks.

"To be is to do"--Socrates.
"To do is to be"--Jean-Paul Sartre.
"Yabba Dabba Doo"--Fred Flintstone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top