How to make a window floating on desktop? - c#

I am trying to create a button to make a windows floating on the desktop using "topmost" but I can't assign a Boolean to the button, because it is a
method group
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
if(this.TopMost)
{
this.TopMost = true;
Button1_Click = true;
}
else
{
this.TopMost = false;
Button1_Click = false;
}
}
}

The answer is actually pretty short:
private void Button1_Click(object sender, EventArgs e)
{
TopMost = !TopMost;
}
You don't need to include this since you are already in the scope of your form and your if else logic can be shortened to e = !e.
Button1_Click
Is a method, you can't assign a value to it.

Related

Object reference is required for the non static field

namespace Pong
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
PongForm.Show();
this.Close();
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Can someone explain why I'm getting an error? I've had a look online and think it should work. I'm trying to change to a new form on button click.
In this function you should refer form, not PongForm:
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
form.Show();
this.Close();
}
Change "PongForm.Show();" to "form.Show().
To eloborate: you are attempting to call the class, not the instance you created.
to just add to what others have said. you probably don't want multiple of the same forms open. I cant comment or I would have done that instead. hope this solves your problem.
if (Application.OpenForms["PongForm"] != null)
{
Application.OpenForms["PongForm"].WindowState = FormWindowState.Normal;
Application.OpenForms["PongForm"].BringToFront();
}
else
{
PongForm form = new PongForm();
form.Show();
}

Cannot redisplay Windows Form after closing it

When I click a button a form is opened, but if the form is already open, then the app should display the message "Form already open!" and do nothing else.
My problem is, once I close the window [x] I can't open the form again.
Here's the code:
Form2 decript_form = new Form2();
private void button2_Click(object sender, EventArgs e)
{
if (!decript_form.Visible)
decript_form.Show();
else
MessageBox.Show("Form already open!");
}
When the "Close" button is pressed, you want it to just "hide" the form...you need to use e.Cancel to stop it going on and closing.
If you really really want to close the Form2 window instead of hiding it while your application is running....then call ReallyClose....so that the close isn't prevent (then create a new decript_form or null it out).
(Alternatively decript_form.Dispose() would force real closure too)
public partial class Form2 : Form
{
private bool m_bReallyClose = false;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (!m_bReallyClose)
{
this.Visible = false;
e.Cancel = true;
}
}
public void ReallyClose()
{
m_bReallyClose = true;
this.Close();
}
}
public partial class Form1 : Form
{
Form2 decript_form = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!decript_form.Visible)
decript_form.Show();
else
MessageBox.Show("Form already open!");
}
private void button2_Click(object sender, EventArgs e)
{
decript_form.Dispose(); // or .ReallyClose();
decript_form = new Form2();
}
}
I assume that you talk about the [x] on Form2 being pressed. It that's the case you should handle the Closing event in Form2() and add
this.Hide();
to the handler. Even a closed window is still 'shown' until it is hidden.
class Form2
{
override protected void OnClosing(CancelEventArgs e)
{
Hide();
}
}

Create one loading form and load different form

I have created one loading form, but I want access the loading form and load the different form. I know there is a way to achieve this, but the way that I think is create another loading form and access that another loading form to load the different form, even though the loading form contents are same.
How can I achieve this?
Here is the code of Loading Form:
public Loading()
{
InitializeComponent();
this.timer1.Interval = SystemManager.RandomNumberGenerator(1000, 2000);
this.timer1.Tick += new EventHandler(this.CheckTimer);
}
private void Loading_Load(object sender, EventArgs e)
{
this.timer1.Start();
}
private void CheckTimer(object sender, EventArgs e)
{
uint timeLeft = 1;
timeLeft--;
if (timeLeft == 0)
{
this.timer1.Stop();
this.Hide();
AgeConfirmation _ageConfirmation = new AgeConfirmation();
_ageConfirmation.ShowDialog();
this.Close();
}
}
Above code is one loading form and load another form by the time is reached 0.
I have tried like this:
public class SystemManager
{
public static void LoadForm(Form _form = null, Form _loadForm = null)
{
_form.Hide();
_loadForm = new Form();
_loadForm.ShowDialog();
_form.Close();
}
}
and access it like this:
SystemManager.LoadForm(this, AgeConfirmation);
But it is throws the following error:
'System.Windows.Forms.AgeConfirmation' is a 'type' but is used like a 'variable'
My Question is: Create only one form (Loading Form), and access that Loading Form and by the time, the time reached 0, it will access different form.
Your answer much appreciated!
Thank you very much!
You should be using _ageConfirmation, which is the form object, not AgeConfirmation which is the form type.
i.e. SystemManager.LoadForm(this, _ageConfirmation);
Solved by myself! I create a getter and setter int value and by using switch case and the code will be like this:
public class UserInformation
{
public static int Value
{
get;
set;
}
}
public partial class Loading : Form
{
public Loading()
{
InitializeComponent();
this.timer1.Interval = SystemManager.RandomNumberGenerator(1000, 2000);
this.timer1.Tick += new EventHandler(this.CheckTimer);
}
private void Loading_Load(object sender, EventArgs e)
{
this.timer1.Start();
}
private void CheckTimer(object sender, EventArgs e)
{
uint timeLeft = 1;
timeLeft--;
if (timeLeft == 0)
{
this.timer1.Stop();
this.Hide();
switch (UserInformation.Value)
{
case 0:
AgeConfirmation _ageConfirmation = new AgeConfirmation();
_ageConfirmation.ShowDialog();
break;
case 1:
MainForm _mainForm = new MainForm();
_mainForm.ShowDialog();
break;
case 2:
Event _event = new Event();
_event.ShowDialog();
break;
}
this.Close();
}
}
}
private void AgeConfirmation_Load(object sender, EventArgs e)
{
UserInformation.Value = 1;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Loading _loading = new Loading();
_loading.ShowDialog();
this.Close();
}
private void MainForm_Load(object sender, EventArgs e)
{
UserInformation.Value = 2;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Loading _loading = new Loading();
_loading.ShowDialog();
this.Close();
}
By the time the program runs, UserInformation.Value will be 0
Thank you very much for those who are replying to my question.
Change
_ageConfirmation.ShowDialog();
to
_ageConfirmation.Show();

How can I init items of a tab?

I have a form with 2 tabs on it. I can chose the tab viewed after initialization and I need some initial code every time after the tab2 is initialized:
public partial class SetupComponent : Form
{
public SetupComponent(bool tab2)
{
InitializeComponent();
if (tab2)
{
this.tabControl1.SelectedTab = tabPage2;
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox2.SelectionStart = textBox2.Text.Length;
textBox2.Focus();
}
}
if I call this class with tab2=false and then click onto tab2, tabControl1_SelectedIndexChanged is called.
But if I select the tab2=true during SetupComponent, I find no possibility to do that code. All the TabControl1_Events I found are too early and I don`t find a matching TabPage2_Event.
How can I manage it?
I managed this problem using the Paint_Event:
bool activated = false;
private void tabPage2_Paint(object sender, PaintEventArgs e)
{
if (!activated)
{
tabControl1_SelectedIndexChanged(null, null);
activated = true;
}
}
I use the variable because the Paint_Event is called many times.

How to open a form only once?

I have an app that show a form call System Parameters and i want the form to only pop one time so that the user cant open the same window million times. I tried
private void SystemParametersClick(object sender, EventArgs e)
{
Xpan sp = new Xpan();
sp.CurrentItem = this.GetCaller(sender);
if (sp.Visible==false)
{
sp.Show();
}
}
It doesnt work because it is not the same instance. :(
How do i make it only pop once?
Why do you instantiate the form within the method? Simply instantiate it within the parent class and only call the Show() method within the click event.
public class MainForm : Form
{
private Xpan _Xpan;
public MainForm()
{
InitializeComponent();
_Xpan = new Xpan();
}
private void SystemParametersClick(object sender, EventArgs e)
{
_Xpan.Show();
}
}
Maybe this simple approach would suffice?
private bool has_been_shown = false;
private void SystemParametersClick(object sender, EventArgs e)
{
if(!has_been_shown)
{
has_been_shown = true;
Xpan sp = new Xpan();
}
}
First disable closing for Xpan form. You can do it by defining OnFormClosing event handler.
private void Xpan_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
Then define your Xpan form as a class member of the parent form, e.g.:
private readonly Xpan _sp = new Xpan();
And finally defile your Click handler this way:
private void SystemParametersClick(object sender, EventArgs e)
{
if (!_sp.Visible)
{
_sp.Show();
}
else
{
_sp.Activate();
}
}
That's it.

Categories

Resources