Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6767565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:57:57+00:00 2026-05-26T14:57:57+00:00

I’ve got this code that uses winforms to get data passed between two forms

  • 0

I’ve got this code that uses winforms to get data passed between two forms (LibraryBookDialog.cs and MainForm.cs).

Here is the code for LibraryBookDialog.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace COMP2614HomeLab08
{
    public partial class LibraryBookDialog : Form
    {
        private LibraryBook book;

        public LibraryBook Book
        {
            get
            {
                if (book == null)
                {
                    book = new LibraryBook();
                }
                return book;
            }
            set { book = value; }
        }

        public LibraryBookDialog()
        {
            InitializeComponent();
        }

        private bool validateData()
        {
            // code that validates user input data
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (validateData())
            {
                try
                {
                    LibraryBook book = new LibraryBook();
                    book.Title = textBoxTitle.Text; 
                    book.Author = textBoxAuthor.Text;
                    book.CopyrightYear = Convert.ToInt32(textBoxCopyrightYear.Text);
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "There was an error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}

Here is the code for MainForm.cs:

namespace COMP2614HomeLab08
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            LibraryBookDialog dlg = new LibraryBookDialog();
            dlg.ShowDialog();

            if (dlg.DialogResult == DialogResult.OK)
            {
                listBoxLibraryBooks.Items.Add(dlg.Book);
            }
            dlg.Dispose();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            listBoxLibraryBooks.DisplayMember = "Title";
        }
    }
}

The QUESTION: Why is it that when I add the LibraryBook book object to the listBox, I get a blank element. I mean, it is there, the element in the listBox exists, but I am not sure if the data has been passed from form to form. Why is this so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:57:58+00:00Added an answer on May 26, 2026 at 2:57 pm

    You are using Book property of dialog but you’re not setting it with in the book dialog and your property getter returns a new instance if it is null. That is why you’re getting a blank entry.

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (validateData())
            {
                try
                {
                    ----> LibraryBook book = new LibraryBook(); <----- this a private book of this method. 
                    book.Title = textBoxTitle.Text; 
                    book.Author = textBoxAuthor.Text;
                    book.CopyrightYear = Convert.ToInt32(textBoxCopyrightYear.Text);
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "There was an error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    

    Either change the book declaration to

    this.book = new LibraryBook();
    

    Or at the end of creating the book set your class member book to this like so

    this.book = book;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.