I want to build a Windows Forms App that has a menu (several labels) on it's left side which is toggled. On the right side there should be some columns i can scroll through. Jst like Excel with it's fixed rownumbers.
Is there a way to do this? Preferably an easy one.
I think you can use two panels to make the form like the picture you provided.
The following code is a code example and you can refer to it.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ScrollBar hScrollBar1 = new HScrollBar();
private void Form1_Load(object sender, EventArgs e)
{
panel1.BorderStyle = BorderStyle.FixedSingle;
panel1.Dock = DockStyle.Left;
panel2.BorderStyle = BorderStyle.FixedSingle;
panel2.Dock = DockStyle.Fill;
hScrollBar1.Dock = DockStyle.Bottom;
hScrollBar1.Scroll += new ScrollEventHandler(hScroller_Scroll);
panel2.Controls.Add(hScrollBar1);
panel2.HorizontalScroll.Visible = false;
panel2.HorizontalScroll.Enabled = true;
}
private void hScroller_Scroll(object sender, ScrollEventArgs e)
{
panel2.HorizontalScroll.Value = e.NewValue;
}
}
The specific result:
Related
I'm using WinForms. In my form i have 2 panels which i want to dock up or down on button click.
The issue i'm running into is that my panels is not docking correctly.
When i click on the up button, panel one label gets covered by panel2.
Panel 1: (Anchor: Top, Left, Right)
Panel 2: (Anchor: Top, Bottom, Left, Right)
private void Up_Btn_Click(object sender, EventArgs e)
{
panel1.Dock = System.Windows.Forms.DockStyle.Fill;
panel2.Dock = System.Windows.Forms.DockStyle.Top;
}
private void Down_Btn_Click(object sender, EventArgs e)
{
panel1.Dock = System.Windows.Forms.DockStyle.Fill;
panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
}
Incorrect the label should not be covered by the panels
What supposed to happen when Up button is clicked
What supposed to happen when Down button is clicked
public Form1()
{
InitializeComponent();
panel1.BringToFront();
}
private void Up_Click(object sender, EventArgs e)
{
panel1.Dock = DockStyle.Fill;
panel2.Dock = DockStyle.Top;
}
private void Down_Click(object sender, EventArgs e)
{
panel1.Dock = DockStyle.Fill;
panel2.Dock = DockStyle.Bottom;
}
The trick is to correct the order of the controls.
See here: Docking multiple controls - one fills remaining space
I've got a smaller image in my form. When the user hovers over the image it brings up a larger view of the image (that follows the mouse, but stays a certain distance from the mouse). In order to do this I am generating a Form with no FormBorderStyles when the cursor hovers the image.
The problem I'm running into is that the first form doesn't seem to detect any longer that the mouse is hovering or leaving the PictureBox once the form activates. The Form also doesn't follow the cursor.
Here is the slimmed down version of what I've got:
C#
bool formOpen = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseHover += pictureBox1_MouseHover;
pictureBox1.MouseLeave +=pictureBox1_MouseLeave;
}
void pictureBox1_MouseHover(object sender, EventArgs e)
{
if (formOpen == false)
{
Form form = new Form();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.BackColor = Color.Orchid;
//Show the form
form.Show();
form.Name = "imageForm";
this.AddOwnedForm(form);
//Set event handler for when the mouse leaves the image area.
//form.MouseLeave += form_MouseLeave;
//Set the location of the form and size
form.BackColor = Color.Black;
form.Dock = DockStyle.Fill;
form.Size = new Size(30, 30);
form.BackgroundImageLayout = ImageLayout.Center;
this.TopMost = true;
formOpen = true;
form.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
}
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
//MessageBox.Show("Worked");
}
}
The MouseLeave (and other events) was not recognized because the opening of the popup window and especially making it topmost=true took away the focus from the original form and its PictureBox.
It also didn't move because not code for moving was provided..
Here are a few changes that will make the form move:
You need a reference to it at the form1 level
you need to move it in the MouseMove event
Note that Hover is a once-only type of event. It fires only once until you leave the control.. (Note: Setsu has switched from Hover to Enter. This works fine, but lacks the short delay before showing the 2nd Form. If you want that back you can either switch back to Hover or you can fake the hover delay by a Timer, which is what I often do.)
// class level variable
Form form;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseEnter += pictureBox1_MouseEnter;
pictureBox1.MouseLeave +=pictureBox1_MouseLeave;
pictureBox1.MouseMove += pictureBox1_MouseMove; // here we move the form..
}
// .. with a little offset. The exact numbers depend on the cursor shape
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if ((form != null) && form.Visible)
{
form.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
}
}
void pictureBox1_MouseEnter(object sender, EventArgs e)
{
// we create it only once. Could also be done at startup!
if (form == null)
{
form = new Form();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//form.BackColor = Color.Orchid;
form.Name = "imageForm";
this.AddOwnedForm(form);
form.BackColor = Color.Black;
form.Dock = DockStyle.Fill;
form.Size = new Size(30, 30);
form.BackgroundImageLayout = ImageLayout.Center;
//this.TopMost = true; // wrong! this will steal the focus!!
form.ShowInTaskbar = false;
}
// later we only show and update it..
form.Show();
form.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
// we want the Focus to be on the main form!
Focus();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
if (form!= null) form.Hide();
}
MouseHover = Occurs when the mouse pointer rests on the control. (msdn)
Try MouseMove instead.
I am trying to open a (non decorated) childform at the upper right corner of my main form no matter if the main form is maximized or at it's normal size.
But no matter how I try I don't get it to open where I want it to.
I found a post that described how to open the form relative to another control in the form, but that didn't work either:
How to display a Modal form in a position relative to the a control in the parent window (opener)
Have tried to search for a few hours now on google for a solution, but either there's no answer (doubdfull) or I am not searching for the tight words combination (more likely).
Could anyone please either point me to a similar question, or help me how to achieve what I am hoping for?
Sounds to me you ought to be using a UserControl that you anchor to the top and right. But let's make a form work. You'll need to wire its Load event so you can move it into the right spot after it rescaled itself. Then you need the main form's LocationChanged and Resize events so you can keep the child form in the right spot.
So a sample program with boilerplate Form1 and Form2 names and a button on Form1 to display the child could look like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.button1.Click += button1_Click;
this.Resize += this.Form1_Resize;
this.LocationChanged += this.Form1_LocationChanged;
}
Form child;
private void button1_Click(object sender, EventArgs e) {
if (child != null) return;
child = new Form2();
child.FormClosed += child_FormClosed;
child.Load += child_Load;
child.Show(this);
}
void child_FormClosed(object sender, FormClosedEventArgs e) {
child.FormClosed -= child_FormClosed;
child.Load -= child_Load;
child = null;
}
void child_Load(object sender, EventArgs e) {
moveChild();
}
void moveChild() {
child.Location = this.PointToScreen(new Point(this.ClientSize.Width - child.Width, 0));
}
private void Form1_Resize(object sender, EventArgs e) {
if (child != null) moveChild();
}
private void Form1_LocationChanged(object sender, EventArgs e) {
if (child != null) moveChild();
}
}
Try something like that:
private void button1_Click(object sender, EventArgs e)
{
ChildForm win = new ChildForm();
int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
Point parentPoint = this.Location;
int parentHeight = this.Height;
int parentWidth = this.Width;
int childHeight = win.Height;
int childWidth = win.Width;
int resultX = 0;
int resultY = 0;
if ((parentPoint.X + parentWidth + childWidth) > screenWidth)
{
resultY = parentPoint.Y;
resultX = parentPoint.X - childWidth;
}
else
{
resultY = parentPoint.Y;
resultX = parentPoint.X + parentWidth;
}
win.StartPosition = FormStartPosition.Manual;
win.Location = new Point(resultX, resultY);
win.Show();
}
I think you should try something like this:
private void buttonOpen_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
//"this" is the parent form
frm2.DesktopLocation = new Point(this.DesktopLocation.X + this.Width - frm2.Width, this.DesktopLocation.Y);
}
Simple, easy, and works (for me).
I want to make a custom button control (image button is ok) like this one.
I'm a new user, so I can't post image here. So I uploaded the picture here
I'm kind of desperate right now after trying some tutorials
Any suggestion is highly appreciated.
Thanks
Updated 08/10/2019: I asked this question so many years ago, and at that time I didn't have the permission to upload image, so the image I uploaded to the third party site is long gone now. I got many requests about re-uploading the image, so here is what I remember from that project I did eight years ago, I just find some random images about window form that match my memory
This is when the button is in normal state
This is when the button is hovered or clicked, with the rounded border
You could create a class that inherits from Button to keep all your styling in one place. To do the hover and pressed states you can override the mouse enter / leave events of the button and change style.
Here is an example from one of our projects (I changed the colours but your get the idea). Where we change some colours you could switch the images.
public class MossieButton : Button
{
private static Font _normalFont = new Font("Arial", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
private static Color _back = System.Drawing.Color.Grey;
private static Color _border = System.Drawing.Color.Black;
private static Color _activeBorder = System.Drawing.Color.Red;
private static Color _fore = System.Drawing.Color.White;
private static Padding _margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
private static Padding _padding = new System.Windows.Forms.Padding(3, 3, 3, 3);
private static Size _minSize = new System.Drawing.Size(100, 30);
private bool _active;
public MossieButton()
: base()
{
base.Font = _normalFont;
base.BackColor = _border;
base.ForeColor = _fore;
base.FlatAppearance.BorderColor = _back;
base.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
base.Margin = _margin;
base.Padding = _padding;
base.MinimumSize = _minSize;
}
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
UseVisualStyleBackColor = false;
}
protected override void OnMouseEnter(System.EventArgs e)
{
base.OnMouseEnter(e);
if (!_active)
base.FlatAppearance.BorderColor = _activeBorder;
}
protected override void OnMouseLeave(System.EventArgs e)
{
base.OnMouseLeave(e);
if (!_active)
base.FlatAppearance.BorderColor = _border;
}
public void SetStateActive()
{
_active = true;
base.FlatAppearance.BorderColor = _activeBorder;
}
public void SetStateNormal()
{
_active = false;
base.FlatAppearance.BorderColor = _border;
}
}
Can't see the picture but I guess you can change the border of the button and set a background image.
button1.FlatStyle = FlatStyle.Flat;
button1.BackgroundImage = Bitmap.FromFile("image.jpg");
I think the simplest way is set some properties of the button like below and
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Image = "Any Image"
this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
then write the code for
private void button1_Click(object sender, EventArgs e)
{
//Code for Image Appearance.
button1.Text = "OnClick";
}
private void button1_MouseEnter(object sender, EventArgs e)
{
//Code for Image Appearance.
button1.Text = "Enter";
}
private void button1_MouseLeave(object sender, EventArgs e)
{
//Code for Image Appearance.
button1.Text = "Normal";
}
Update:
I don't know whether I am going correct or not but I think You can also achive your goal by putting a Button and a label inside a panel and arrange them according to your choice. Make the button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat at initial with Label.Text="Normal". Then on Mouse enter to the Panel draw a rectangle with a border around the button and change the text of the label to "Hover". Like that Clicking on the Panel also you change the rectangle border according to you and make the label.Text="OnClick".
Im trying to integrate a toolwindow in a Winforms application, it will be a tiny floating window to display element details in a listbox. What I need is pop the window in a relative position to the control that triggers the action, so here is the thing: the Location property gives me the relative position of the control from its container (the main form in this case) so this is the workaround im using:
public void Show(kTextBox source)
{
Point absCoord = source.PointToScreen(source.Location);
this.Location = this.PointToClient(absCoord);
base.Show();
}
Basically this is: get the absolute control position and set this position (previously converted into owner relative) to the toolwindow. I think it should work just fine but is missing for a certain degree, and it varies depending what control i use. Its kinda confusing. Been there anyone?? Thanks in advance.
What happens if you try the following:
public void Show(kTextBox source)
{
Point control_origin = source.PointToScreen(new Point(0, 0));
this.Location = new Point(control_origin.X, control_origin.Y);
base.Show();
}
private void button1_Click(object sender, EventArgs e)
{
ToolStripDropDown popup = new ToolStripDropDown();
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(frm);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(button1, button1.Left - 10, button1.Top + (int)(button1.Height / 2));
}
Form2 frm = new Form2();
private void Form1_Load(object sender, EventArgs e)
{
frm.TopLevel = false;
}
}