Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
---

If I call it as follows in form load:

CheckBox1_CheckedChanged(Me, Nothin)

Then sender.ToString will be: Project_Name.ProjectName.FormName

 

But if user manually click on the check box then sender.ToString will be: System.Windows.Forms.CheckBox, CheckState: 1/0

 

Now I want to do some thing if user clicked on the check box and do some thing if it was called on form load event.

 

I can do it this way:

In CheckBox1_CheckedChanged event I use sender.ToString and compare this string, to see how this event has been called!

 

But this does seem to be logical.

There should be a better way.

Edited by PlausiblyDamp
  • Administrators
Posted

You could alternatively check the sender's type or cast it to a control and check it's name.

 

If you want it to do two different things based on how it is called it might be easier to either handle the form load and the check changed event's with their own event handlers.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted

You can use VB's TypeOf ... Is operator, like so:


[Color=Blue]If TypeOf[/Color] sender [Color=Blue]Is[/Color] Form [Color=Blue]Then
[/Color] [Color=Green]' Do things and stuff[/Color]
[Color=Blue]Else If TypeOf[/Color] sender [Color=blue]Is[/Color] CheckBox [Color=Blue]Then[/Color]
[Color=Green]' Do other things and stuff[/Color]
[Color=Blue]End If[/Color]
[/Code]

I personally would just do a simple object comparison such as:

[Code]
[Color=Blue]If[/Color] sender [Color=Blue]Is Me Then
[/Color] [Color=Green]' Do things and stuff[/Color]
[Color=Blue]Else If[/Color] sender[Color=Blue] Is [/Color]CheckBox1 [Color=Blue]Then[/Color]
[Color=Green]' Do other things and stuff[/Color]
[Color=Blue]End If[/Color]
[/Code]

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...