• 13
• 27
• 9
• 9
• 20

[.net] need help with C#

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

With C#, I came across one of my most struggles with the language. Because it's OO, I was clicking on a button named "btnNewBook." It opens a new form which acts like a Prompt Box that you would see in VB.NET. I would enter the title of the new book. When I click on btnOK, it should send the information back to the other form so I can store it as a string. So, I'm guessing its the classes that aren't communicating with each other. If that made any sense, it'd be great to get your help if you're familiar with .NET. Plus asking this may solve my biggest OO issue lol. Thanks, Phil Current odd code I'm working with...
        private void btnBookNew_Click(object sender, EventArgs e)
{
string sNewBook;

// user wants to create a new book. Get a Message Prompt
// asking for the name. Then add it to the cb book collection.
using (frmPrompt newBookPrompt = new frmPrompt())
{
newBookPrompt.Show();

}
}



Share on other sites
Stick a property (e.g. Title) to the frmPrompt class.
After ShowDialog returns, read the property.

Cheers

Share on other sites
you could look up events, or initialise the form with a reference to the other form / class that it needs to update.

Share on other sites
Okay, I'm trying this but came across an old problem. I can't access my controls in the form I'm calling. Here's the code.. why is this?

        private void btnBookNew_Click(object sender, EventArgs e)        {            string sNewBook;            // user wants to create a new book. Get a Message Prompt             // asking for the name. Then add it to the cb book collection.                        using(frmPrompt newBookPrompt = new frmPrompt())            {                newBookPrompt.Text = "New Book";                newBookPrompt. // can't access lblPrompt to add a new question                newBookPrompt.ShowDialog();            }        }

Share on other sites
Most likely it is because lblPrompt is a private member of frmPrompt. Create a public property for it like so:

// In the frmPrompt classpublic string PromptLabel {    get { return lblPrompt; }  set { lblPrompt = value; }}

Share on other sites
Sounds familiar with what I was thinking. I went to the form design generated code and I switched the private lblPrompt to public. I now have access to it. Thanks.

Share on other sites
Next, I want to click my btnOkay using the current code above. Always something new to learn lol. Here's the current code. No compile errors but it doesn't do anything during runtime. How do I connect this with btnOkay?

 newBookPrompt.btnOkay.DialogResult = DialogResult.OK;        newBookPrompt.ShowDialog();  if (newBookPrompt.btnOkay.DialogResult == DialogResult.OK)         MessageBox.Show("Got it");

Hitting okay and cancel result in the same "Got it" message box.

[Edited by - philvaira on February 16, 2005 3:37:45 AM]

Share on other sites
Quote:
 Sounds familiar with what I was thinking. I went to the form design generated code and I switched the private lblPrompt to public. I now have access to it. Thanks.

This is the worst way to do it. That this is a label (lbl prefix? RTFM!) is an implementation detail.

Provide access through a property, do not expose implementation details.

Share on other sites
If I am reading you correctly you want a form that prompts the user for input and then provides the details to the base form.

Well it is quite simple really. Provide a property control for any detail the base form requires. Example: You want the user to input the name of a book and hit Ok or Cancel. If your text box on the prompting form is named txtBookName provide a property like this:

public string Book_Name{ get{return txtBookName.Text;} }

The denies direct access to the control itself, very important to incorporate properties rather than making members public.

Now what about the Ok / Cancel deal? Well when the buttons are click simply set the DialogResult of the form (this.DialogResult) to Ok or Cancel. Then call the form's close method.

private void btnOk(object sender, EventArgs e){    this.DialogResult = DialogResult.Ok;    this.Close();}private void btnCancel(object sender, EventArgs e){    this.DialogResult = DialogResult.Cancel;    this.Close();}

I hope this helps.

Share on other sites
Thanks, that helped me out quite a bit. I can now send information to another form with ease. But if form1 sends info to form2, how can form2 send info back to form1? I wouldn't want to instantiate a new form1 object because info wouldn't show up on my current form, obviously. Know how to do this?

[Edited by - philvaira on February 16, 2005 3:35:58 PM]