ante011
ante011

Reputation: 99

Boolean in another class, return it's value

I've got a problem with my bool and windowsforms.

I have some radiobuttons and textboxes. If they are not filled I want to return false. These statements I want to have inside my control.cs and I want to call my control.cs when I press my button (createProject). If I get a false statement I want to show a messagebox that tells the user to fill them out.

I'm trying to understand how to use different classes and now I would like to put a evaluation in there and return a true or false back to my Form1.cs.
At the moment it wont return anything.

Should I also move control objControl = new control(); outside my createProject_Click?

I have 2 files:

Form1.cs looks like below:

public void createProject_Click(object sender, EventArgs e)
{
    sChoosedOfferPath = comboBoxOfferPath.Text;

    control objControl = new control();

    if (objControl.controlProject())
    {
        MessageBox.Show("ths is true");
    }
}

My control.cs looks like below:

public class control : Form1
{

    public bool controlProject()
    {
        //See if the user have choosen a radiobutton
        if (rBtn101.Checked == false && rBtn102.Checked == false && rBtn103.Checked == false && rBtn104.Checked == false && rBtn206.Checked == false && rBtn306.Checked == false)
        {
            return false;
        }

        //See if the user has filled all textboxes
        if ((String.IsNullOrWhiteSpace(textBoxProjectname.Text)) || (String.IsNullOrWhiteSpace(textBoxClient.Text)) || (String.IsNullOrWhiteSpace(textBoxProjectnr.Text)))
        {
            return false;
        }

        return true;
    }
}

Inside Form1.Designer.cs I've made all the textboxes and radiobuttons public.

Upvotes: 0

Views: 1701

Answers (3)

LoL
LoL

Reputation: 172

Since the question was raised by somehow a newbie programmer, I'd like to guess the meaning of the question in a different way (which might be wrong).

In the first piece of code the Form initiated a new instance of the Control class, which inherits from Form1.

In the second piece of code, it seemed to me that the programmer wants to retrieve the state of the buttons and etc. from that the instance of Form that has created the controller object.

There're some comments made to the controller class but unfortunately it was not in English and I have no way to understand it.

Back to the main problem, because you're inheriting the Form, all states that you access in the controller is just like "Another World", where they are all now independent from the Form that you are clicking on the screen. (Actually you could call to show that form up, and you could see two separate forms)

If you want to access the state of the Original Form that you are clicking, you should pass that instance to the controller class instead of inheriting from it.

I am new to Stack Overflow and I'm trying my best to get a code snippet to indicate my thought. I'll edit it somehow later and I'm sorry for that.

Edit: Please give the following code a try. Thank you.

public class Control
{
  private Form1 mainForm;
  public Control(Form1 form)
  {
    mainForm = form;
  }
  public bool ControlProject()
  {
    if (mainForm.rBtn101.Checked == false && mainForm.rBtn102.Checked == false && mainForm.rBtn103.Checked == false && mainForm.rBtn104.Checked == false && mainForm.rBtn206.Checked == false && mainForm.rBtn306.Checked == false)
      return false;
    if ((String.IsNullOrWhiteSpace(mainForm.textBoxProjectname.Text)) || (String.IsNullOrWhiteSpace(mainForm.textBoxClient.Text)) || (String.IsNullOrWhiteSpace(mainForm.textBoxProjectnr.Text))
      return false;
    return true;
  }
}

Upvotes: 2

Paddy
Paddy

Reputation: 33857

OK, there are a few issues with your code:

public void createProject_Click(object sender, EventArgs e)
{
    sChoosedOfferPath = comboBoxOfferPath.Text;

    control objControl = new control();

    if (objControl.controlProject())
    {
        MessageBox.Show("ths is true");
    }
}
  1. You instantiate a new instance of your control each time you hit this button - the new instance will have whatever the default values are. I assume that this is not your intention and instead you should reference the instance of the control already on your page.

  2. You haven't stated if this is winforms or asp.net - if asp.net messagebox.show is not the way to go here, as it will be shown on the server!

Upvotes: 0

slugster
slugster

Reputation: 49974

If i get a false statement i want to show a messagebox that tells the user to fill them out.

I assume this is the messagebox part inside the createProject_Click() method on Form1. Based on your description that I quoted it looks like you have your test the wrong way round, you need to test for not true to show the messagebox:

if (!objControl.controlProject())
{
    MessageBox.Show("You need to fill out the controls");
}

Upvotes: 1

Related Questions