Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Clearing All Textboxs on a Form in vb.net

Posted on 2009-02-12
9
Medium Priority
?
431 Views
Last Modified: 2013-11-26
Hello All

I have 2 issues that I need help with. I have a form with a few textboxes and 3 radio buttons that control which fields are enabled by using an IF statement (see the frmSearch_CheckedChanged sub below).

Issue 1
I want the contents of all the textboxes to be cleared when the user selects a different radio button
I wrote a small sub ( ClearTextBoxes) that would clear the contents of all textboxes on the form and then called this sub in the IF statement but this does not work. But if I do it manually for each textbox (as in the code below), the process works. Could someone explain why the ClearTextBoxes sub isnt working when I call it in the frmSearch_CheckedChanged sub? The sub calls have been commented out on purpose below.

Issue 2
I'm new to vb.net programming and I'm not sure what is the best way to write my code. Is there a better way to write the sub frmSearch_CheckedChanged? Would it be better to write code for each radio button rather than have on sub that deals with all 3?

Many thanks for your help.

Rob

Public Sub ClearTextBoxes(ByVal myform As Form)
 
        Dim control As Control
 
        'clear the text boxes
        For Each control In Me.Controls
            If TypeName(control) = "TextBox" Then
                control.Text = ""
            End If
 
        Next
 
    End Sub
 
'------------------------------------------------------
    Private Sub frmSearch_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbTitleSearch.CheckedChanged, rbNameSearch.CheckedChanged, rbKeywordSearch.CheckedChanged
 
        'ClearTextBoxes(Me)
 
        If rbTitleSearch.Checked = True Then
            'ClearTextBoxes(Me)
            tbTitleSearch.Enabled = True
            tbYearSearch.Enabled = True
 
            tbSurnameSearch.Text = ""
            tbForenameSearch.Text = ""
            tbKey1.Text = ""
            tbKey2.Text = ""
            tbKey3.Text = ""
            tbKey4.Text = ""
            tbKey5.Text = ""
 
            tbForenameSearch.Enabled = False
            tbSurnameSearch.Enabled = False
            tbKey1.Enabled = False
            tbKey2.Enabled = False
            tbKey3.Enabled = False
            tbKey4.Enabled = False
            tbKey5.Enabled = False
            rbKeyAll.Enabled = False
            rbKeyAny.Enabled = False
 
        ElseIf rbNameSearch.Checked = True Then
            'ClearTextBoxes(Me)
            tbForenameSearch.Enabled = True
            tbSurnameSearch.Enabled = True
 
            tbTitleSearch.Text = ""
            tbYearSearch.Text = ""
            tbKey1.Text = ""
            tbKey2.Text = ""
            tbKey3.Text = ""
            tbKey4.Text = ""
            tbKey5.Text = ""
 
            tbTitleSearch.Enabled = False
            tbYearSearch.Enabled = False
            tbKey1.Enabled = False
            tbKey2.Enabled = False
            tbKey3.Enabled = False
            tbKey4.Enabled = False
            tbKey5.Enabled = False
            rbKeyAll.Enabled = False
            rbKeyAny.Enabled = False
 
        ElseIf rbKeywordSearch.Checked = True Then
 
            'ClearTextBoxes(Me)
            tbKey1.Enabled = True
            tbKey2.Enabled = True
            tbKey3.Enabled = True
            tbKey4.Enabled = True
            tbKey5.Enabled = True
            rbKeyAll.Enabled = True
            rbKeyAny.Enabled = True
 
            tbTitleSearch.Text = ""
            tbYearSearch.Text = ""
            tbSurnameSearch.Text = ""
            tbForenameSearch.Text = ""
 
            tbTitleSearch.Enabled = False
            tbYearSearch.Enabled = False
            tbForenameSearch.Enabled = False
            tbSurnameSearch.Enabled = False
 
        End If
 
    End Sub

Open in new window

0
Comment
Question by:shorak
  • 6
  • 3
9 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 23620715
you can use LINQ to improve the code

I would use the ClearTextBoxes to clear the textboxes and enable = false all the textboxes.

I would keep your frmSearch_CheckedChanged, but remember that if you have entered something in the textboxes and start switching with the radiobuttons, everything will be cleared
Public Sub ClearTextBoxes()
 '---retrieve all the checkbox controls in the current form---
Dim allTextBoxes As IEnumerable = From ctrl In Me.Controls Where TypeOf ctrl Is TextBox
' Now clear every textbox
For Each c As TextBox In allTextBoxes 
     c.Text = ""
     c.Enabled = false             
Next
End Sub
 
'------------------------------------------------------
    Private Sub frmSearch_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbTitleSearch.CheckedChanged, rbNameSearch.CheckedChanged, rbKeywordSearch.CheckedChanged
 
        ClearTextBoxes
 
        If rbTitleSearch.Checked = True Then
            tbTitleSearch.Enabled = True
            tbYearSearch.Enabled = True
 
            rbKeyAny.Enabled = False
 
        ElseIf rbNameSearch.Checked = True Then
            tbForenameSearch.Enabled = True
            tbSurnameSearch.Enabled = True
 
        ElseIf rbKeywordSearch.Checked = True Then
 
            'ClearTextBoxes(Me)
            tbKey1.Enabled = True
            tbKey2.Enabled = True
            tbKey3.Enabled = True
            tbKey4.Enabled = True
            tbKey5.Enabled = True
            rbKeyAll.Enabled = True
            rbKeyAny.Enabled = True
 
        End If
 
    End Sub
 

Open in new window

0
 
LVL 11

Author Comment

by:shorak
ID: 23620968
Hi. thanks for your help. I tried substituting my code with yours and it dosnt seem to do anything. When the form load.. All the texboxes are enabled and switiching between radio buttons dosnt disable any of them. Also it is not clearing any of the entered text either.

I also tried calling the function within each part  of the if statement and that hasnt done anything either.

I'm thinking that maybe the calling of the ClearTextBoxes function isnt working properly as nothing happened when I tried calling the original version of the function in my original code.

Any ideas?

Thanks

Rob
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23620974
When you debug your code, does it go into the procedure frmSearch_CheckedChanged or cleartextboxes ?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 53

Expert Comment

by:Dhaest
ID: 23620981
Are your textboxes directly on the form or are they on a panel, a groupbox, ... ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23621000
If the controls can be on a panel, groupbox, ... then you need a recursive cleartextboxes

Public Shared Sub ClearTextboxes(ByVal container As Control)
    For Each ctl As Control In container.Controls
        Dim textBox = TryCast(ctl, TextBox)
        If textBox IsNot Nothing Then
            textBox.Text = ""
            textBox.Enabled = false
        End If
        If ctl.Controls.Count > 0 Then
            ClearTextboxes(ctl)
        End If
    Next
End Sub
0
 
LVL 11

Author Comment

by:shorak
ID: 23621044
they are in a groupboxes.. which might explain why function might not be not working.. there are 3 group boxes for each radio button

GroupBox1called contains tbTitle and tb Year
GroupBox2 contains tbSurname and tbForename
GroupBox3 contains tbKey1, tbKey2 etc

the groupboxes are named as above

What do I need to do to reference the textboxes within the groupboxes in your function?
Also what is LINQ, a standard, a process or application of some sort?

Thanks again for your help

Rob


0
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 23621063
You can call the function below like this:

ClearTextboxes(Me)


What is LINQ: http://geekswithblogs.net/technetbytes/archive/2007/04/30/112129.aspx
(Language-Integrated Query , new feauture from .NET 3.5, which will be more and more used in the future)
Public Shared Sub ClearTextboxes(ByVal container As Control) 
    For Each ctl As Control In container.Controls 
        Dim textBox = TryCast(ctl, TextBox) 
        If textBox IsNot Nothing Then 
            textBox.Text = ""
            textBox.Enabled = false
        End If 
        If ctl.Controls.Count > 0 Then 
            ClearTextboxes(ctl) 
        End If 
    Next 
End Sub 

Open in new window

0
 
LVL 11

Author Comment

by:shorak
ID: 23621127
Many thanks for your help and effort. The form now works the way I want but there is one drawback.. when the form first loads, all the fields are enabled but ideally I only want tbTitle and tbYear enabled as a default since my default radiobutton selected is rbTitleSearch.

I suppose I could set the Enabled field to False  in the textbox properties for all the other texboxes as a default.

Thanks again

Rob
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23621136
>> I suppose I could set the Enabled field to False  in the textbox properties for all the other texboxes as a default.

You can also put the enabled in your form-load .
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question