Hi, I have created 2 forms.

Form1 and Form2

Form1 contains button to open form 2
Form2 contains a check box and ok button
When checkbox is checked and I click ok button, the TopMost property of form 1 is set true. But I m unable to see the change.

Help me out!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

This is form 1 code

namespace xyz
{
    public partial class Form1 : Form
       { 
    form2 pqr = new form2();

    private void openform2_Click(object sender, EventArgs e)
        {

            pqr.Show();
        }
    }
}

Form 2 code

 namespace xyz
    {
        public partial class Form2 : Form
           { 
        form1 backform = new form1();

        private void ok_Click(object sender, EventArgs e)
            {

                 if (ontop.Checked == true)
            {
                backform.TopMost = true;
            }
            else
            {
                backform.TopMost = false;
            }

            }
        }
    }

Edited 4 Years Ago by __avd: added [code] tags.

line 5, form2. That is not the same as the original instance of form1, which is why you don't see the changes when from1 returns. What you want to do is to set a public int in form2 that can be read by form1 when it regains control, then use that information to set its TopMost property appropriately.

Something like below: warning: I didn't compile this so it might contain compile errors.
but you get the idea.

namespace xyz
{
    public partial class Form1 : Form
       { 
    form2 pqr = new form2();

    private void openform2_Click(object sender, EventArgs e)
        {

            pqr.Show();
            this->TopMost = pqr.GetTopMost();
        }
    }
}


 namespace xyz
    {
        public partial class Form2 : Form
           { 
           private bool IsTopMost = false;
        form1 backform = new form1();

        private void ok_Click(object sender, EventArgs e)
            {

                 if (ontop.Checked == true)
            {
                IsTopMost = true;
            }
            else
            {
                IsTopMost = false;
            }
public bool GetTopMost() { return IsTopMost; }
            }
        }
    }

Edited 4 Years Ago by Ancient Dragon

The top most property is set when I click openform2 button in form1. I want the property to be changed when I click ok button in form2

Lets go through your needes:
You have a form1. From it you open form2. On form2 you have a checkBox, and when you check it, you want the form1 to show on top of the form2. Is that correct?

If so, pass a reference of form1 to form2 in the constructor of form2, when opening form2.
Then use this reference to set the TopMost property.

Example:
//when opening form2:
Form2 f2 = new Form2(this); //pass f1s reference`

//on form2:

Form1 f1;
public Form2(Form1 _f1)
{
    this.f1 = _f1;
}

//on form2 when you want to show f1 on top:
f1.TopMost = true;

Edited 4 Years Ago by __avd: added [code] tags.

Form2 f2 = new Form2(this); //pass f1

I have declared this line as global in my project! I get blue line under 'this'.

global? no need to.
put this code into some button click event, where you wanna open form2.
and "this" is a reference of a FORM1 (if you type it on form1).

//on FORM1:
private void buttonOpenForm2_Click(object sender, EventArgs e)
{
   Form2 f2 = new Form2(this);
   f2.Show();
}

I need it to be global cause in other part of program I m using some variables from form 2

You dont need global, thats why you pass a referecne to other form or class. And you access to the base form (or class) with this referecne. See How I showed you in the example.

This article has been dead for over six months. Start a new discussion instead.