Sign in to follow this  
phil05

[.net] need help with C#

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
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
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
Probably the easiest thing to do is send in the instance of the form.

like:

Form2 myNewForm = new Form2(this);

where in Form2 you have a new constructor...


public Form2(Form1 f)
{
if(f.TextBox_Text != "The password")
this.Close();
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this