Binding Navigator problem

Hi, In essence I have a form with empty textboxes with must be filled by a user and upon clicking a save button, the data is saved to a database. I could not succeed in connecting to the database and setting up adapters etc, manually, so I dragged a DataGridView to the form (from the Data Sources Pane). VB placed all necessary controls at the bottom. I deleted all items from the BindingNavigator except for the 'Add' button and the 'Save' button. What I need to do is place code in the form's load event 'clicking' the 'add' button so that when the form becomes active, the textboxes is aready in addnew mode. (The textboxes is bound to the datasource). The same with the 'Save' button. I have an "Import..." button in which the data must be Updated (save) to the DB. I would also like to keep the BindingNavigator Toolstrip invisible to users. Please advise. Regards
HansieNelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David L. HansenCEOCommented:
Put the Add button back and just make it invisible.  Then when the form loads (On form load event) you raise the event that triggers the add or just raise the "YourAddButtonsName.click" event.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HansieNelAuthor Commented:
I tried this:
RaiseEvent BindingNavigatorAddNewItem_Click()

and

BindingNavigatorAddNewItem_Click(sender.e)

The event is not raised. Any advice, please...
HansieNelAuthor Commented:
I'm sorry, I'm a ex-VB6 programmer and I used to ise this statement:
Call Text1_Click

This does not work in VB2010. I referenced SAMS and SYBEX and internet but solution evades me.

I tried this:

RaiseEvent BindingNavigatorAddNewItem_Click()
and
BindingNavigatorAddNewItem_Click(sender.e)

The event is not raised. Any advice, please...
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

David L. HansenCEOCommented:
I'd make my own custom event then tie that event to the click handler attached to the button.  Like this:


Dim Event StartupAdd(ByVal sender As System.Object, ByVal e As System.EventArgs)
.
.
.
'below is the code that runs when you click the add button (which you've set visible = false)'

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click, Me.StartupAdd
.
.
End Sub
'NOTE THE PART: "Handles btnAdd.Click, Me.StartupAdd " you've linked you event to this subroutine by just adding ", Me.StartupAdd" now this sub will run whenever you raise your custom event.'

Open in new window

David L. HansenCEOCommented:
Note also: The subroutine you're adding the handler to is surely name something other than btnAdd_Click.
David L. HansenCEOCommented:
When raising the event you can pass in "nothing" for both the sender and e parameters.
HansieNelAuthor Commented:
Thank you very much for your post sl8rz. But, forgive me, I'm still at a lost.

1st) I cannot find this sub:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click, Me.StartupAdd
.
.
End Sub

Where do I place this line?:

Dim Event StartupAdd(ByVal sender As System.Object, ByVal e As System.EventArgs)

When I DoubleClick the "add" button I get this in the code pane:

 Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
.
End Sub

Please note that I don't want to write ane code to add a new record. I want my app to 'click' on that '+' button programmatically. The same counts for the 'Save' button.

In VB6 I could just say: Call BindingNavigatorAddNewItem_Click
but how does one do it in VB 2010?

Another thing: I cant find the code that VB atomatically created to make that BindingNavigator Strip do its work.

If I can't get this to work, I need to get the code to connect a SQLEXPRESS Database and save the textboxes to the db using code.

Thanks for your time.
HansieNelAuthor Commented:
Oh, and what statement do I use to make vb click on that button (to RAIZE that button's event)?

Much appreciated.
David L. HansenCEOCommented:
The sub "btnAdd_Click" is the same as "BindingNavigatorAddNewItem_Click" I just couldn't remember what the wizard calls it when you build it by dragging the datasource onto the form as you did.  So go there and just add the handler to the sub so it reads " ...EventArgs) Handles BindingNavigatorAddNewItem.Click, Me.StartupAdd"

You'll place the line: Dim Event StartupAdd(ByVal sender As System.Object, ByVal e As System.EventArgs)   at the top of the form you are raising the event from.  You may just want to place it in a module (those are global).  Then to trigger it, you'll just need to place the following line in which ever routine you want to call it from (most likely in the FormLoad routine)...

raiseevent StartupAdd(Nothing, Nothing)
HansieNelAuthor Commented:
Thank you sl8rz for your detailed explanation. I have done everything you instructed but it does not work.

I placed this:    Dim Event StartupAdd(ByVal sender As System.Object, ByVal e As System.EventArgs) in the gen declaration section of the form.


I added this:

Me.StartupAdd

so it looks like this:

Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click, Me.StartupAdd
.
 End Sub

I placed this code: raiseevent StartupAdd(Nothing, Nothing) inside the click event of the button.

Result:

1) VB 2010 does not accept the DIM statement. I changed it to:
Event StartupAdd(ByVal sender As System.Object, ByVal e As System.EventArgs)
Now vb does not complain.

2) I run the app. Clicking my button doen not create  addnew. When I click the addnew button on the BindingNavigator it works.

Sorry for this. Any other advise?
HansieNelAuthor Commented:
I inserted a breakpoint in the button and followed the execution line by line.
VB DOES execute the:

Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click, Me.StartupAdd

line, but there is no code INSIDE that sub to execute.

I don't think that's the sub the BindingNavigator.addnew uses to make an addnew. That is why I asked earlier VB hides the Navigator's code. (Which the Form Designer creates.) I think the sub resides in there. I think...
David L. HansenCEOCommented:
Ok, let's see...well, the "Raiseevent StartupAdd(Nothing,Nothing)" line should go in the Form_Load sub instead.  Do that then let's see what happens...also, leave your break point on the BindingNavigatorAddNewItem_Click subroutine or you may even want to put a: Message.Show("Running Add New Item routine now.") line in the BindingNavigatorAddNewItem_Click subroutine so you know for sure when it works.
HansieNelAuthor Commented:
Nope, I've done what you instructed but still no addnew.

That Sub is executed although I don't think it's the same event the addbutton on the Navigator Strip fires.
David L. HansenCEOCommented:
So when Form_Load runs you do see the triggering of the BindingNavigatorAddNewItem_Click routine, right?  But there is no code in the routine???
HansieNelAuthor Commented:
That's right.  The only code in the routine is the one you instructed me to put in there (message.show).
But like I said, clicking on the add button on the navigator DOES perform an addnew.
HansieNelAuthor Commented:
BTW, when I doubleclick the add button on the navigator, I am taken to that same sub you instructed me to put the message code in.
David L. HansenCEOCommented:
This is what I'd do to find the 'hidden' routine.  When you run in debug and hit the message.show line press Ctrl + Alt + C and a Call-Stack window will open.  I use this quite a bit when debugging (I just keep it docked so it is alway there when I'm looking for bugs).  The topmost item in that window will be the current routine you're in, below it, you will see a list of all the other routines the app has already passed through (in the order it ran them) with the first at the bottom.  I suspect the hidden routine will be one or two steps below the topmost step.  Just double click on any of the items and it will take you there.
HansieNelAuthor Commented:
I'm sorry, my friend, I just don't understand what's going on.

First of all, In debug mode, pressing F8, as I scroll through the routines, the Call Stack window displays nothing.

Secondly, I put the Beep command inside that sub (Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click, Me.StartupAdd). When the form loaded I heard a beep (RaizeEvent in the Form.Load). When I clicked the Addnew button in the Navigator, I also heard a beep! That means clicking that button DOES fire that sub, although there is no code to manipulate the DataSet. (Stepping with F8 also confirmed this.

So,,, WHERE is the code that create an AddNew event when clicking that button.

You ,must be tired of this I guess. Be well.
HansieNelAuthor Commented:
Ok, I just commented out that sub:

    'Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click, Me.StartupAdd
    '    Beep()
    '    'MsgBox("Stop", , "Stop")
    'End Sub

I run the app. I Clicked on the AddNew button and an AddNew Event is still raized. The Textboxes are cleared waiting for new input! That button does not need that sub mentioned above in this current post.

I wonder where that code resides.
HansieNelAuthor Commented:
My Friend, I discovered the secret.

I don't have to worry about that AddNew button in order to raize the addnew event.

All I have to do is to set the Navigator's visible property to False and add this code:

Me.MainBindingNavigator.BindingSource.AddNew()

to the form.load event. THERE YOU ARE, SUCCESS!!!

Thanks so much for all your help.
Blessings
David L. HansenCEOCommented:
That's great!  Glad you figured it out.  I'm suprised you didn't see anything in the call stack.  Perhaps if you put a break point at a place in your code that will fire later on you'll see the step that has eluded us.  I suppose that's no more than a curiosity at this point though.  Those wizards are useful but customizing the code after the wizard builds it is a major headache.  I think it's better to just build your own classes etc. unless very little customization is needed.  Anyway, glad you got it, hope I was somewhat helpful.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.