p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 (http://p2p.wrox.com/forumdisplay.php?f=409)
-   -   Form Data Will Not Display When I Click On Display Button (http://p2p.wrox.com/showthread.php?t=73199)

sprdave March 9th, 2009 10:07 PM

Form Data Will Not Display When I Click On Display Button
 
Hello,

I have followed the books instructions, and the Errata, and still cannot get any DATA to display from the "Mailing Label Program". I fill in all of the fields and click the "Display Button" The button changes and says "Mail Label" but no DATA is displayed in the textbox from the fields. What am I doing wrong???? Here is my code: Thanks, in advance

using System;
using System.Windows.Forms;

public class frmMain : Form
{
private Label label1;
private Label label2;
private Label label4;
private Label label5;
private TextBox txtName;
private TextBox txtAddress;
private TextBox txtCity;
private TextBox txtState;
private TextBox txtZip;
private Button btnDisplayOutput;
private Button btnExit;
private TextBox txtResult;
private Label label3;
#region Windows code
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.txtAddress = new System.Windows.Forms.TextBox();
this.txtCity = new System.Windows.Forms.TextBox();
this.txtState = new System.Windows.Forms.TextBox();
this.txtZip = new System.Windows.Forms.TextBox();
this.btnDisplayOutput = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.txtResult = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(63, 41);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(75, 20);
this.label1.TabIndex = 0;
this.label1.Text = "Name:";
//
// label2
//
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label2.Location = new System.Drawing.Point(63, 83);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(75, 20);
this.label2.TabIndex = 1;
this.label2.Text = "Address:";
//
// label3
//
this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label3.Location = new System.Drawing.Point(60, 130);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(75, 20);
this.label3.TabIndex = 2;
this.label3.Text = "City";
//
// label4
//
this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label4.Location = new System.Drawing.Point(258, 130);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 20);
this.label4.TabIndex = 3;
this.label4.Text = "State";
//
// label5
//
this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label5.Location = new System.Drawing.Point(376, 130);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(40, 20);
this.label5.TabIndex = 4;
this.label5.Text = "Zip";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(181, 40);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(411, 20);
this.txtName.TabIndex = 5;
//
// txtAddress
//
this.txtAddress.Location = new System.Drawing.Point(181, 82);
this.txtAddress.Name = "txtAddress";
this.txtAddress.Size = new System.Drawing.Size(411, 20);
this.txtAddress.TabIndex = 6;
//
// txtCity
//
this.txtCity.Location = new System.Drawing.Point(141, 130);
this.txtCity.Name = "txtCity";
this.txtCity.Size = new System.Drawing.Size(111, 20);
this.txtCity.TabIndex = 7;
//
// txtState
//
this.txtState.Location = new System.Drawing.Point(304, 130);
this.txtState.Name = "txtState";
this.txtState.Size = new System.Drawing.Size(54, 20);
this.txtState.TabIndex = 8;
//
// txtZip
//
this.txtZip.Location = new System.Drawing.Point(434, 129);
this.txtZip.Name = "txtZip";
this.txtZip.Size = new System.Drawing.Size(158, 20);
this.txtZip.TabIndex = 9;
//
// btnDisplayOutput
//
this.btnDisplayOutput.Location = new System.Drawing.Point(60, 217);
this.btnDisplayOutput.Name = "btnDisplayOutput";
this.btnDisplayOutput.Size = new System.Drawing.Size(75, 23);
this.btnDisplayOutput.TabIndex = 10;
this.btnDisplayOutput.Text = "&Display";
this.btnDisplayOutput.UseVisualStyleBackColor = true;
this.btnDisplayOutput.Click += new System.EventHandler(this.btnDisplayOutput_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(517, 217);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 23);
this.btnExit.TabIndex = 11;
this.btnExit.Text = "E&xit";
this.btnExit.UseVisualStyleBackColor = true;
//
// txtResult
//
this.txtResult.Location = new System.Drawing.Point(60, 262);
this.txtResult.Multiline = true;
this.txtResult.Name = "txtResult";
this.txtResult.ReadOnly = true;
this.txtResult.Size = new System.Drawing.Size(532, 194);
this.txtResult.TabIndex = 12;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(635, 468);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnDisplayOutput);
this.Controls.Add(this.txtZip);
this.Controls.Add(this.txtState);
this.Controls.Add(this.txtCity);
this.Controls.Add(this.txtAddress);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n;
this.Text = "Mailing Label Program";
this.Load += new System.EventHandler(this.frmMain_Load);
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

public frmMain()
{
InitializeComponent();
}

public static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}

private void frmMain_Load(object sender, EventArgs e)
{

}

private void btnDisplayOutput_Click(object sender, EventArgs e)
{
String buffer;

buffer = "Mailing Label:" + Environment.NewLine +
Environment.NewLine;
buffer = buffer + " Name: " + txtName.Text +
Environment.NewLine;
buffer = buffer + "Address: " + txtAddress.Text +
Environment.NewLine;
buffer = buffer + " City: " + txtCity.Text +
" State: " + txtState.Text +
" Zip: " + txtZip.Text;
btnDisplayOutput.Text = buffer;

}
}

dparsons March 9th, 2009 11:03 PM

This book is the hot topic tonight! Anyway, I am looking at the manuscript right now on Books 24x7 and the last line of the DisplayOutput click event handler is not:

btnDisplayOutput.Text = buffer;

but rather:

txtDisplayOutput.Text = buffer;

In the case of your code you would actually want to use txtResult.Text = buffer;

The Books 24 x 7 manuscript refers to the textbox as txtDisplayOutput throughout, however, if the manuscript that you have is calling it txtResult in one place and txtDisplayOutput in another could you please submit the issue to the errata for the book?

Let me know if you have any other questions.

hth
-Doug

sprdave March 9th, 2009 11:04 PM

Displays Now
 
Here is the fix if anyone else had the same problem:

I named the bottom "textbox" txtDisplayOutput in the properties.
Re-ran the example and now I have data displayed. I guess I did not understand what the Errata was trying to explain about this issue.

sprdave March 9th, 2009 11:07 PM

Doug says:
The Books 24 x 7 manuscript refers to the textbox as txtDisplayOutput throughout, however, if the manuscript that you have is calling it txtResult in one place and txtDisplayOutput in another could you please submit the issue to the errata for the book?

Doug - Will do!! I LOVE YOUR BOOK!!!!! I am finally understanding OOP!! Thanks for the fast reply!

dparsons March 9th, 2009 11:28 PM

Well thank you, I only wish it were MY book! ;] In any event, if you need any more help feel free to post.

-Doug

clay5050 March 10th, 2009 12:56 AM

resolved
 
I resolved it. Well, I went to a different computer and entered the excerise carefully. Its now working.... thanks everyone for your assistance.

DrPurdum March 12th, 2009 08:49 PM

Thanks, Doug, for helping this guy out. My computer has been dead for almost two weeks now and I'm writing this from Panera Bread! I'll try to check in more often when I can.

Dr. Purdum

dparsons March 12th, 2009 11:28 PM

No problem Jack. =]

scolty February 12th, 2011 03:53 PM

Same problem as above
 
Good evening, im also experiencing similar problems. I have, to the best of my knowledge managed to correct the code according to the Errata posts yet neither of my buttons (referring to the code, not myself) are working. Ive already copied the original posts code and scanned through it + couldnt see any differences other than i have made the alterations stated in the Errata.

If someone would be willing to have a look at this and point out my stupid mistake id appreciate it as ive already spent a good few hours starting from scratch a couple time.

Code:

using System;
using System.Windows.Forms;

public class frmMain : Form
{
    private Label label1;
    private Label label2;
    private Label label4;
    private Label label5;
    private TextBox txtCity;
    private TextBox txtName;
    private TextBox txtAddress;
    private TextBox txtState;
    private TextBox txtZip;
    private Button btnDisplayOutput;
    private Button btnExit;
    private TextBox txtDisplayOutput;
    private Label label3;
    #region Windows code
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.label4 = new System.Windows.Forms.Label();
        this.label5 = new System.Windows.Forms.Label();
        this.txtCity = new System.Windows.Forms.TextBox();
        this.txtName = new System.Windows.Forms.TextBox();
        this.txtAddress = new System.Windows.Forms.TextBox();
        this.txtState = new System.Windows.Forms.TextBox();
        this.txtZip = new System.Windows.Forms.TextBox();
        this.btnDisplayOutput = new System.Windows.Forms.Button();
        this.btnExit = new System.Windows.Forms.Button();
        this.txtDisplayOutput = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        //
        // label1
        //
        this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.label1.Location = new System.Drawing.Point(0, 0);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(75, 20);
        this.label1.TabIndex = 0;
        this.label1.Text = "Name:";
        //
        // label2
        //
        this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.label2.Location = new System.Drawing.Point(-3, 20);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(75, 20);
        this.label2.TabIndex = 1;
        this.label2.Text = "Address:";
        //
        // label3
        //
        this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.label3.Location = new System.Drawing.Point(-3, 40);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(75, 20);
        this.label3.TabIndex = 2;
        this.label3.Text = "City:";
        //
        // label4
        //
        this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.label4.Location = new System.Drawing.Point(-3, 60);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(40, 20);
        this.label4.TabIndex = 3;
        this.label4.Text = "State:";
        //
        // label5
        //
        this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.label5.Location = new System.Drawing.Point(-3, 80);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(40, 20);
        this.label5.TabIndex = 4;
        this.label5.Text = "Zip:";
        //
        // txtCity
        //
        this.txtCity.Location = new System.Drawing.Point(81, 40);
        this.txtCity.Name = "txtCity";
        this.txtCity.Size = new System.Drawing.Size(100, 20);
        this.txtCity.TabIndex = 7;
        //
        // txtName
        //
        this.txtName.Location = new System.Drawing.Point(81, 0);
        this.txtName.Name = "txtName";
        this.txtName.Size = new System.Drawing.Size(100, 20);
        this.txtName.TabIndex = 5;
        //
        // txtAddress
        //
        this.txtAddress.Location = new System.Drawing.Point(81, 20);
        this.txtAddress.Name = "txtAddress";
        this.txtAddress.Size = new System.Drawing.Size(100, 20);
        this.txtAddress.TabIndex = 6;
        //
        // txtState
        //
        this.txtState.Location = new System.Drawing.Point(46, 61);
        this.txtState.Name = "txtState";
        this.txtState.Size = new System.Drawing.Size(100, 20);
        this.txtState.TabIndex = 8;
        //
        // txtZip
        //
        this.txtZip.Location = new System.Drawing.Point(46, 79);
        this.txtZip.Name = "txtZip";
        this.txtZip.Size = new System.Drawing.Size(100, 20);
        this.txtZip.TabIndex = 9;
        //
        // btnDisplayOutput
        //
        this.btnDisplayOutput.Location = new System.Drawing.Point(0, 114);
        this.btnDisplayOutput.Name = "btnDisplayOutput";
        this.btnDisplayOutput.Size = new System.Drawing.Size(75, 23);
        this.btnDisplayOutput.TabIndex = 11;
        this.btnDisplayOutput.Text = "&Display";
        this.btnDisplayOutput.UseVisualStyleBackColor = true;
        //
        // btnExit
        //
        this.btnExit.Location = new System.Drawing.Point(120, 114);
        this.btnExit.Name = "btnExit";
        this.btnExit.Size = new System.Drawing.Size(75, 23);
        this.btnExit.TabIndex = 12;
        this.btnExit.Text = "E&xit";
        this.btnExit.UseVisualStyleBackColor = true;
        //
        // txtDisplayOutput
        //
        this.txtDisplayOutput.Location = new System.Drawing.Point(28, 174);
        this.txtDisplayOutput.Multiline = true;
        this.txtDisplayOutput.Name = "txtDisplayOutput";
        this.txtDisplayOutput.ReadOnly = true;
        this.txtDisplayOutput.Size = new System.Drawing.Size(202, 71);
        this.txtDisplayOutput.TabIndex = 13;
        //
        // frmMain
        //
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.txtDisplayOutput);
        this.Controls.Add(this.btnExit);
        this.Controls.Add(this.btnDisplayOutput);
        this.Controls.Add(this.txtZip);
        this.Controls.Add(this.txtState);
        this.Controls.Add(this.txtCity);
        this.Controls.Add(this.txtAddress);
        this.Controls.Add(this.txtName);
        this.Controls.Add(this.label5);
        this.Controls.Add(this.label4);
        this.Controls.Add(this.label3);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Name = "frmMain";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Mailing Label Program";
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    #endregion

    public frmMain()
    {
        InitializeComponent();
    }
    public static void Main()
    {
        frmMain main = new frmMain();
        Application.Run(main);
    }

    private void btnDisplayOutput_Click(object sender, EventArgs e)
    {
        String buffer;

buffer = "Mailing Label:" + Environment.NewLine + Environment.NewLine;
buffer = buffer + "  Name: " + txtName.Text + Environment.NewLine;
buffer = buffer + "Address: " + txtAddress.Text + Environment.NewLine;
        buffer = buffer + " City: " + txtCity.Text +
                " State: " + txtState.Text +
                " Zip: " + txtZip.Text;
        txtDisplayOutput.Text = buffer;
    }
    private void btnExit_Click(object sender, EventArgs e)
    {
        Close();
    }

   
}


DrPurdum February 12th, 2011 05:34 PM

Hi scolty:

While I can't be positive, I put your code into VS and it did not behave as advertised. I put a breakpoint in the button Click event...and NEVER reached the breakpoint. This suggests that you wrote the Click event code before placing the button on the form.

The easiest way to check for such things is to display the form in the Design mode, and double click the button in question. If you see something like:

btnDisplayOutput_Click_1()

(notice the underscore 1 at the end), that tells me that the button control was not tied to the code. When you look at your source code, you will likely see code for two Click events: 1) The one you wrote, and 2) the one the compiler wants to execute when the user clicks the button. Alas, the compiler will ignore your code and execute the click event that doesn't have any code!

The fix is easy. Copy all of the code between the opening and closing braces for the Click event to the clipboard (e.g., highlight the code, then Ctrl-X). Now, delete the button click event so there's no code tied to the button in the source code file. Now switch back to the Design mode. Double-click on the button. This causes the button Click event fragment to be written back into the souce file. Now, place your cursor between the braces and press Ctrl-V to insert your code back into the button Click event. Your program should run fine now.


All times are GMT -4. The time now is 03:22 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.