|
-
May 8th, 2006, 11:56 AM
#1
Thread Starter
Junior Member
[2.0] WebPages in C#
I am working on a Windows Application in C# using VS 2005
In my application when I click the Show Salary button from a MenuBar
It adds a new Tab Page called Salary and updates the Salary Info.
I have two text boxes which takes the name.
I am using Event Handlers to handle the events.
Question:
When I click the Show Salary from the menu. Everything works fine, the Salary information is updated.
But when I change the person's name and click on the Lookup Button.
Error:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'System.Windows.Forms.Button' to type 'System.Windows.Forms.TabPage'."
Source="WindowsApplication1"
StackTrace:
at WindowsApplication1.Form1.UpdateSalaryTabPage(Object sender, EventArgs e) in C:\Documents and Settings\sramakrishna\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs:line 175
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsApplication1.Program.Main() in C:\Documents and Settings\sramakrishna\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
This is my code:
-----------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private Employees employees;
private Employee employee;
//TabPage tabpg;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//hookup common validation event handler for text boxes:
this.txtFirstName.Validating += new CancelEventHandler(this.nonEmpty_Validating);
this.txtLastName.Validating += new CancelEventHandler(this.nonEmpty_Validating);
/* hookup event handlers for Lookup click, which also triggers updates of common tab pages
**
** NOTE: order here is important, since called in this order --- and we want cmdLookup to run first & do
** the lookup before we start updating the tab pages.
*/
this.btnLookUp.Click += new EventHandler(this.btnLookUp_Click);
this.btnLookUp.Click += new EventHandler(this.UpdateContactInfoTabPage );
this.btnLookUp.Click += new EventHandler(this.UpdateHomeAddrTabPage);
this.employees = new Employees();
}
//General validation method for textboxes that require a non-empty string.
private void nonEmpty_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
TextBox txt;
txt = ((TextBox)(sender));
if (txt.Text == string.Empty )
{
MessageBox.Show("Please enter a value...");
e.Cancel = true;
return;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
//
//Called to dynamically show/hide the "Salary" tab page
//
private void showSalaryToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.showSalaryToolStripMenuItem.Text.StartsWith("Show"))
{
//Show Salary tab, which we create on the fly
//
TabPage tabpg;
tabpg = new TabPage("Salary");
tabpg.Name = "tabpgSalary";
tabpg.ForeColor = Color.Red;
Label lbl;
lbl = new Label();
lbl.Text = "Salary:";
lbl.Location = new Point(30, 40);
TextBox txt;
txt = new TextBox();
txt.Name = "txtSalary";
txt.Location = new Point(100, 37);
txt.ReadOnly = true;
txt.Size = new Size(200, 26);
tabpg.Controls.Add(txt);
tabpg.Controls.Add(lbl);
this.tabctrlEmployeeInfo.TabPages.Add(tabpg);
if (this.employee != null)
{
//we have an employee being displayed, show their salary
txt.DataBindings.Add("Text", this.employee, "FormattedSalary");
}
this.showSalaryToolStripMenuItem.Text = "Hide Salary"; //flip menu text
//now that tab is visible, add event handler to update when a lookup is performed
this.btnLookUp.Click += new EventHandler(this.UpdateSalaryTabPage);
// but update tab to reflect any lookup we've already done
//this.UpdateSalaryTabPage(null, null);
}
else
{
//
//Hide Salary tab by discarding what we created earlier...
//
this.tabctrlEmployeeInfo.TabPages.RemoveAt(2);
this.showSalaryToolStripMenuItem.Text = "Show Salary"; //flip menu text
//remove event handler to update tab page since page is now gone!
this.btnLookUp.Click -= new EventHandler(this.UpdateSalaryTabPage);
}
}
/**
** These methods are triggered when the user looks up an employee, which will in turn require updates
** on the various tab pages
**/
private void btnLookUp_Click(object sender, EventArgs e)
{
//Lookup new Employees...
this.employee = this.employees.Lookup(this.txtFirstName.Text, this.txtLastName.Text);
//If found, display his/her info, otherwise display an error message...
if (this.employee == null)
{
MessageBox.Show("Employee not found...");
this.txtFirstName.SelectAll();
this.txtFirstName.Focus();
}
else
{
//** nothing, updates are triggered as part of event processing...
}
}
private void UpdateContactInfoTabPage(object sender, System.EventArgs e)
{
this.txtEmail.DataBindings.Clear();
this.txtEmail.Clear();
this.txtCellPhone.DataBindings.Clear();
this.txtCellPhone.Clear();
if (this.employee == null)
{
return;
}
this.txtEmail.DataBindings.Add("Text", this.employee, "Email");
this.txtCellPhone.DataBindings.Add("Text", this.employee, "CellPhone");
}
private void UpdateHomeAddrTabPage(object sender, System.EventArgs e)
{
this.txtCity.DataBindings.Clear();
this.txtCity.Clear();
this.txtState.DataBindings.Clear();
this.txtState.Clear();
if (this.employee == null)
{
return;
}
this.txtCity.DataBindings.Add("Text", this.employee, "City");
this.txtState.DataBindings.Add("Text", this.employee, "State");
}
private void UpdateSalaryTabPage(object sender, System.EventArgs e)
{
TabPage tabpg = (TabPage) sender ;
this.tabctrlEmployeeInfo.TabPages["tabpgSalary"].Equals(tabpg);
tabpg.Controls["txtSalary"].DataBindings.Clear();
((TextBox)(tabpg.Controls["txtSalary"])).Clear();
if (this.employee == null)
{
return;
}
tabpg.Controls["txtSalary"].DataBindings.Add("Text", this.employee, "FormattedSalary");
}
}
}
--------------------------------------------------------
-
May 8th, 2006, 04:42 PM
#2
Fanatic Member
Re: [2.0] WebPages in C#
Somehow you have linked a button's click event to the UpdateSalaryTabPage method, but that method only expects TabPages to use it.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|