7 Replies - 381 Views - Last Post: 28 September 2012 - 02:58 PM Rate Topic: -----

#1 risky14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 12-September 12

how to use one form instead of 30 to save memory?

Posted 28 September 2012 - 04:18 AM

Hello people

I need to use form2 to hold different text in the message box because I don't want to use a form for each button I have otherwise the application will have too much memory (please correct me if I'm wrong).

I need the form to display a MessageBox of text for each town of a city. The user clicks on a town which leads to form2. Form2 then has a button for "info" which opens up a messagebox of text. It will have other functions but I want to learn how to do this one first.

below i've got 2 buttons for 2 towns - lr is "La Reno" and ca is "carrera"

private void lr_Click(object sender, EventArgs e)
        {
            Form2 secondForm = new Form2();

            secondForm.Show();
        }

private void btnCa_Click(object sender, EventArgs e)
        {
            Form2 secondForm = new Form2();

            secondForm.Show();
        }


lr displays the same messagebox as ca when the info button is clicked on form2. I'd like lr to display different text in the messagebox whilst still being able to use the same form. If this isn't possible what's the alternative?

This post has been edited by Curtis Rutland: 28 September 2012 - 08:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: how to use one form instead of 30 to save memory?

#2 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 376
  • Joined: 04-January 11

Re: how to use one form instead of 30 to save memory?

Posted 28 September 2012 - 05:01 AM

If your forms have the same characteristics like having a label that displays a message, images that displays something, a button that does a certain process, but the design is similar or may exactly be the same, why don't you use just one form and just manipulate dynamically the display and the jobs they do?

Pass parameters between forms and do a switch case or an if else statements to determine which to display and to do.

There's a good tutoral by eclipsed4utoo about passing parameters between forms.

http://www.dreaminco...-forms-in-c%23/
Was This Post Helpful? 0
  • +
  • -

#3 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: how to use one form instead of 30 to save memory?

Posted 28 September 2012 - 05:01 AM

If I might ask, what is the end goal of the application?
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: how to use one form instead of 30 to save memory?

Posted 28 September 2012 - 05:01 AM

Sure this is possible. One way to do this is to have a method on Form2, let's call it SetTown, which is called from Form1.

public class Form2 : Form
{
  public void SetTown(string townName)
  {
    if (townName == "Ca") // carrera
      // do something, like set some text
    else if (townName = "lr") // La Reno
      // again, do something
  }
}



and in your Form1 button click:

private void btnCa_Click(object sender, EventArgs e)
{
  Form2 secondForm = new Form2();
  secondForm.SetTown("Ca");
  secondForm.Show();
}


Was This Post Helpful? 0
  • +
  • -

#5 risky14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 12-September 12

Re: how to use one form instead of 30 to save memory?

Posted 28 September 2012 - 11:37 AM

View Postjanne_panne, on 28 September 2012 - 05:01 AM, said:

Sure this is possible. One way to do this is to have a method on Form2, let's call it SetTown, which is called from Form1.

public class Form2 : Form
{
  public void SetTown(string townName)
  {
    if (townName == "Ca") // carrera
      // do something, like set some text
    else if (townName = "lr") // La Reno
      // again, do something
  }
}



and in your Form1 button click:

private void btnCa_Click(object sender, EventArgs e)
{
  Form2 secondForm = new Form2();
  secondForm.SetTown("Ca");
  secondForm.Show();
}



I gave this a go however in form1 "SetTown" has a red squiggly line under it. What do I need to add in form 1 to make SetTown recognisable?
Was This Post Helpful? 0
  • +
  • -

#6 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: how to use one form instead of 30 to save memory?

Posted 28 September 2012 - 12:10 PM

did you create the method SetTown in form 2?
Was This Post Helpful? 0
  • +
  • -

#7 risky14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 12-September 12

Re: how to use one form instead of 30 to save memory?

Posted 28 September 2012 - 12:32 PM

View PostBigR1983, on 28 September 2012 - 12:10 PM, said:

did you create the method SetTown in form 2?


I did yes and that was all fine no squiggly lines

I've been doing some research and what I really need (I think) is a switch statement for each town but all I see is switch statements for integers. Is there a way I can use switch statements for the message box i.e displaying the info associated with the town they clicked on.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: how to use one form instead of 30 to save memory?

Posted 28 September 2012 - 02:58 PM

View Postrisky14, on 28 September 2012 - 12:37 PM, said:

I need to use form2 to hold different text in the message box because ...

I gave this a go however in form1 "SetTown" has a red squiggly line under it.


It seems obvious by this line alone that you have not done any lessons or learning. No understanding of using variables to hold information. No understanding of Visual Studio. Maybe not even built a "Hello World" application yet. But are expecting to just copy/paste together a lot of disparate code you find on the net into a working application. That's just plain insulting to those of us that do this for a living.

Stop. There are 6 billion people out there and maybe 200 experts here. We can't hold your hand through your education because you want to jump into writing a program from scratch before you have taken the time to learn anything. You can't learn to build a house by just banging nails into lumber and think that somehow by magic a house will come out of it.

Go buy a couple "Learn C# in 30 days" type books. Work them from cover to cover. DO NOT TRY TO ARCHITECT A PROGRAM. Just do the lessons and learn. Do not move to chapter 2 unless you FULLY COMPREHEND chapter 1.

You'll get there. You'll learn. You'll understand. And you'll become a decent coder if you apply yourself. But you have to accept that any skill is learned before it is applied. We don't take people off the street and give them guns: We send them to police training first. We don't give a 16 year car keys and say "go figure it out": We teach them and they gain skills slowly. You have to accept that first you do lessons and learn the C# language and .NET namespace. Then you learn O.O. concepts. Then you start building little things. You'll get there, but you aren't ready for that yet.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1