Event handler anomaly

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Event handler anomaly

I pattern my lookup forms according to chapter 13 of ado.net core reference.
With some tweaks and additions from things I've learned in these newsgroups,
they work fine.

Recently, a form went bad. I have a form based on an authors table. I
added some fields to a table called authors2, and added the data. I copied
the usual buttons from the authors forms << < > >> Edit Add Delete Update
Cancel SubmitChanged, Close. I have a grid on the form as well, called dgrd.
Textboxes, comboboxes and a checkbox for a boolean field. I added the
underlying code from the authors form, then made all of the adjustments.

For a bizarre reason, when I doubledclicked the add button, instead of the
add button code, I got a new private sub called Click_1. Mind, the original
sub was there. Same with every button except form load. In the end I made
additional controls and new data adapters and a dataset, etc. I filled in
the new click_1 event with the same code.

Private Sub btnAdd_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
cm.AddNew()
bAdd = True
Me.cboAuthorType.SelectedIndex = 1
Me.cboContentRights.SelectedIndex = 1
Me.cboPointOfView.SelectedIndex = 1
Me.cboPrimaryPub.SelectedIndex = 1


Me.cboAuthorType.SelectedIndex = 0
Me.cboContentRights.SelectedIndex = 0
Me.cboPointOfView.SelectedIndex = 0
Me.cboPrimaryPub.SelectedIndex = 0
SetEditMode(True)
Me.dgrd.Enabled = False
End Sub

Here's a problem. Under certain conditions when this form is called by
another form, I want some selected text added to a new entry. I do this
through a Authors2CallingForm property (as string). If the value is "TFS2" I
want the addnew button clicked and the selected text put in the right place.
The details don't matter here.

I can't use the btnAdd.PerformClick() method because of the click_1 problem,
in the form_load event procedure. So I created a Authors2Add sub that had
the exact same code as the above, and call it from the form_load procedure.

But it doesn't work the same. If I click the add button, all the boxes
clear. But when I call the subroutine, I'm on item 12 of 13. If I click the
move next button all the boxes clear and the text is in the right place. But
this causes other problems.

I don't understand why the subroutine doesn't produce the same results as
clicking the add button.

As an alternative, is there a way of calling click_1 from form_load that
will produce the same result as clicking the add button?

thanks.

dennist685
 
Hi Dennist,

Welcome to MSDN newsgroup. From your description, you are trying to
generate a DataAccess Form from an existing form through copying the
existed control and code into the new form, but encountered some strange
problem(such as eventhandler didn't be reconized correctly or your custom
sub function not work correclty ) ,yes?

AS for the first problem you mentioned,
=====================
For a bizarre reason, when I doubledclicked the add button, instead of the
add button code, I got a new private sub called Click_1. Mind, the
original
sub was there.
=====================

I think the possible cause of this is the button you copied from original
form to the new form lost its event handler wireup code ( which should be
placed in the form's InitializeComponents function ). Have you have a look
to see whether you've also copied those code?

In fact, when we double click a button at design-time, the VS.NET will auto
geneate a eventhandler fucnction and attach it to the control(button) in
the "InitializeComponents" Sub which is called in the form's constructor.
So , when you didn't copy this code, the Button's Click event dosn't have
an eventhandler associated with it, then when we double click it in
design-view, the IDE will try to create a new eventhandler funcdtion. Since
the Button_Click function already exists, a new one named Button_Click_1
will appear, do you think so?


As for your another problem on Adding New DataRow, Are you sure the code
you put in your custom sub funcdtion is the same in your AddNew button?
From my local test, I can get all the TextBoxes be set to empty (or the new
primary key) as long as I call the CurrencyManager.AddNew() method, I think
this is the keypoint. Have you also called this in your sub function?
Here is some of my test code period which makes all the binding textboxes
turn into "Add new" state,

==================
CurrencyManager cm = this.BindingContext[this.objUserDS.Tables[0]] as
CurrencyManager;

if(cm != null)
{
cm.AddNew();
}
=================

If you have anything unclear or any other findings, please feel free to
post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Thank you Steven,
I couldn't find an event handler in Authors2, but then I couldn't find an
event handler in the windows initialize component sub in the other forms that
work, either. Can you give me an example of where that code resides, and
what it looks like?

I'm sure the click_1 handler and the sub are identical.

First, the click_1 handler

Private Sub btnAdd_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
cm.AddNew()
bAdd = True
Me.cboAuthorType.SelectedIndex = 1
Me.cboContentRights.SelectedIndex = 1
Me.cboPointOfView.SelectedIndex = 1
Me.cboPrimaryPub.SelectedIndex = 1


Me.cboAuthorType.SelectedIndex = 0
Me.cboContentRights.SelectedIndex = 0
Me.cboPointOfView.SelectedIndex = 0
Me.cboPrimaryPub.SelectedIndex = 0
SetEditMode(True)
Me.dgrd.Enabled = False
End Sub

Then the sub

Private Sub Authors2Add()
MsgBox("In authors2add")
Me.cm.AddNew()
bAdd = True
Me.cboAuthorType.SelectedIndex = 1
Me.cboContentRights.SelectedIndex = 1
Me.cboPointOfView.SelectedIndex = 1
Me.cboPrimaryPub.SelectedIndex = 1


Me.cboAuthorType.SelectedIndex = 0
Me.cboContentRights.SelectedIndex = 0
Me.cboPointOfView.SelectedIndex = 0
Me.cboPrimaryPub.SelectedIndex = 0
SetEditMode(True)
Me.dgrd.Enabled = False
End Sub

I might add that if I don't use the Me keyword before cm the same thing
happens.

Thank you for your help.

dennist685

Steven Cheng said:
Hi Dennist,

Welcome to MSDN newsgroup. From your description, you are trying to
generate a DataAccess Form from an existing form through copying the
existed control and code into the new form, but encountered some strange
problem(such as eventhandler didn't be reconized correctly or your custom
sub function not work correclty ) ,yes?

AS for the first problem you mentioned,
=====================
For a bizarre reason, when I doubledclicked the add button, instead of the
add button code, I got a new private sub called Click_1. Mind, the
original
sub was there.
=====================

I think the possible cause of this is the button you copied from original
form to the new form lost its event handler wireup code ( which should be
placed in the form's InitializeComponents function ). Have you have a look
to see whether you've also copied those code?

In fact, when we double click a button at design-time, the VS.NET will auto
geneate a eventhandler fucnction and attach it to the control(button) in
the "InitializeComponents" Sub which is called in the form's constructor.
So , when you didn't copy this code, the Button's Click event dosn't have
an eventhandler associated with it, then when we double click it in
design-view, the IDE will try to create a new eventhandler funcdtion. Since
the Button_Click function already exists, a new one named Button_Click_1
will appear, do you think so?


As for your another problem on Adding New DataRow, Are you sure the code
you put in your custom sub funcdtion is the same in your AddNew button?
From my local test, I can get all the TextBoxes be set to empty (or the new
primary key) as long as I call the CurrencyManager.AddNew() method, I think
this is the keypoint. Have you also called this in your sub function?
Here is some of my test code period which makes all the binding textboxes
turn into "Add new" state,

==================
CurrencyManager cm = this.BindingContext[this.objUserDS.Tables[0]] as
CurrencyManager;

if(cm != null)
{
cm.AddNew();
}
=================

If you have anything unclear or any other findings, please feel free to
post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Hi Dennis,

A typical line of event wire up code should look like the following. This
example add this.button1_Click method to the button1.Click event hander.

this.button1.Click += new System.EventHandler(this.button1_Click);

When double click on the button at design time, the designer will create
another event handler if it detects that there is already a method named
button1_Click exists. I think this is the problem you have met. So in this
case, we can do the following to use the existing handlers.

1. Open the properties window.
2. Switch to events view.
3. Click on the button which you need to set events for in design view.
4. Find the Click event and select the existing method you need to wire up
in the dropdown list. The code will be added automatically in the
InitialComponents method.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thank you Steve and Kevin.

What I did in the end was copy the controls and underlying code from a
similar form to a new version of the target form. As in all other cases
except that one time, it worked perfectly.

dennist685
 
You're welcome, Dennis.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top