clearscreen()

I want to clear the user typed content of a textbox and the results which appear in labels as a result of the code in my command button. I thought I could create a command button, label it btnClearScreen and code it ClearScreen() however, when I do this, VB.Net tells me that ClearScreen() is not declared.  I thought I read that ClearScreen() would clear data on a form but not save it.  I take it ClearScreen() is not a built in (method) is that the correct way to ask that
thanks

Gary


Answer this question

clearscreen()

  • RazzleDazzle

    Thank you, Cathal,

    In creating my own clearscreen ()
    I should code something like this on my winform

    sub clearscreen(passedContainer as control)
    Dim ctl as control

    ' This clears the textbox txtName
    If CType(ctl, txtName).Text = " "

    End If

    ' This clears the label lblResults
    If CType(ctl, lblResults).Text = " "

    End If

  • Mujeeb_rahman

    sorry i never made it clear that the previous solution was applicable for webforms, for winforms similar code works, you just reference passedContainer.Controls

    Cathal

  • Alin Constantin - MSFT

    theres lots of options, depending on how reusable you want the code to be.The simplest is just to create a sub that names the individual controls you want cleared e.g
    sub clearAll
    txtCalc.text=""
    lblCalcResults.text=""
    end sub

    Alternatively, you can use a naming scheme to determine what controls to clear e.g using a variation on the earlier code, you add an additional check, and only clear labels that being with some predetermined string i.e. in this example labels called lblClrResult and lblClrResult2 would be cleared, but a label called lblDontTouch would be ignored.

    dim ctl as control
    if typeof ctl is label then
       if ctl.name.StartsWith("lblClr") then
          ctype(ctl, label).text=""
       end if
    end if
    ...any other types as well e.g.
    end sub

    Another scheme some people use is to initially set values on both the tag and text properties, and then check if they differ when a clear button is set and reset them to their initial values. This means non-editable values are never cleared e.g.

    txtResult.tag="Please enter some text"
    txtResult.text="Please enter some text"

    and when someone clicks the clear button you do:
    if txtResult.text<>txtResult.tag then
    txtResult.text=txtResult.tag
    end if

    Obviously you can combine this with the earlier control looping i.e.
    if typeof ctl is textbox then
       if ctype(ctl, textbox).text<>ctype(ctl, textbox).tag then
          ctype(ctl, textbox).text=ctype(ctl, textbox).tag
       end if
    end if

    Cathal

  • Hamish_NZ

    Hello Daniel,

    please understand, I am not a programmer and have a great deal of difficulty getting these things straight in my head. This is all a learning experience and I am grateful for assistance, guidance and straight code help. 

    Thank you for the note,  I honestly did not think about how .net might handle something like that. I have been in class for 2 weeks so I am learning.

    best regards,
    Gary

  • SAS8721

    Hello Cathal,

    I used the Case Label which cleared the label however, it also clears the labels of the text boxes. I need to restore those labels

    can I declare a Temp for each of the text I need to restore and code to replace the labels I did not want cleared or is there an easy way to clear specific labels   ie  only the labels that contain results of computations performed in the code. Originally, these labels have no text value they get their text from code.

  • Scafe

    You can code your own, something like this should do it. Pass in the container control i.e. the form/groupbox etc. clearScreen(grpEdit)


    sub clearScreen(passedContainer as control)
    Dim ctl As Control
     
    For Each ctl In passedContainer.Controls(1).Controls
     
    If TypeOf ctl Is TextBox Then
     
    CType(ctl, TextBox).Text = ""
     
    End If

    If TypeOf ctl Is LabelThen
     
    CType(ctl, Label).Text = ""
     
    End If

    Next 


     


    Cathal

  • Michela

    I don't ClearScreen is a built-in method. How should .NET know which textboxes you want to clear, or whether it should clear the text or set it to some default value

    You will need to create your own method to do that.

  • yimin chen

    Good Morning Cathal,

    well, I had it close and with your guidance, it works.  I think I am learning but programming is not what I really do.  I love the hardware part of my job, I need to understand more about programming but will not, I am sure, ever get into programming.  thank you for your help, I will have many more questions as this class runs through Christmas. This forum is wonderful and everyone I have met so far is very gererous. I am appreciative! 

    best regards,
    Gary

  • Arul Ram

     chipscap wrote:
    Thank you, Cathal,

    In creating my own clearscreen ()
    I should code something like this on my winform

    sub clearscreen(passedContainer as control)
    Dim ctl as control

    ' This clears the textbox txtName
    If CType(ctl, txtName).Text = " "

    End If

    ' This clears the label lblResults
    If CType(ctl, lblResults).Text = " "

    End If

    No, you don't need to reference your field, we're referencing them by their type,which means it's generic i.e. it will do every textbox is a container (containers such as panels, groupboxs, forms etc.) (sorry the " " is a typo, theres no space). In fact a good way to do this, is to create a shared function in a helper class eg.

    Public Class MyHelperMethods

    public Shared Sub(byref passedContainer as control)
    dim ctl as control
    if typeof ctl is textbox then
    ctype(ctl, textbox).text=""
    end if
    ...any other types as well e.g.
    end sub
    end class

    Cathal

  • clearscreen()