Jump to content

Archived

This topic is now archived and is closed to further replies.

TheOnlyDroid

[C#] Simple Form (Follow)

Recommended Posts

While thinking of ways to update my Portfolio (just because I needed to clean it up and add some new things), I thought to myself "What about 'Child' forms following a 'Parent' forum when it moves regardless of its width, left or right"..... Granted getting the 'right' side position correct was more of a hustle than getting the left position to work correctly.

 

So lets get into creating a 'Child' form that will follow a parent forum around regardless of its size, now if you're new to C# this isn't the most complex thing in the world to do, nor is it that advanced, going onto the mathematical side of things, it gets a little harder to read and under-stand if you're new, but frankly if I can call up my dad and a few friends and ask them to help me get the calculation right, you're going to be able to understand it.

 

 

Note: I'm not going to cover the basics of Visual Studio 2015/2014, you should know how to use that already, if not google some tutorials.

Note #2: This entirely depends on having two or more 'Forms' (the graphical interface that you place your 'components' on, and so on).

 

 

 

Task One:

 

In your primary form 'Parent Form' (Called 'Main' on creation [or form1]), we first need to declare the two (or one) extra forums that will act like its Child, allowing us to use "This" in its functions... In my case my second form is called 'Secondary' and my third one is called 'Tertiary' for obvious reasons.

 

Secondary Secondary;
Tertiary Tertiary;

 

After declaring the two or more 'Forms' within your main 'Parent' form we need to go into the function that Initializer's everything on startup, this is usually 'public {Name}()' and in my case as my 'Parent' form is called 'Primary' its 'public Primary()', once you locate that function you need to add the following below 'InitializeComponent' (if you don't have one it doesn't matter, that's usually because you've removed it).

 

Secondary = new Secondary(this);
Tertiary = new Tertiary(this);

Secondary.Show();
Tertiary.Show();

 

Now if you see some errors, don't worry that's because we have yet to do one more thing in both the Child Forms (or the other, if you only have one).. the following allows us to reference 'this' (IE: The parent form), in turn allowing us to allot more when it comes to size, position ect.... EG: 'Secondary.MaximumSize = new Size(this.Height, this.Width);'

 

Assuming you've gone to one of the Child form's code portion (accessible from the designer view by pressing F7, or right clicking it in the Solution Explorer and clicking 'View Code'); Once you're looking at the code you need to find the following 'public partial class {Name} : Form' and right under than we need to declare the 'Parent' forum, this will become clear in a second.

 

Primary f;

 

Once you've done this, find the following function 'public {name}()', and add the following within the brackets '( )', then we've only got a few more things to do after that.

Primary MA

 

Okay moving onto the final steps related to the Child forms, and you'll never have to touch the code again. Right under the InitializeComponent() call in the 'Child' form you have modified, we need to change the value of 'F' by doing the following. (Note: the variable 'F' and 'MA' can by anything of your choice, I named them like this for simplicity)

 

f = MA;

 

Moving back to the 'Parent' form, we need to reference a 'Move' listener, so we can update the position of the child form when it happens, this also acts as a way for the Child form to be in the correct place on startup, so you get two for the price of one. (If you're not sure on how to do this within Visual Studio, look at this image: http://prntscr.com/8dzss4 - Double clicking the empty space next to 'Move' will automatically create the call for you).

 

private void Primary_Move(object sender, EventArgs e)
{
    ChangeChildPosition(this, Secondary, 0, 1);
    ChangeChildPosition(this, Tertiary, Secondary.Width, 1);
}

 

Now we'll add the code that allows us to move the Child form, you could place this anywhere you'd like (the math portion I mean), but for my ease I created a little function that will do it for me, as most people here know; I like to cut a few corners while doing things to make it a little more simplistic for my sake..

 

       public void ChangeChildPosition(Form Parent, Form ToMove, int Offset = 0, int type = 1)
       {
           switch (type)
           {
               case 1:
                   ToMove.Location = new Point(((Parent.Width + Parent.Location.X) - Parent.Width - ToMove.Width - Offset), Parent.Location.Y);
                   break;
               case 2:
                   ToMove.Location = new Point((((Parent.Width + Parent.Location.X) - Parent.Width - ToMove.Width) + Parent.Width + ToMove.Width + Offset), Parent.Location.Y);
                   break;
           }
       }

 

Edited to work with C# WPF (Everything else can remain the same).

 

       public void ChangeChildPosition(Window Parent, Window ToMove, int Offset = 0, int type = 1)
       {
           switch (type)
           {
               case 1:
                   ToMove.Left = ((Parent.Width + Parent.Left) - Parent.Width - ToMove.Width - Offset);
                   ToMove.Top = Parent.Top;

                   break;
               case 2:
                   ToMove.Left = (((Parent.Width + Parent.Left) - Parent.Width - ToMove.Width) + Parent.Width + ToMove.Width + Offset);
                   ToMove.Top = Parent.Top;
                   break;
           }
       }

 

Note:

- Type 1; is the left side operation, and adding Secondary.Width to the offset will place the third (/Tertiary) form before the secondary, leaving it blank will result in it being below or above the second one, or the form you've got there.

- Type 2; is the right side operation and the same applies to this as the first type.

 

 

The end result should look like this, without it being an invoice manager that is. http://prntscr.com/8dzvdr

Share this post


Link to post
Share on other sites

f = MA;

 

Rate of change of momentum which is directly proportional to the applied force on the body (f=ma)

Share this post


Link to post
Share on other sites

f = MA;

 

Rate of change of momentum which is directly proportional to the applied force on the body (f=ma)

 

You clever little jelly bean, I hoped nobody would notice that.

Share this post


Link to post
Share on other sites

f = MA;

 

Rate of change of momentum which is directly proportional to the applied force on the body (f=ma)

 

You clever little jelly bean, I hoped nobody would notice that.

 

am a science freak.

Share this post


Link to post
Share on other sites

Updated to add WPF Functionality where needed, I forgot to add this when I actually created it (Screenshot will be added shortly).

Share this post


Link to post
Share on other sites

Do you mind to send a link of download of the results?

 

I'm unable to do this, as I no longer have a working project that isn't closed source.

Share this post


Link to post
Share on other sites

Do you mind to send a link of download of the results?

 

I'm unable to do this, as I no longer have a working project that isn't closed source.

 

I am learning C# in school, and I am usually using Console Application. I'd like to try what you have sent and get some results, but the tutorial isn't that understandable for new coders in C#.

Share this post


Link to post
Share on other sites

Do you mind to send a link of download of the results?

 

I'm unable to do this, as I no longer have a working project that isn't closed source.

 

I am learning C# in school, and I am usually using Console Application. I'd like to try what you have sent and get some results, but the tutorial isn't that understandable for new coders in C#.

 

Hey Lonely,

 

Its not really created for "New" programmers (correct term), its mainly for the "newish" programmers that know their way around C# enough to create backend/front end systems with ease.

 

I'll see if I can find the time some time this month or next to create a demo application, I'm not going to promise you or anyone else that I will, due to having massively busy year from the lovely clients.

Share this post


Link to post
Share on other sites


×
×
  • Create New...

Important Information

Please visit Terms of Use for our Terms of Use and Privacy Policy for our privacy policy