Position form where the last form used was positioned - c#

Let's say I have 2 movable forms and when I click a button in one form (the fist one which has it's start position CenterScreen) it brings me to another one (but the second one has still centerscreen start position) and let's say I move the first form before I press the button and when I click the button I want to position the new form where I moved my first form... how can I do this?

use this following code,
set StartupPosition = Manual then the Left and Top values (Location) set via the this code
Code for the Form1
using System;
using System.Windows.Forms;
namespace PositioningCs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void myButton_Click(object sender, EventArgs e)
{
Form2 frm2=new Form2();
frm2.setLocation(this.Top,this.Left);
frm2.show();
}
}
}
Code for the Form2
using System;
using System.Windows.Forms;
namespace PositioningCs
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private int top_val=0;
private int left_val=0;
public void setLocation(int top_val,int left_val)
{
this.top_val=top_val;
this.left_val=left_val;
}
private void Form2_Load(object sender, EventArgs e)
{
this.Top = top_val;
this.Left = left_val;
}
}
}

Remember the position of a form after it's moved and re-use it later.
private static Point location = null;
private void MyForm_Move(object sender, EventArgs e)
{
location = this.Location;
}
private void MyForm_Load(object sender, EventArgs e)
{
if (location != null) { this.Location = location; }
}

you can relate position with position of parent form...
//if "this" is Form1, in the button event to call Form2...
...
Form2.Position = this.Position;
Form2.ShowDialog(this);
in this link you can find more details: Link
i hope this help you

Related

get the Width of the textbox when opening the form in C#

I have a form as shown below, I want to save the width of the original textbox when the form loads, I have written code but not running, please help.
namespace DieuKhienMayTinh
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
float Sizetb = textBox1.Width;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Sizetb);
}
}
}
form
You are storing textbox width in a method-local variable Sizetb. This wont compile at all.
Move it out to class level:
namespace DieuKhienMayTinh
{
public partial class Form2 : Form
{
// class level member field available for each method in class
private float Sizetb;
public Form2()
{
InitializeComponent();
Sizetb = textBox1.Width;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Sizetb);
}
}
}

Move PictureBox from one form to another form C#

im actually trying to program in C# and want to move an image from a pictureBox1 from Form1 to another pictureBox1 of Form2.
I did following
In Form1.cs:
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Load("C:\\Users\\img_0.bmp");
}
In Form1.Designer: change from private to public
public System.Windows.Forms.PictureBox pictureBox1;
In Form2.cs:
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Form1.pictureBox1.Image;
}
But i get this error: CS0120: An object reference is required for the non-static field, method, or property 'Form1.pictureBox1'
Can somebody help me please? :)
Although it is not appropriate to do it this way, but, Yes, this is possible.
First create the picture box in external class file like this:
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public class Class1
{
public static PictureBox pb1 = new PictureBox();
}
}
Then add it into your 1st form, let's say another button click event will open another form. So you will have to first remove the picturebox, which will allow the 2nd form to use it.
public Form2()
{
InitializeComponent();
this.Controls.Add(Class1.pb1);
}
private void button2_Click(object sender, EventArgs e)
{
this.Controls.Remove(Class1.pb1);
Form3 f = new Form3();
f.ShowDialog();
this.Controls.Add(Class1.pb1);
}
Then, while you open the next form, add the picturebox and when the next form is closing, remove it.
public Form3()
{
InitializeComponent();
this.Controls.Add(Class1.pb1);
this.FormClosing += Form3_FormClosing;
}
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
this.Controls.Remove(Class1.pb1);
}
There, you have move the picturebox from 1st form to 2nd form and then back to 1st form.

Windows Form 1 Google Search button that opens second Windows Form and loads Page

So here is my setup so you can better understand what I am trying to do. I have the following:
Windows Form 1 (Toolbox) with a textbox for the Google Search and a Search Click button that loads the secondary Windows Form (Search Engine). I am wanting to do the following.
Type the search criteria in my text box and clicking on the Search button which will open the secondary Windows Form (Search Engine) which will load the google search on the (Search Engine) form using "WebBrowser1"
Any assistance would be greatly appreciated. If you can provide code instead of where to go that would be best :)
Updated Code: Which opens the Google Search Engine but does not display on the Webbrowser1. Any additional assistance would be appreciated
Form 1 Search button click Code:
namespace Plumchoice_Toolbox
{
public partial class plumchoice_Form1 : Form
{
public plumchoice_Form1()
{
InitializeComponent();
}
private void plumchoice_Form1_Load(object sender, EventArgs e)
{
}
//Search Engine Form 1 added
//// Search Button that opens the Search Engine
private void button18_Click(object sender, EventArgs e)
{
Google_Search.SearchEngineForm1 frm2 = new Google_Search.SearchEngineForm1();
frm2.searchAddress = "http://www.google.com/webhp?hl=en&tab=ww#hl=en&tbo=d&output=search&sclient=psy-ab&q=" + textBox3.Text.Replace(" ", "+");
frm2.Show();
}
Webbrowser1 Code
namespace Google_Search
{
public partial class SearchEngineForm1 : Form
{
public SearchEngineForm1()
{
InitializeComponent();
}
public string searchAddress;
private void PlumChoiceToolboxForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(searchAddress);
}
You can do this: In form1 search button click
This code is for Form1 that has a search text box and a button for start searching. I think (but not sure) it is same as your Toolbox form.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Search_Click(object sender, EventArgs e)//When user clicks on search button
{
SearchEngineForm1 frm2 = new SearchEngineForm1();
frm2.searchAddress = "https://www.google.com/search?q=" + txtSearch.Text.Replace(" ", "+");
frm2.Show();
}
}
And below is the Form2 or SearchEngineForm1 that will open after search button click.
public partial class SearchEngineForm1 : Form
{
public SearchEngineForm1()
{
InitializeComponent();
}
public string searchAddress;
private void SearchEngineForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url= new Uri(searchAddress);
}
}
In your code you have a mistake:
private void PlumChoiceToolboxForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(searchAddress);
}
What is this void? This will never fire. You need to double click on SearchEngineForm1 form (a blank space on form) to add this void to your code:
private void SearchEngineForm1_Load(object sender, EventArgs e)
{
}
Do not forget double click i said. Pasting this only does not help you!
then enter the webBrowser1.Url= new Uri(searchAddress); between its braces {} to have this exactly:
private void SearchEngineForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url= new Uri(searchAddress);
}

Copying data from form1.richTextBox to form2.richTextbox

I am making a Time clock for fun and to learn C#.
I have the time down, and the start, stop, clear.
However I am having issues with a "Notes" section. Ideally I'd like to be able to write notes into a field, and have an "Edit" button to allow the user to open a window for more options relating to text editing. (with the text from the Form1 rich text box)
My issue comes from copying the data from one form to another.
Here is the code:
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;
namespace PunchOut
{
public partial class PunchOut : Form
{
public PunchOut()
{
InitializeComponent();
}
int i = 0;
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
i++;
TimeSpan t = TimeSpan.FromSeconds(i);
textBox2.Text = string.Format("{0:D2}:{1:D2}:{2:D2}",
t.Hours,
t.Minutes,
t.Seconds);
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Clear();
textBox2.Text = ("00:00:00").ToString();
}
private void button6_Click(object sender, EventArgs e)
{
}
public void button4_Click(object sender, EventArgs e)
{
new Form2().Show();
richTextBox1.Text = Form2.richTextBox1.Text;
}
}
}
Here is the Form2 code:
namespace PunchOut
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.Text = PunchOut.richTextBox1.Text;
}
}
}
Currently, I get an error that states:
an object reference is required for the non-static field, method, or property 'PunchOut.PunchOut.richTextBox1'
and
an object reference is required for the non-static field, method, or property 'PunchOut.Form2.richTextBox1'
Why do I get these errors?
lots of unneeded work going on there. I hope I explained this well enough
Breakdown:
We add a String Member Variable so that we can put the contents of a RichTextBox into a string and pass that instead of using the RichTextbox control.
We change the constructor to take a string parameter which is the RTF text that you want to change. Now Form2 can change any RTF text and not be specifically tied to just the richTextbox1 on the punchoutForm.
We then change the updating of the member variable to when the form is closing otherwise you are changing it with each keystroke which is a lot of unnecessary method calls.
namespace PunchOut
{
public partial class Form2 : Form
{
public String richText;
public Form2(String rText)
{
InitializeComponent();
this.richTextBox1.Rtf = rText;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
richText = this.richTextBox1.Rtf;
}
}
}
Then in your button4_Click. Use Rtf to include the codes otherwise just use a plain textbox
Now in your button click event handler we create a new form and assign it to a variable. We then call showdialog. The reason for showdialog is that this will make the form the top most form so that a user cannot go back to the punchout form and make a change to the richtextbox which would then make the text in the Form2 obsolete as it would no longer represent the correct RTF text in the punchout form. When the user is done editing the text and closes the form we then request the edited rtf text by accessing the richText Member Variable of Form2. The reason you can access this after the form has closed is that the form is not disposed until the method returns, your local variable lives within the scope of the method.
public void button4_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(richTextBox1.Rtf);
f2.ShowDialog();
richTextBox1.Rtf= f2.richText;
}
You didn't make a new reference to PunchOut in Form2. In your form 2 add this under the class declaration:
PunchOut punchOut;
And you get in Form2:
namespace PunchOut
{
public partial class Form2 : Form
{
PunchOut punchOut;
public Form2(PunchOut PUNCHOUT)
{
punchOut = PUNCHOUT;
InitializeComponent();
}
public void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.Text = punchOut.richTextBox1.Text;
}
}
}
In the original replace button4_CLick with:
public void button4_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
richTextBox1.Text = form2.richTextBox1.Text;
}
EDITED: You should pass the old PunchOut instead of creating a new one. I've updated the code.
You could store the text in a more accessible string. Then have your other form call that string.
public string yourText;
// down further
yourText = textBox1.Text;

Enter Data to main form

I Made an application. The Main form Name is Form1.
And the other Form is called PoP.
public partial class pops : Form
{
public pops()
{
InitializeComponent();
CenterToScreen();
}
private void pops_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Close();
}
private void lblAdminNo_Click(object sender, EventArgs e)
{
}
}
Make two public properties on popup form and retrieve them from parent form.
string username = string.Empty;
string password = string.Empty;
using (LoginForm form = new LoginForm ())
{
DialogResult result = form.ShowDialog();
if (result == DialogResult.Ok)
{
username = form.Username;
password = form.Password;
}
}
It all depends on from where are you calling the Pop form.
If it is called from the Form1 itself, then the Popform's object itself would provide you the value.
Pop popFrm = new Pop();
if(popFrm.ShowDialog() == Ok)
{
string userName = popFrm.TextBox1.Text;
}
If the Pop is invoked from a different area/part of application, you may have to store it somewhere common to both the forms.
This can be done through events. This approach is particularly useful when data to be posted even when the child form is kept open.
The technique is- From parent form, subscribe to a child from event. Fire the event when child form closes, to send data
----- SAMPLE CODE-----
Note: In the Parent Form add a Button:button1
namespace WindowsFormsApplication2
{
public delegate void PopSaveClickedHandler(String text);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Pops p = new Pops();
p.PopSaveClicked += new PopSaveClickedHandler(p_PopSaveClicked);//Subscribe
p.ShowDialog();
}
void p_PopSaveClicked(string text)
{
this.Text = text;//you have the value in parent form now, use it appropriately here.
}
}
Note: In the Pops Form add a TextBox:txtUserName and a Button:btnSave
namespace WindowsFormsApplication2
{
public partial class Pops : Form
{
public event PopSaveClickedHandler PopSaveClicked;
public Pops()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
if(PopSaveClicked!=null)
{
this.PopSaveClicked(txtUserName.Text);
}
}
}
}
Summary:
1.Add a delegate(place where it available to both parent and child form) :
public delegate void PopSaveClickedHandler(String text);
2.In form:Pops, Add an event:
public event PopSaveClickedHandler PopSaveClicked;
3.Subscribe to the event in Parent Form:
p.PopSaveClicked += new PopSaveClickedHandler(p_PopSaveClicked);
4.Invoke the event in form:Pops Save Button Click
if(PopSaveClicked!=null)
{
this.PopSaveClicked(txtUserName.Text);
}
You can send data to the form object before you display it. Create a method to call, send the info through the constructor... etc.

Categories