Author Topic: Align One Form to another  (Read 11616 times)

0 Members and 1 Guest are viewing this topic.

jjs

  • Guest
Align One Form to another
« on: November 07, 2008, 02:47:45 PM »
I want the lefts and tops to match when loaded, and when main form is moved.

I get the error =
Error   3   The name 'frmInformation' does not exist in the current context   C:\Users\jimbob\Documents\Visual Studio 2005\Projects\MotorThemCS\MotorThemCS\FrmMotorThem.cs   2225   21   MotorThemCS
which applies to the alignforms routine


FrmMotorThem.cs
Code: [Select]
private void cmdInformation_Click(object sender, EventArgs e)
        {
            Form frmInformation = FrmInformation.Instance();
            this.AddOwnedForm(frmInformation);
            //frmInformation.Left = this.Left + this.Width;    <<<<<<----- this works
            //frmInformation.Top = this.Top;
            AlignForms();
            frmInformation.Show();
            frmInformation.Activate();
           
        }

        private void cmdOverloads_Click(object sender, EventArgs e)
        {
            Form frmOverload = FrmOverload.Instance();
            this.AddOwnedForm(frmOverload);
            //frmOverload.Left = this.Left + this.Width;
            //frmOverload.Top = this.Top;
            AlignForms();
            frmOverload.Show();
            frmOverload.Activate();
        }
private void FrmMotorThem_Move(object sender, EventArgs e)
        {

            AlignForms();
        }

        private void AlignForms()
        {
           

            if (frmInformation == null || frmOverload == null)
            {
                if (frmInformation != null)
                {
                    frmInformation.Left = this.Left + this.Width;
                    frmInformation.Top = this.Top;
                }

                if (frmOverload != null)
                {
                    frmOverload.Left = this.Left + this.Width;
                    frmOverload.Top = this.Top;
                }
               
            }
            else
            {
                frmOverload.Left = this.Left + this.Width;
                frmOverload.Top = this.Top;
                frmInformation.Left = this.Left + this.Width + frmOverload.Width;
                frmInformation.Top = this.Top;
            }
        }
FrmInformation.cs (FrmOverload would be the same)
Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MotorThemCS
{
    public partial class FrmInformation : Form
    {
        //**************
        private static FrmInformation frmInformation = null;
        public static FrmInformation Instance()
        {
            if (frmInformation == null)
            {
                frmInformation = new FrmInformation();
            }
            return frmInformation;
        }

        private FrmInformation()
        //**************
        //public FrmInformation()
        {
            InitializeComponent();
        }

  }
}

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Align One Form to another
« Reply #1 on: November 07, 2008, 03:09:37 PM »
I want the lefts and tops to match when loaded, and when main form is moved.

I get the error =
Error   3   The name 'frmInformation' does not exist in the current context   C:\Users\jimbob\Documents\Visual Studio 2005\Projects\MotorThemCS\MotorThemCS\FrmMotorThem.cs   2225   21   MotorThemCS
which applies to the alignforms routine


FrmMotorThem.cs
Code: [Select]
private void cmdInformation_Click(object sender, EventArgs e)
        {
            Form frmInformation = FrmInformation.Instance();   [color=red]<<<<<<<__Should the F in FrmInformation be capitalized???[/color]
            this.AddOwnedForm(frmInformation);
            //frmInformation.Left = this.Left + this.Width;    <<<<<<----- this works
            //frmInformation.Top = this.Top;
            AlignForms();
            frmInformation.Show();
            frmInformation.Activate();
           
        }




jjs

  • Guest
Re: Align One Form to another
« Reply #2 on: November 07, 2008, 03:47:43 PM »
yes, that part of the code works fine. the form name is with capital F.
the variable name that references the form has lowercase F.

TonyT

  • Guest
Re: Align One Form to another
« Reply #3 on: November 08, 2008, 04:45:59 AM »
yes, that part of the code works fine. the form name is with capital F.
the variable name that references the form has lowercase F.

You should probably consider some basic learning about the
programming language you're trying to use.

While the folks here are good natured and willing to help, the
mistakes in your code clearly indicate that you've skipped some
basics and prerequisites needed to use C# and you seem to be
expecting folks here to fill in the blanks for you.

Unlike LISP (which uses Dynamic Scoping), in C#, local variables
that are declared in a method are not visible from other methods
you call from it.



Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Align One Form to another
« Reply #4 on: November 08, 2008, 06:25:35 PM »
yes, that part of the code works fine. the form name is with capital F.
the variable name that references the form has lowercase F.

...

While the folks here are good natured and willing to help, the
mistakes in your code clearly indicate that you've skipped some
basics and prerequisites needed to use C# and you seem to be
expecting folks here to fill in the blanks for you.


....


Maybe it's more like a helping hand at solving a problem. I really don't think jjs expects anything.
TheSwamp.org  (serving the CAD community since 2003)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Align One Form to another
« Reply #5 on: November 08, 2008, 06:37:54 PM »

Mark,
With respect,
Tony did indicate the cause of the problem.

Code: [Select]
... in C#, local variables
that are declared in a method are not visible from other methods
you call from it.
which is intimated in the error description ..
Code: [Select]
Error   3   The name 'frmInformation' does not exist in the current context   

I'd also like to know what the first 2 errors are.


Sometimes a little tough love is required to jolt some people into doing a little study.




kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Align One Form to another
« Reply #6 on: November 08, 2008, 06:42:07 PM »
Without looking too hard at the code, as Tony pointed out you have a scoping problem.

You are creating the new forms in seperate event handlers/methods and these forms have no scope out side of the method they are defined and created in.
To fix this you need to declare the form var's outside of the methods so you can have access to them at a more 'global' like manner.
Something like -

class TheApp()
{
// declare them outside of the methods to manipulate them
private Form frm1;
private Form frm2;

public void methodToCreateFrm1()
{
// define frm1
frm1 = new Form();
}

public void methodToCreateFrm2()
{
// define frm2
frm2 = new Form();
}

//once both forms are created you can test for this and align them
void AlignForms()
{
// check for forms first and now do it;
}


hope that helps to understand it a bit better.

"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

TonyT

  • Guest
Re: Align One Form to another
« Reply #7 on: November 09, 2008, 01:27:24 AM »

Maybe it's more like a helping hand at solving a problem. I really don't think jjs expects anything.

You seem to have confused the problem with one of its symptoms.

An error that is a result of not understanding the most basic
aspects of a programming language (which can be self-learned
with a decent a book, and/or a variety of free online resources)
is not the 'problem', it is merely a symptom of the problem.

« Last Edit: November 09, 2008, 11:17:29 AM by TonyT »

jjs

  • Guest
Re: Align One Form to another
« Reply #8 on: November 13, 2008, 10:08:38 AM »
Tony,
I had tried to put the dims in the main program and it seemed to give me the same error. I figured it was a scoping issue, but moving the item to program.cs file did not help. So then I figured I was wrong about it being a scoping problem.

I have a book on C#. I have read it. It went through scoping, but it does not get into specifics about where exactly stuff needs to be placed. VB6 used to take care of a lot of that stuff automatically.

I also searched the web before I posted.

In VB6 this was simple. You just called the form.left value and added the form.width value. Make the .TOP value the same and you were done.


Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Align One Form to another
« Reply #9 on: November 13, 2008, 10:22:24 AM »
*is disgusted*

sinc

  • Guest
Re: Align One Form to another
« Reply #10 on: November 13, 2008, 10:32:49 AM »
I figured it was a scoping issue, but moving the item to program.cs file did not help. So then I figured I was wrong about it being a scoping problem.

Do you understand now how it's a scoping problem?  Did MickD's post help clarify the issue?

If you missed it, go back and go over MickD's post.

If you think you understand MickD's post, then we'll start confusing you all over again by telling you about garbage collection...   :-)

Glenn R

  • Guest
Re: Align One Form to another
« Reply #11 on: November 13, 2008, 10:46:05 AM »
Mick,

Your avatar/piccy looks suspiciously like the old ABC broadcast logo...or was it when the channel 'went to sleep'...haven't seen that for many years......

jjs

  • Guest
Re: Align One Form to another
« Reply #12 on: November 13, 2008, 11:26:00 AM »
see the comments in red below
Without looking too hard at the code, as Tony pointed out you have a scoping problem.

You are creating the new forms in seperate event handlers/methods and these forms have no scope out side of the method they are defined and created in.
To fix this you need to declare the form var's outside of the methods so you can have access to them at a more 'global' like manner.
Something like -

class TheApp() <<--So I should dim them outside the form? If I do that as a private, tho, then they won't be calleable in the form will they? Scoping issue?
{
// declare them outside of the methods to manipulate them
private Form frm1;  <<--Do I want them dimmed as generic Form type or as the specific form they are suppose to be?
private Form frm2;

public void methodToCreateFrm1()
{
// define frm1
frm1 = new Form(); <<--Do I want them dimmed as generic Form type or as the specific form they are suppose to be?
}

public void methodToCreateFrm2()
{
// define frm2
frm2 = new Form(); <<--Do I want them dimmed as generic Form type or as the specific form they are suppose to be?
}

//once both forms are created you can test for this and align them
void AlignForms()
{
// check for forms first and now do it;
}


hope that helps to understand it a bit better.



jjs

  • Guest
Re: Align One Form to another
« Reply #13 on: November 13, 2008, 12:21:10 PM »
I think I have something working. Will post in a few.

TonyT

  • Guest
Re: Align One Form to another
« Reply #14 on: November 13, 2008, 12:49:20 PM »
Tony,
I had tried to put the dims in the main program and it seemed to give me the same error. I figured it was a scoping issue, but moving the item to program.cs file did not help. So then I figured I was wrong about it being a scoping problem.

I have a book on C#. I have read it. It went through scoping, but it does not get into specifics about where exactly stuff needs to be placed. VB6 used to take care of a lot of that stuff automatically.

I also searched the web before I posted.

In VB6 this was simple. You just called the form.left value and added the form.width value. Make the .TOP value the same and you were done.


I think you're getting ahead of yourself by taking on a project that
requires a bit more familiarity with the tools than you have.

This usually happens when a newbie goes looking for code to solve a
problem, finds something and tries to use it, without understanding it
or understanding how it works.

The problem you had in your AlignForms() should be obvious.
You are using undefined variables (the two form variables).

To access the singleton forms, you have to do the same thing
you did in the other methods (call the Instance() method).

Code: [Select]

       private void AlignForms()
       {
            FrmInformation.Instance().Left = this.left;
            FrmInformation.Instance().Top = this.top;
       }



The problem is that the cause of the error should be completely
obvious and you shouldn't have gotten stuck on that if you had
a reasonable understanding of the language.

Again, my advice is to put down the attempt to write real software
and spend time learning the basics first. While folks here are glad
to lend a helping hand along the way, they're not going to tutor
you on C#, and that's basically what this thread is becoming.