[.net] need help with C#

Started by
9 comments, last by Krisc 19 years, 2 months ago
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();
                
                

            } 
        } 

Advertisement
Stick a property (e.g. Title) to the frmPrompt class.
Use ShowDialog() instead of Show()
After ShowDialog returns, read the property.

Cheers
you could look up events, or initialise the form with a reference to the other form / class that it needs to update.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
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();            }        } 
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; }}
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.
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]
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.
RegardsThomas TomiczekTHONA Consulting Ltd.(Microsoft MVP C#/.NET)
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.
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]

This topic is closed to new replies.

Advertisement