Jump to content
  • Advertisement
Sign in to follow this  
phil05

[.net] need help with C#

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Stick a property (e.g. Title) to the frmPrompt class.
Use ShowDialog() instead of Show()
After ShowDialog returns, read the property.

Cheers

Share this post


Link to post
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 this post


Link to post
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 class
public string PromptLabel {
get { return lblPrompt; }
set { lblPrompt = value; }
}

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!