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.
Related
I did :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
private int x, y;
private int gap = 0;
private int startingY = 83;
private GroupBox lastGB = null;
public Form1()
{
InitializeComponent();
button1.Text = "Details " + char.ConvertFromUtf32(8595);
}
The result is something bad not looking good :
This is what I want to make : The NEW is acting like a button it is a button. The arrow near it is like a combobox :
If I click on the NEW it will open folder dialog if I click on the arrow it will open kind of combobox without the borders.
This is called SplitButton. This question has been asked few times over the years.
You can draw an inspiration from the Windows Forms codebase.
Or look at other answers to the same question: Split button in .NET Winforms.
Or search online for SplitButton.
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);
I have a Main Screen form set up, with it's body consisting of one giant panel. I then have a series of User Controls which get erased or loaded into that Main Screen. What I don't know how to do is have the User Controls affect the Main Screen.
Code Example from a User Control:
PartsSystem parts = new PartsSystem(); //a user control
MainMenu.pnlMain.Controls.Clear();
MainMenu.pnlMain.Controls.Add(parts);
This code works when loading the main screen, but I can't seem to access pnlMain from my other user controls. Is it possible to fix this? I have a function set up to grab a user Control within the MainMenu form, but I can't seem to get it to be called from a user control. I'm trying to make it so that when they click a button, they go to the next screen (user control).
I am using C# on Windows 7 with Visual Studio 2010 and all the default settings.
Here is some additional code samples:
public partial class Main_Menu : Form
{
public Main_Menu()
{
InitializeComponent();
MainMenuPanel main = new MainMenuPanel();
panelChange(main);
}
public void panelChange(UserControl newPanel)
{
pnlMain.Controls.Clear();
pnlMain.Controls.Add(newPanel);
}
}
That was Main_Menu. This code works on launch. Now, bringing up my PartsSystem is another issue.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Program_v1._0._0._0._4
{
public partial class MainMenuPanel : UserControl
{
public MainMenuPanel()
{
InitializeComponent();
Main_Menu parentForm = (this.Parent as Main_Menu);
}
public void btnPartsInventory_Click(object sender, EventArgs e)
{
Main_Menu myParent = (this.Parent as Main_Menu);
PartsSystem parts = new PartsSystem();
myParent.pnlMain.Controls.Clear(); //Object reference not set to an instance of an object.
//This is the error I get at this point. It won't let me use the function.
myParent.pnlMain.Controls.Add(parts);
}
}
}
Thank you for the help!
Try using UserControl.Parent to get access the parent control.
access form from usercontrol
Get access to parent control from user control - 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
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 :)