Open second form, right next first form - c#

I'am using winforms.
I created an application which is nearly finished. Consider the following: I have two forms, the first form starts at application startup, the second form needs to be opened right next to the first form.
Example:
How can I access the location of the first form at the second form? Should I send "this" to the constructor of the second form?
EDIT
following code helped me out:
private void changelogToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_changelog.IsDisposed)
{
_changelog = new Changelog();
}
_changelog.Location = new Point((Left + Width), Top);
_changelog.Show();
}

A basic rule to keep in mind when designing one's constructor: Never give any unnecessary information to the constructor.
So, what you need here is not the other window, but rather it's position.
Even better, you need the position where your new window should be located at.
This means that you shouldn't let the second form know about the first form, instead it's constructor should take either:
One parameter Point location
Two parameters int x, int y
Depending on your preferance.
You could (should) of course have both constructors, so you can decide whether to give Point location or int x, int y.
This all being said, forget what you read.
Better than using a constructor at all, I would just set the property manually when creating the second form:
SecondForm form = new SecondForm()
{
Location = new Point(this.Right, this.Top)
};
Which is just an other way of saying:
SecondForm form = new SecondForm();
form.Location = new Point(this.Right, this.Top);

Why do not position the new form when you open it?
Form2 f = Form2();
f.Location = new Point(this.Left + this.Width, this.Top);
f.Show(); // Or ShowDialog()
Of course, this requires that the second form property StartPosition is set to FormStartPosition.Manual

Related

Windows Forms - Integer being reset upon form load

My issue is that an integer created in my main class,
public int loadCountGold = 0;
that is incremented to when a button triggering the appearance of another form
public void goldButton_Click(object sender, EventArgs e)
{
loadCountGold += 5;
Console.WriteLine(loadCountGold);
GoldForm gForm = new GoldForm();
gForm.Show();
}
Is not having its incremented value carried over to the form where it needs to be used in an if statement.
private void GoldForm_Load(object sender, EventArgs e)
{
//Sets the random (within reason) value for gold
if (main.loadCountGold <= 1)//if its the firstload of the form
{
Console.WriteLine(main.loadCountGold);
Random rand = new Random();
currentGoldValue = rand.Next(1200, 1350);
}
}
I had included a few write lines at points before the form is opened, and one within the problem form to see what was happening to the value. This can be seen below
It shows the output that my program is giving, highlighted are the outputs from my "Main" class and the other are the ones being produced from the problem form.
The aim is to have the value incremented each time the form is opened so that the code within the if statement is only run the first time the form is opened, but currently it is running every time. (I am aware I would need to change the amount it is incremented)
In your main form, call GoldForm like this:
GoldForm gForm = new GoldForm(this);
In the constructor of GoldForm, do it like this:
Main main;
public GoldForm(Main main)
{
InitializeComponent();
this.main = main;
}
Now you don't create a new instance of Main every time you click the button.
Output after 3 button clicks:
5
10
15
This is an issue of needing a static variable rather than one instanced with the class. The easy answer is to change your integer declaration to:
public static int loadCountGold = 0;
now all references to this variable will point to the same thing.
I should specify that your reference in the later form needs to be Main with a capital M.
if (Main.loadCountGold <= 1)//if its the firstload of the form

Deleting old winform and tray icon when I make a new winform

In my application I have two winforms. The first acts as my control panel and the second I use to take screen shots. However, when I go from Winform 2 back to Winform 1 I have a new winform created and a brand new tray icon. This is on top of the initial ones I create when the program first starts.
When I go from winform 1 to winform 2 I do the following:
this.Hide();
Form2 form2 = new Form2();
form2.InstanceRef = this;
form2.Show();
Then when I want to go back from Winform 2 to Winform 1 I do the following:
this.Close();
Winform1 form;
form = new Winform1 (capturedImageObj);
form.Show();
I know straight off the bat the issue falls on the fact I'm creating a new Winform1, but I need to do that so I can pass my capturedImageObj back into Winform 1.
I've tried calling this.close() and this.dispose() in the my first section of code but that only closes the program down. Is there a way I can dispose of Winform 1 but still use Winform 2 and pass the object I need to back into a new copy of Winform 1?
Here is the constructor for my Winform 1:
public ControlPanel(Image imjObj)
{
InitializeComponent();
_screenCap = new ScreenCapture();
_screenCap.OnUpdateStatus += _screen_CapOnUpdateStatus;
capturedImage = imjObj;
imagePreview.Image = capturedImage;
}
Change Winform1 to use properties like this:
public Image CapturedImage {
get { return imagePreview.Image; }
set { imagePreview.Image = value; }
}
Then change your constructor like this:
public ControlPanel(Image imjObj)
{
InitializeComponent();
_screenCap = new ScreenCapture();
_screenCap.OnUpdateStatus += _screen_CapOnUpdateStatus;
CapturedImage = imjObj;
}
And, finally, change your Winform2 to do this:
((Winform1)this.InstanceRef).CapturedImage = capturedImageObj;
this.InstanceRef.Show();
this.Close();
Based on the comment, it sounds like your InstanceRef property is of type Form. Change it to be of type Winform1. Or, I changed the code above to do some casting for you.

Filling one form from another

I have two forms in my project, one is with button and second one is with two textboxes.
When I am clicking the first form button, I want to fill textbox in another.
code:
secondForm secondForm = new secondForm();
secondForm.MdiParent = this.MdiParent;
secondForm.fillForm("String");
if (formShown != true)
{
secondForm.Show();
formShown = true;
}
else
{
Application.OpenForms[secondForm.Name].Focus();
}
and
public void fillForm(string text)
{
if (String.IsNullOrEmpty(priceText1.Text))
{
priceText1.Text = text;
}
else
{
priceText2.Text = text;
}
}
looks simple, but when i am clicking button second time, its not detecting text in second form text box and entering data in textbox1 instead of textbox2, why?
The problem is that when you click the button a second time you're creating a second instance of the form, and then just not showing it (you're focusing the first instance instead). You should refactor the program so that you don't do that.
private secondform secondform = null;
private void Foo()
{
if(secondForm == null)
{
secondForm = new secondForm();
secondForm.MdiParent = this.MdiParent;
secondForm.Show();
}
secondForm.fillForm("String");
secondForm.Focus();
}
So a new instance is only created if we don't already have one, we fill the data every time, and then focus the form.
P.S. there's nothing wrong with focusing the form the first time, so I just left it after the end of the if. If there was something that should only happen when it's not the first time we could add an else to the if and put the code there.
P.S.S. secondForm isn't following standard naming conventions for the name of a class. Class names should start with an upper case letter, i.e. SecondForm. Among other issues, this removes the ambiguity about whether secondForm is refering to the type, or the instance of the type.
P.S.S.S. It may not be needed functionality in your case, but we may need to properly handle the case where the child form is closed and then the button is clicked again. The easiest way of handling this is to clear out the secondForm instance field when the form is closed (letting a new one be created when the button is next clicked, if that ever happens). Closures make this really easy, we just add this line right before secondForm.Show();:
secondform.FormClosed += (s, arg) => secondform = null;

Adding Controls to Form from another class

I have a program, which creates one pictureBox in Form1, and then creates an instance of a class that I called InitialState. The InitialState puts the source to the Image so that it is displayed, and after some time has passed, for which I used a Timer, it creates the next class, MainMenuState. Now, in that MainMenuState class that I've created, I would like to create another pictureBox and make it display on that Form1. Later on, I would like to make the pictures inside it change a bit, and then (possibly) destroy that pictureBox. After that, the program enters the next state (which is in yet another class), and again I would like that class to add a picture box to the original form, and so on.
Basically, I would like to dynamically add controls to the main Form1, but not in the said form, but from the classes I create later on. I've been searching on the internet for a way to do that, and it seems like I would have to use a delegate in order to invoke the Controls.Add method of the Form1 class. I've tried that, and the code compiles, but the pictureBox still doesn't show up.
Here's my code:
Form1 class:
public const string RESOURCE_PATH = "C:/Users/Noel/Documents/Visual Studio 2010/Projects/A/Resources/Animations/";
public Form1()
{
InitializeComponent(); //here, the first pictureBox shows
iInitializeComponent();
zacetnaAnimacija.Dock = DockStyle.Fill; //zacetnaAnimacija is the first pictureBox that appears
zacetnaAnimacija.Anchor = AnchorStyles.Top | AnchorStyles.Left;
zacetnaAnimacija.SizeMode = PictureBoxSizeMode.StretchImage;
InitialState intialState = new InitialState(this, zacetnaAnimacija); //entering InitialState
}
InitialState class:
class InitialState : State
{
System.Timers.Timer initialTimer;
PictureBox pictureBox1;
Form1 form;
public InitialState (Form1 form, PictureBox pictureBox1) {
this.form = form;
GifImage zacetnaSlika = new GifImage(Form1.RESOURCE_PATH + "Presenting.gif"); //this is just a .gif picture I'm displaying
Image trenutnaSlika = zacetnaSlika.GetFrame(0); //a method that plays the .gif
pictureBox1.Image = trenutnaSlika; //makes the first .gif display
this.pictureBox1 = pictureBox1;
initialTimer = new System.Timers.Timer(2500);
initialTimer.Enabled = true;
initialTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
initialTimer.Enabled = false;
MainMenuState menuState = new MainMenuState(form, pictureBox1); //enters main menu state with the Form1 argument passed on
}
MainMenuState class:
class MainMenuState : State
{
Form1 form;
public MainMenuState (Form1 form, PictureBox pictureBox1) {
this.form = form;
GifImage zacetnaSlika = new GifImage(Form1.RESOURCE_PATH + "animated.gif");
Image trenutnaSlika = zacetnaSlika.GetFrame(0);
pictureBox1.Image = trenutnaSlika; //this simply makes another .gif appear in the picture box instead of the first one
PictureBox a = new PictureBox(); //HERE'S my problem, when I want to add ANOTHER pictureBox to that form.
a.BackgroundImage = trenutnaSlika;
a.Location = new System.Drawing.Point(0, 0);
a.Name = "zacetnaAnimacija";
a.Size = new System.Drawing.Size(150, 150);
a.TabIndex = 1;
a.TabStop = false;
AddControl(a); //calling the delegate
}
public delegate void AddControls(PictureBox a);
public void AddControl(PictureBox a)
{
if (form.InvokeRequired)
{
AddControls del = new AddControls(AddControl);
form.Invoke(del, new object[] { a });
}
else
{
form.Controls.Add(a);
}
}
As I've said, the code compiles, but it doesn't create the PictureBox a on the Form1, when the MainMenuState is created. The thing is, if I don't use the delegate in the MainMenuState and just try to do something like form.Controls.Add(a), then I get a "cross-thread operation not valid" exception, and it doesn't even compile. That's why I used the delegate, but even now, it doesn't work.
Can someone please help me?
initialTimer = new System.Timers.Timer(2500);
That's part of the reason you're having trouble. The Elapsed event runs on a threadpool thread, forcing you to do the BeginInvoke song and dance. Use a System.Windows.Forms.Timer instead, its Tick event runs on the UI thread.
You'll also run into trouble with memory management, these classes need to implement IDisposable.
Oh my God, I just found the reason X_x
It was the fact that since the first pictureBox was covering the entire form, and the second one, which was created by the delegate, showed behind it! I just need to bring it to front!
Thank you guys, nonetheless, I probably wouldn't have come to that without you.
Edit: However, may I ask how to bring that control to the front? The a.BringToFront() function doesn't seem to work.
Instead of
form.Invoke(del, new object[]{a});
try:
form.Invoke(new ThreadStart(delegate
{
form.Controls.Add(a);
}
));

Changing the property of a control on another form

Basically, I have a settings window, and when you click "OK", it's suppose to apply settings to the main form (eg, set font of a control, etc), and then close.
frmmain frm = new frmmain();
frm.OLVAltBackColor = Color.Aquamarine ;
I tried that, but it only applies the settings to that instance, and you can see it if you do frm.Show();
I'm trying to make it so the already opened form has it's control's properties changed.
What you are trying to do is not working because you are creating a NEW instance of your main form and updating that rather than the first instance. It is possible to update the main form by keeping a reference to it in your settings form... but...
...it sounds like you are approaching this from the wrong direction.
Don't make the settings form dependent on the main form. Instead create the settings form from the main dialog.
class SettingsForm : Form
{
// You need to ensure that this color is updated before the form exits
// either make it return the value straight from a control or set it
// as the control is updated
public Color OLVAltBackColor
{
get;
private set;
}
}
In your main form
(I'm assuming some kind of button or menu click)
private void ShowSettingsClicked(object sender, EventArgs args)
{
using (SettingsForm settings = new SettingsForm())
{
// Using 'this' in the ShowDialog parents the settings dialog to the main form
if (settings.ShowDialog(this) == DialogResult.OK)
{
// update settings in the main form
this.OLVAltBackColor = settings.OLVAltBackColor;
}
}
}
Apply the property change to the form that already exists and is already shown instead of creating a new form and changing that one.
In this code you're creating a new instance of the frmmain. Any changes you make to that new object will happen in the new object, not the one you actually want to change.:
frmmain frm = new frmmain(); //Creating a new object isn't the way.
frm.OLVAltBackColor = Color.Aquamarine ;
What you're looking for is a way to call on the already existant frmmain class and change the property of that.
Edit, for example:
using System;
class Statmethod
{
//A class method declared
static void show()
{
int x = 100;
int y = 200;
Console.WriteLine(x);
Console.WriteLine(y);
}
public static void Main()
{
// Class method called without creating an object of the class
Statmethod.show();
}
}

Categories