|
tekfall wrote:
Once again, thanks for the help
Glad to help. Remember, these forums are here for you if you get stuck.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi, I have been using google to find a way to count all controls and go trough them to find all the TextBox controls and put them into an array of TextBoxes.
I can't seem to find any good way to do this so I was thinking of making a function to count them, but I get an error I just can't get around it seems.
This is how the function looks like:
Public Function GetControls(ByVal cControl As ControlCollection)<br />
Dim iControls As Integer<br />
For i As Integer = 0 To cControl.Count - 1<br />
If TypeOf cControl(i) Is TabControl Then<br />
MsgBox(cControl(i).Name)<br />
GetControls(cControl(i).Controls)<br />
End If<br />
If TypeOf cControl(i) Is TabPage Then<br />
MsgBox(cControl(i).Name)<br />
GetControls(cControl(i).Controls)<br />
End If<br />
If TypeOf cControl(i) Is TextBox Then<br />
iControls += 1<br />
End If<br />
If TypeOf cControl(i) Is Label Then<br />
iControls += 1<br />
End If<br />
Next<br />
MsgBox(iControls)<br />
End Function
I call the function with 'GetControls(tcMain.Controls)' there tcMain is an TabControl object and all I get is this exception 'System.InvalidCastException'. When I try to call the function with 'GetControls(Me.Controls)' it get it right and count the controls on the form, but when the function finds a TabControl object I get the same exception.
Anyone got any idea what I made wrong, or have any tip on how I can count all controls and "child-controls" I would be more then happy!
Thanks on beforehand! /Joakim
|
|
|
|
|
have you tried this:
dim count as integer = 0<br />
for each c as control in me.controls<br />
if typeof c is textbox then<br />
count +=1<br />
end if<br />
next<br />
<br />
messagebox.show("There are {0} textboxes.",count)
that will NOT check other container controls such as panels, groupboxes.
Hope that helps!
Thanks for firing me Darrell Eaker. I TRIPLED my salary and don't have to deal with morons like you. ![Smile | :)](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_smile.gif)
|
|
|
|
|
Well yes I have tried that sorta, and I have all my textboxes in a TabControl with multiple Tabs so I need to loop trough all controlcollections to find all the controls, and thats why I made the function to loop trough all object that has controlcollection like TabControl and TabPages.
Thanks for you reply tho. /Joakim
|
|
|
|
|
Do it recursively - write a function that takes a control and iterates through it's children, and have it call itself for the control collections as it steps through.
function CountTextBoxes(ControlCollection controlCollection, int count) returns int ' Whatever the VB syntax is
for each c as control in controlCollection
if typeof c is textbox then
count +=1
else
count = CountTextBoxes(c.Controls, count)
end if
next
Then you need to do something like
int count = CountTextBoxes(myControl.Controls, 0)
messagebox.show("There are {0} textboxes.",count)
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Dunno why but it doesn't work for me...
Check out this test:
<br />
Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click<br />
<br />
Dim ctest As ControlCollection<br />
ctest = Me.Controls<br />
<br />
End Sub<br />
This works.
<br />
Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click<br />
<br />
Dim ctest As ControlCollection<br />
ctest = tcMain.Controls 'tcMain is a TableControl object<br />
<br />
End Sub<br />
This doesn't, all I get here is a casting error.
Anyone know why?
/regards Joakim
|
|
|
|
|
That's weird, because that is a ControlCollection. Is that all your code ? What line blows up ? If you can give me some html, I can do a test here.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Yeah I know, I made another test just to check that there was no other code in my program that was messing with this function.
So I made a fresh start, standard windows app. with a TabControl with 2 TabPages, then I added 2 TextBoxes and 1 Label to each TabPage, and a Button on the first TabPage. and then I added this code and get the same error.
<br />
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
GetControls(Me.Controls)<br />
End Sub<br />
Public Function GetControls(ByVal cControl As ControlCollection)<br />
Dim iControls As Integer<br />
For i As Integer = 0 To cControl.Count - 1<br />
If TypeOf cControl(i) Is TabControl Then<br />
MsgBox(cControl(i).Name)<br />
GetControls(cControl(i).Controls)<br />
End If<br />
If TypeOf cControl(i) Is TabPage Then<br />
MsgBox(cControl(i).Name)<br />
GetControls(cControl(i).Controls)<br />
End If<br />
If TypeOf cControl(i) Is TextBox Then<br />
iControls += 1<br />
End If<br />
If TypeOf cControl(i) Is Label Then<br />
iControls += 1<br />
End If<br />
Next<br />
MsgBox(iControls)<br />
End Function<br />
No idea why it wont work, looks like TabControls don't use the same ControlCollection as for example TabPages or something like that.
Thing is that I like to make this function dynamic so I can use it in any situation to check for Controls in an ControlCollection.
Joakim .A - State: VB6 -> VB.NET
|
|
|
|
|
and here is a html with a rar on the testproject >WindowsApplication1.rar<
the line 165 crashes in this example, when it tries to call the function with a TabControls ControlCollection.
Joakim .A - State: VB6 -> VB.NET
|
|
|
|
|
You seem to be right - the TabPage AND TagControl have TabControl.ControlCollection and TabPage.ControlCollection collections, you need seperate functions to pass those objects to and iterate through. What a disaster.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I think you making it harder than you need to.
For each tabcontrol there are tabpages which contain the controls your trying to count.
Function CountControls(ByVal tPage As TabPage)
Dim count As Integer = 0
Dim c As Control
For Each c In tPage.Controls
If TypeOf (c) Is TextBox Then
count += 1
End If
Next
Return count
End Function
Simply pass in the tabpage you wish to count the controls on..
Debug.WriteLine("Total textboxes: " & CountControls(TabControl1.TabPages(0)))
Hope this helps.
|
|
|
|
|
Sould read... sorry
Function CountControls(ByVal tPage As TabPage) as integer
Dim count As Integer = 0
Dim c As Control
For Each c In tPage.Controls
If TypeOf (c) Is TextBox Then
count += 1
End If
Next
Return count
End Function
|
|
|
|
|
Aye, I have done that now, but I want to make this function more dynamic.
So I could pass Me.Controls to the function and get refs to all TextBoxes for example.
Joakim .A - State: VB6 -> VB.NET
|
|
|
|
|
How do I open a new child MDI form from an existing childmdi form?
I want the new child mdi form to open within the same parent.
Godwin
|
|
|
|
|
in the child form do this
Dim frm as new Form1
frm.mdiParent = Me.MdiParent
frm.show()
|
|
|
|
|
|
hii everybody..
my ASP .NET Web Application was working properly.. but suddenly one day all the code in javascript stopped working.. i m not able to find out the prob.. all the other data retrieval is working properly..
plzzz help if u can...
|
|
|
|
|
Is the browser running javascript ? Did you install XP SP2 ? That could be turning off jscript support in the browser. Although, the rest of ASP.NET relies of jscript - are you sure your scripts are being inserted into the pages still ? How do you insert them ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I have a windows form with an com component.This application works fine on local hard drive but when i acces my application from the network it gives system.security.securityexception:Request failed.What should i do
smita
|
|
|
|
|
I guess that you are trying to load the application after mapping the network drive (directly from server instead of installing). is it true ? if yes then remember that application will load in your system and in your system com component does not exist. So install it on your machine.
|
|
|
|
|
i am working with vb.net and want to create a random file. the file should not be fixed size......helpme .... ByVal e As System.EventArgs) Handles Button6.Click
Dim Employee As Person
FileNum = FreeFile()
FileOpen(FileNum, TextBox1.Text, OpenMode.Random, , , Len(Employee))
Do While Not EOF(FileNum)
FileGet(FileNum, Employee, )
If Employee.ID = Val(TextBox5.Text) Then
Position = Loc(FileNum)
TextBox5.Enabled = False
TextBox6.Enabled = True
TextBox7.Enabled = True
Button5.Enabled = True
Button6.Enabled = False
Button7.Enabled = True
TextBox6.Text = Employee.Name
TextBox7.Text = Employee.Surname
Exit Do
End If
Loop
FileClose(FileNum)
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim Employee As Person
FileNum = FreeFile()
FileOpen(FileNum, TextBox1.Text, OpenMode.Random, , , Len(Employee))
Seek(FileNum, Position)
Employee.ID = Val(TextBox5.Text)
Employee.Name = TextBox6.Text
Employee.Surname = TextBox7.Text
FilePut(FileNum, Employee, )
FileClose(FileNum)
TextBox5.Enabled = True
TextBox6.Enabled = False
TextBox7.Enabled = False
Button5.Enabled = False
Button6.Enabled = True
Button7.Enabled = False
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim Employee As Person, FileNum1, FileNum2 As Integer
FileNum1 = FreeFile()
FileOpen(FileNum1, TextBox1.Text, OpenMode.Random, OpenAccess.Read, , Len(Employee))
FileNum2 = FreeFile()
FileOpen(FileNum2, "abcdefg.tnt", OpenMode.Random, OpenAccess.Write, , Len(Employee))
Do While Not EOF(FileNum1)
If (Loc(FileNum1) <> Position - 1) Then
FileGet(FileNum1, Employee, )
FilePut(FileNum2, Employee, )
Else
FileGet(FileNum1, Employee, )
End If
Loop
FileClose(FileNum1)
FileClose(FileNum2)
Kill(TextBox1.Text)
Rename("abcdefg.tnt", TextBox1.Text)
TextBox5.Enabled = True
TextBox6.Enabled = False
TextBox7.Enabled = False
Button5.Enabled = False
Button6.Enabled = True
Button7.Enabled = False
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
End Sub
End Class
after inserting few records in file ....new data is not being taken....
|
|
|
|
|
virtual_manu wrote:
i am working with vb.net and want to create a random file
doesn't mean anything for me...
what to you write inside your file ?
virtual_manu wrote:
the file should not be fixed size......
just write in it, it will increase its size ...
pssst, do you know what you're trying to do ?!
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
make your question more clear and do not put the long code as 99% of the viewers do not have enough time to read and debug your code. So put your question again without code and explain it little more what you want. like creating text file and inserting values in it from text boxes or what ever is your requirement
or wait for the 1% guys if they found this code they might debug it for you.
|
|
|
|
|
i want to create a random file in vb.net ...where i can insert, update and search data ...help
|
|
|
|
|
well I guess you want to work in file system. as there is nothing in my vocabulary called "Random File" . I suggest you to use XML which is supported well by Dot net and is good according to your requirement
|
|
|
|