How to clear code.... more in post

ckeezer

Well-known member
Joined
Jan 16, 2006
Messages
100
Programming Experience
1-3
How do you clear code that just happend when something else is done?

Example:
I have code that shows a search button when a menu item is clicked
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] frmCommercialCustomers_mnuSearch_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] frmCommercialCustomers_mnuSearch.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].dsOrdersByCustomer.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].btnSearchCommCust.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

When I start to enter data, I want that btn to go away... how is that done. I have many other items that this would apply to.

thanks
 
answered it by myself again... just tell it to hide itself when it is done...
VB.NET:
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].btnSearchCommCust.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[COLOR=#0000ff]

[/COLOR]
 
But that only works for the btn...
I need to clear out some formatting from labels... when a error pops it changes the lbl.BackColor to Red
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] CompanyName.length < 1 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]error1()[/SIZE]
[SIZE=2]frmComm_lblCompanyName.BackColor = System.Drawing.Color.Red[/SIZE]

Do I need to write another long If...Then procedure to clear this?
Also, if the error pops up, how do you stop processing the rest of the code until the error is fixed?
 
Last edited:
Can you supply the code or some of it? Sounds like you just need to re-arrange how you've coded it and maybe make it all into one sub routine to call on.
 
I made it into a sub routine called CheckData ()
I am changing the way that it works, by using an ErrorProvider
old code:
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ContactTitle.length < 1 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]error1()
frmComm_lblTitle.BackColor = Red
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]

new code:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CheckData()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] CompanyName = frmCommercialCUstomers_txtCompanyName.Text
[/SIZE][SIZE=2][COLOR=#008000]'Start checking for missing info
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] CompanyName.text = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]ErrorProvider1.SetError(CompanyName, "Cannot leave Company Name blank")
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]ErrorProvider1.SetError(CompanyName, "")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Problem is the routine that it is opperating in...
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] frmCommercialCustomers_mnuNext_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] frmCommercialCustomers_mnuNext.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2] CheckData()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].BindingContext(dsOrdersByCustomer, "Customers").Position = ([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].BindingContext(dsOrdersByCustomer, "Customers").Position + 1)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].dsOrdersByCustomer_PositionChanged()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

When clear out the text in CompanyName and click on mnuNext I get an error (see attached)
 

Attachments

  • error2.JPG
    error2.JPG
    17.5 KB · Views: 46
I have even tried this combo of code:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CheckData()[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.ComponentModel.CancelEventArgs[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] CompanyName = frmCommercialCUstomers_txtCompanyName.Text[/SIZE]
[SIZE=2][COLOR=#008000]'Start checking for missing info[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] CompanyName.text = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]ErrorProvider1.SetError(CompanyName, "Cannot leave Company Name blank")[/SIZE]
[SIZE=2]e.Cancel = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2]ErrorProvider1.SetError(CompanyName, [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

Getting same error as above.

I have read every posting on this thread with errorprovider in it, and searched at least 20 or so other websites and have found nothing to do what I want. I need for the text to be validated when the mnuNext item is clicked. You can see from the code above that this is tied to a BindingContext control which is what I think is causing the error. I do not want the user to have to click another button to validate the form. They should be able to click on any of the navigational controls and have the data verified before bein able to continue. If it fails, I need the code to stop until the error is fixed.

Hopes this gives more explaination to the issue.
 
Last edited:
For that code here is your problem

VB.NET:
[COLOR=#0000ff]Dim[/COLOR][SIZE=2] CompanyName = frmCommercialCUstomers_txtCompanyName.Text[/SIZE]

When you declared something and don't give an as clause it is late bound. So it dosn't know what CompanyName is. Now in that line you also have an = which in turn makes CompanyName = .text which is finally a string.

I reccommend programming with Option Strict On to avoid these problems. Either way here is your solution.

VB.NET:
[COLOR=#0000ff]Dim[/COLOR][SIZE=2] CompanyName [COLOR=blue]as [/COLOR]string = frmCommercialCUstomers_txtCompanyName.Text[/SIZE]


Then you use CompanyName as a string object without the .text included. .text is a property of class Control.

Just don't add the .text to the CompanyName object to make the code above work but i reccommend using as to make it a string and turning option strict on. Hope that helps some.
 
VB.NET:
[COLOR=blue]If[/COLOR] CompanyName [COLOR=blue]is[/COLOR] Nothing then .... (it is Null)

If it is Null it will be Nothing.
 
I am running into the error because the winform is pulling from two different tables. One table populates the form and the other populates a datagrid on the form. If there is nothing in the fields on the datagrid then the values show as null.... How do I make it so that it will not effect the form?
 
You will have to call each value indivudually and check to see whether it's Null or not before adding it. Send me the entire code segment or post it if you can and it's not too long. If it's too long then zip the project and I will have a look at it with you.

Posting the segment would be better though because you can also attract more help.
 
I am a little confused as to why the error is happening. I do not want to post a bunch of code and then it be something else.

I have my management form, which is nothing more than a GUI to other forms. When I attempt to close that form, which does not connect to the db it throws the exception at me. There is nothing out of the ordinary in the form, just some simple .show and .close code.

I have zipped the project and attached it so you can see what I am talking about.
 

Attachments

  • Smart House, INC.zip
    1.6 MB · Views: 14
NP, I am at work right now and will look at the code as soon as I get home or round abouts. Then I will try and send you a fix or cause. Also, don't be shy to post code b/c it may be somthing simple. Most of the time it is really simple and over looked or just the developer is nieve to modern commands ect.... I myself have several times posted obvious mistakes and felt ignorant for asking but either way the solution came. Besides, you never know who else may need the info.
 
Back
Top