displays the position of the form location dynamically C# - c#

I want to display the position of form 2 according to my wishes in Form1, to be precise, placed on the right. I write this code in form1 :
public static int ParentX, ParentY;
private void BT_ShowForm2_Click(object sender, EventArgs e)
{
using (Form2 Frm = new Form2 ())
{
ParentX = Location.X;
ParentY = Location.Y;
Frm.ShowDialog();
}
and this code is in form2 :
private void Form2_Load(object sender, EventArgs e)
{
Location = new Point(Form1.ParentX + 385, Form1.ParentY + 120);
}
when form1 is in the normal position, the code works as I want, but when form1 is in Maximize position, Form2 is no longer in the position I want. I want form2 to appear in the same position when form1 is in Normal and Maximal positions. how to achieve that?
sorry if this question is wrong, I'm still in the learning stage, and not very good at English.

Does this Work?
if (Form1.WindowState == FormWindowState.Maximized)
{
//Place form
} else {
Location = new Point(Form1.ParentX + 385, Form1.ParentY + 120);
}

Related

move Form2 according to form1

My problem is:
i want Form 2 to be moved only when I move form 1, and it will always stay beneath Form1.
i tried everything i could think of: Location point and set desktop position, i tried a timer of realtime moving , i just cant get it , this shouldn't be so difficult :(
i'm using a panel to move form 1
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mov = 1;
movX = e.X;
movY = e.Y;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mov == 1)
{
this.SetDesktopLocation(MousePosition.X - movX, MousePosition.Y - movY);
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mov = 0;
}
i also tried to make One form and put panels in it and make the form transparent, but i get an issue when i'm trying to move the form via the panel.
i found solution on this:
Move Form1 when I move Form2 in C#
in first form:
protected override void OnLocationChanged(EventArgs e)
{
if (secondForm == null)
{
secondForm = new SecondForm();
}
if (secondForm.wasShown == true)
{
secondForm.Location = new Point(this.Left - parentOffset.X,
this.Top - parentOffset.Y);
}
base.OnLocationChanged(e);
}
in the second form:
public bool wasShown = false;
private void SecondForm_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.Manual;
Location = new Point(topForm.lX -40, topForm.lY +85);
wasShown = true;
this.Owner = topForm;
}

Drawing from one Form on another while the one is closing/closed

I am programming something in c# for school. I have 2 Forms and while I close the child form I want to draw something on the parent Form and I don't want to use any button I would have to click afterwards. This is what I have tried but it doesn't work.
private void button1_Click(object sender, EventArgs e)
{
this.Close(); //Closes Form2(Child)
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 f1 = new Form1();
Graphics g;
g = f1.CreateGraphics();
g.DrawRectangle(Pens.Black, 100, 100, 50, 50);
}
I also have tried using the FormClosed event.
On your parent form, when you initialize the child form, set it's Owner property to this:
var form2 = new Form2();
form2.Owner = this;
And then from the child form, you can access the parent by doing this:
Graphics g;
g = ((Form1)this.Owner).CreateGraphics();
g.DrawRectangle(Pens.Black, 100, 100, 50, 50);
Taking in consideration your Form1 is the parent form and Form2 is your child form, that is Form2 is being called from Form1 originally. Then you should first send your copy of Form1 to the new Form2 you are creating, stored it on a private field and on closing use it to draw on it. Something similar to:
private Form1 _parent;
public Form2(Form1 parent)
{
_parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close(); //Closes Form2(Child)
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Graphics g = _parent.CreateGraphics();
g.DrawRectangle(Pens.Black, 100, 100, 50, 50);
}
Also on parent, you should have something similar to:
private void button1_Click(object sender, FormClosingEventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
The problem with the code you posted is that you are creating a new Form1, which is not the same object/instance as the original Form1 you have already when opening a new instance Form2.

How to increase the name of a new form when opening a new child form

How do you change the text in the Titlebar in Windows Forms?
When I open a new form I want it to say "Bob, the next time i click new and open a form it should say "Bob1"
I tried to use TryParase() it to string and that wont work.
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 childform2 = new Form2();
decimal childcount;
childform2.MdiParent= this;
string menuname;
menuname = "untilted" + childcount.ToString();
childform2.Text = menuname;
childform2.Show();
childcount++;
}
You need to keep childcount around longer than just that method. Since you declare it inside the method, it will reset to 0 each time you run this code. Declare the variable outside of the method, so something like this:
int childcount=1;
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 childform2 = new Form2();
childform2.MdiParent= this;
string menuname;
menuname = "untilted" + childcount.ToString();
childform2.Text = menuname;
childform2.Show();
childcount++;
}

How can I display a form below textbox after textbox is clicked?

Currently Form1 has textBox1 and Form1 has StartPosition = CenterScreen, the textBox1 has textBox1_MouseClick
Code for textBox1_MouseClick
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
In Form2 has also StartPosition = CenterScreen when I click textBox1
the Form2 will cover the textBox1.
What I want to happen is that it will not cover the textBox1 when the Form2 will be displayed, it should be displayed under textBox1 its like a tooltip. How can I achieve this?
UPDATED CODE:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
Form2 frm2 = new Form2();
frm2.StartPosition = FormStartPosition.Manual;
frm2.Location = new Point(this.Location.X + textBox1.Location.X, this.Location.Y + textBox1.Location.Y);
frm2.ShowDialog();
}
private void textBox2_MouseClick(object sender, MouseEventArgs e)
{
Form2 frm2 = new Form2();
frm2.StartPosition = FormStartPosition.Manual;
frm2.Location = new Point(this.Location.X + textBox2.Location.X, this.Location.Y + textBox2.Location.Y);
frm2.ShowDialog();
}
NO TEXTBOX CLICKED:
TEXTBOX1 CLICKED:
TEXTBOX2 CLICKED:
What I have posted before is a generic solution for the long-term. To quickly address the problem at hand, you should simply do this:
frmKeyboard.Location = this.PointToScreen(new Point(txtYourTextBox.Left, txtYourTextBox.Top + txtYourTextBox.Height));
You should better do it with a UserControl instead of a form. Simply set the position of your UserControl to (textbox1.Left, textbox1.Top + textbox1.Height).
You should rather add a custom TextBox class in your project inheriting from standard TextBox and wire its Enter/Leave events, showing/hiding your keyboard control therein and wire its "keypress" event to modify the Text of your custom textbox. This will let you create as many instances of the textbox as you need. You could even make your keyboard control a static member of your custom TextBox to save some resources.
You can also use something like this:
private Point GetPosition()
{
return new Point(this.Location.X + this.textBox1.Location.X, this.Location.Y + this.textBox1.Location.Y);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 fm = new Form2();
fm.Location = this.GetPosition();
fm.ShowDialog();
}
This is not accurate yet. You have to add Form Borderwith to the position. For Form2 set StartPosition = Manual

Find, Find Next?

I am trying to make a find, find next function for my program, which I did manage to do with this code:
int findPos = 0;
private void button1_Click(object sender, EventArgs e)
{
try
{
string s = textBox1.Text;
richTextBox1.Focus();
findPos = richTextBox1.Find(s, findPos, RichTextBoxFinds.None);
richTextBox1.Select(findPos, s.Length);
findPos += textBox1.Text.Length;
//i = richTextBox1.Find(s, i + s.Length, RichTextBoxFinds.None);
}
catch
{
MessageBox.Show("No Occurences Found");
findPos = 0;
}
}
And it works great in form1 but if I use this code and try to call it from form2 It doesn't do anything:
//Form1
public void FindNext()
{
try
{
this.Focus();
Form2 frm2 = new Form2();
string s = frm2.textBox1.Text;
richTextBox1.Focus();
findPos = richTextBox1.Find(s, findPos, RichTextBoxFinds.None);
richTextBox1.Select(findPos + 1, s.Length);
findPos += textBox1.Text.Length;
}
catch
{
MessageBox.Show("No Occurences Found");
findPos = 0;
}
}
//Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.FindNext();
}
Does any one know why this is?
Thanks,Tanner.
string s = Interaction.InputBox("enter search text", "Notepad-search", "", 100, 100);
//The above syntax is from vb.net so add reference as microsoft.VisualBasic from references. The above code creates an alertbox. Then type the text which you want search and click on ok.
int f = richTextBox1.Find(s);
if (f >= 0)
{
MessageBox.Show("search Text is found");
}
else
{
MessageBox.Show("search Text is not found");
}
I think you may be confused in how you reference Form1 and Form2 from each other.
Calling new Form() and new Form2() create references to new instances of Form1 and Form2, they don't reference the forms that are already open. You need to get the references for the existing instances.
Assuming that Form1 is the main form for your application and it creates and shows Form2, you can either add a property to Form2 that represents the instance of Form1 that created it, or you can appropriate the Owner property for this purpose (I'd recommend that).
In your code on Form1 that shows Form2 initially (not in the code you have above), call frm2.Show(this) instead of just frm2.Show(). This will set the Owner property of your Form2 instance equal to thinstance of Form1 that opened it.
Then change your button code for Form2 to this:
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)Owner;
frm1.FindNext();
}
This will make you reference the existing form rather than a new one, which is what you want.
As far as the FindNext function goes, you have two choices: either you can hold on to the reference of Form2 (though you probably want to do this anyway) and access the text directly, or you can change FindNext to take a string (this is what I'd recommend).
public void FindNext(string searchText)
{
try
{
this.Focus();
richTextBox1.Focus();
findPos = richTextBox1.Find(searchText, findPos, RichTextBoxFinds.None);
richTextBox1.Select(findPos + 1, searchText.Length);
findPos += searchText.Length;
}
catch
{
MessageBox.Show("No Occurences Found");
findPos = 0;
}
}
Then change the call to frm1.FindNext() on Form2 to frm1.FindNext(textBox1.Text):
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)Owner;
frm1.FindNext(textBox1.Text);
}
Looks like you are referencing two different instances of Form2.
In your Form1.FindNext() you have a new instance of Form2 that you are creating and getting the text value from which is different to the instance where you are calling your FindNext() from.
What you might want to do is to pass in the instance of the form to FindNext(). So your function would be...
//Form1
public void FindNext(Form2 frm2)
{
try
{
this.Focus();
string s = frm2.textBox1.Text;
richTextBox1.Focus();
findPos = richTextBox1.Find(s, findPos, RichTextBoxFinds.None);
richTextBox1.Select(findPos + 1, s.Length);
findPos += textBox1.Text.Length;
}
catch
{
MessageBox.Show("No Occurences Found");
findPos = 0;
}
}
//Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.FindNext(this);
}
By writing Form1 frm1 = new Form1();, you're creating a brand-new instance of the Form1 form, which never gets any text and is never shown to the user.
You need to pass the original Form1 instance to Form2 in Form2's constructor.
Similarly, when you write Form2 frm2 = new Form2(); in FindNext, you're making a brand-new Form2 instance without any text.
Instead, you should pass the text as a parameter to the FindNext method.
For example:
public void FindNext(string searchText) {
...
findPos = richTextBox1.Find(searchText, findPos, RichTextBoxFinds.None);
...
}
originalForm.FindNext(textBox1.Text);
Your textbox on the new instance of frm1 will have no value surely? So there is nothing for the method to do...
Try stepping through the code and checking you actually have values to work with?
When you say:
Form1 frm1 = new Form1();
You are creating a fresh version so any extra information thats been added you dont have when accessing frm1
Try this and you will see what I mean
Form1 frm1 = new Form1();
frm1.Show();
When this code is excecuted you will see that you have actually made another instance of your form.
What you need to do is work with the original instance rather than create a new one, so that you still have all that information in your textboxes.
I'll leave you to work this one out, but there is you answer :)

Categories