How to move form box to a new location - c#

I have a windows form project, and I want the whole form to change location automatically, but the truth is that I have no idea what to call, and where to call it. I have searched online, and all code I discovered was incomplete. I am fairly new at this, so it did not help me.
Here is the code that I am working with, if it helps:
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.Media;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private SoundPlayer _soundplayer;
public Form1()
{
InitializeComponent();
SoundPlayer player = new SoundPlayer(Properties.Resources.sound);
player.Play();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
var myForm = new Form2();
myForm.Show();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
_soundplayer.PlayLooping();
}
}
}

Change the Location for the form:
this.Location = new Point(400, 500);
You just need to decide which event will trigger this code; for example, the Click event of a button.
MSDN: Location

To position forms using the Properties window
In the Properties window, choose the form from the drop-down. Set the form's StartPosition property to Manual.
Type the values for the Location property, separated by a comma, to position the form, where the first number (X) is the distance from the left border of the display area and second number (Y) is the distance from the upper border of the display area.
Note Expand the Location property to enter the X and Y subproperty values individually.
Reference MSDN

Related

FormLayoutPanel, Not Changing Direction when using LinkLabels

I'm trying to use the FlowLayoutPanel with linkLabels in Visual Studio for a quick project. I've selected "TopDown" for direction and wrapping to false. When I launch the program; however, the direction always shows left to right. Is there a box or something that I haven't checked? Or is there any reason a linklabel would ignore the flow direction?
Here's my code and some screenshots of what I see.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace myProject
{
public partial class Form1 : Form
{
FlowLayoutPanel panel = new FlowLayoutPanel();
public Form1()
{
InitializeComponent();
linkLabel1.LinkClicked += linkLabel1_LinkClicked;
linkLabel2.LinkClicked += linkLabel2_LinkClicked;
linkLabel3.LinkClicked += linkLabel3_LinkClicked;
Controls.Add(panel);
panel.Controls.Add(linkLabel1);
panel.Controls.Add(linkLabel2);
panel.Controls.Add(linkLabel3);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel1, 0);
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel2, 0);
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel3, 0);
}
}
}
This is the control view before I've started the program.
This is what I see when I run the program - marked with the red arrow.
Because you are initializing your FlowLayoutPanel in the code-behind, you have to set the FlowDirection property of this new instance of FlowLayoutPanel in the same code-behind:
FlowLayoutPanel panel = new FlowLayoutPanel();
public Form1()
{
InitializeComponent();
panel.FlowDirection = FlowDirection.TopDown;
The FlowLayoutPanel that you declare in your code-behind is separate from the one you have in your layout, so the FlowDirection property is not set the same. I tested the code above and I believe it does what you were looking for.

c# winforms creating pictureboxes dynamically at runtime doesn't work

I'm trying to create PictureBoxes dynamically at runtime with c# winforms.
My project: I want to write a program, which has a node-GUI (a GUI with various types of nodes, some kind of boxes, which are connected together and process an image, an audio stream or whatever).
Therefor i want to create and delete Pictureboxes dynamically at runtime, but my testing won't work, the form is empty.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AudioNodeGUI
{
public partial class AudioNodeWindow : Form
{
public AudioNodeWindow()
{
InitializeComponent();
}
private void AudioNodeWindow_Load(object sender, EventArgs e)
{
}
private void AudioNodeWindow_Paint(object sender, PaintEventArgs e)
{
PictureBox start_picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(19, 32),
Location = new Point(100, 100),
Visible = true,
Image = Bitmap.FromFile(#"C:\Users\Benjamin.MBENJAMIN\Pictures\Start.png"),
};
start_picture.Show();
}
}
}
Please help !
You need to add the control you created to the Forms control.
Before you Show() the picturebox, try adding this line:
Controls.Add(start_picture);
Secondly, you don't want to doing this onPaint()!
I would say you need to move it to Load() method instead, that way it will be done when the form loads, rather than everytime it's repainted!
Change:
start_picture.Show();
to:
this.Controls.Add(start_picture);
start_picture.Show();
Controls.Add tells the form that the PictureBox is meant to be part of this specific form.
Also, you will not want to do this in in your Paint event handler. Leaving it there will result in many more picture boxes than you would like I expect...
I have change your code as :
PictureBox start_picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(19, 32),
Location = new Point(100, 100),
Visible = true,
Image = Bitmap.FromFile(#"D:\test\learn.png"),
};
//start_picture.Show();
Controls.Add(start_picture);

C# smooth resize of the form (smooth resize efect)

First of all, beginner here, apologies in advance if my question has obvious answer, but until now, I didn't manage to figure it out.
So, I'm trying to make an Windows Form App. In this app I have two forms: the login form and main window form. To make it look more "fancy", after successfully logging in, the main window form must increase its size (same size as the login form) to something bigger. I managed to create the code I need, but the resize effect looks "laggy". Is there a way to make this smother? Or is there another way to make the resize of the form smoother?
PS. I have set the "FormBorderStyle" property to "None"
Bellow, my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SlindingPanel
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
// Enables the timer
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Width >= 820 && this.Height >= 540) this.timer1.Enabled = false;
else {
// Resizes the form
this.ClientSize = new System.Drawing.Size(Width + 20, Height + 20);
//Centers the form on the screen
this.CenterToScreen();
}
}
}
This may be late, but I suggest using the "anchor" attribute of the UI controls.

C# bound table adapter not updating until called twice

New to C# from vb.net and I am just making some mock bound applications for now. I have problems with the following code. If I pic an image and exit the application, there is no change. Even if I move a row. However if I upload an image, move to another row, then add another image. After exiting the application the first image will be there but not the second.
In short I have to attempt to upload to another record before the record I actually want updating will do so.
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.IO;
namespace DBUserManagement
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dsUsers.Users' table. You can move, or remove it, as needed.
this.usersTableAdapter.Fill(this.dsUsers.Users);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (DialogResult.OK == ofd.ShowDialog())
{
imgUser.SizeMode = PictureBoxSizeMode.StretchImage;
imgUser.Image = new Bitmap(ofd.OpenFile());
//update bound field.
usersTableAdapter.Update(dsUsers);
}
}
}
}
Any ideas on what I am missing or not understand correctly? Any help appreciated.
/P
The answer was I needed to call the BindingSource's .EndEdit(); method.
So I am guessing it was down to the binding source still having hold of something.
Seems like I'm on the right track anyhow, I looked up the details on MSDN :)

User Placed Text on a picturebox in c#

I am trying to make a tool that allows me to choose a certain location on a picturebox to put text from a textbox on. It will need to be able to place multiple different texts on the picturebox and then be able to be deleted. This is my current 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 TextboxTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Visible = true;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Graphics G = Graphics.FromImage(pictureBox1.Image);
G.DrawString(textBox1.Text, new Font("Tahoma", 40), Brushes.Black, new Point(MousePosition.X, MousePosition.Y));
}
}
}
At the moment i can type the text in the textbox, but can't draw the string on the picturebox and choose its location. I have a button which is meant to confirm the text written is right and then allow the user to choose its location. Please can someone help me sort this code out?
Thanks-
The MousePosition property is relative to the screen, not the PictureBox.
You should handle the MouseClick event and draw the string at e.X and e.Y.
Alternatively, you can call pictureBox1.PointToClient to transform screen coordinates to control-relative coordinates.
Also, you should dispose the Graphics object in a using statement.
Finally, I'm pretty sure you'll need to call pictureBox1.Invalidate() after modifying the image to force it to repaint.

Categories

Resources