how to make a vertical progress bar - c#

I'm trying to make a vertical progress bar and I understand that there isn't any easy way to do it.
I've seen this code floating around the forums:
public class VerticalProgressBar : ProgressBar
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0x04;
return cp;
}
}
}
My question is where do I put this code? Does it go in my program.cs file or the form that the progress bar is on?

It doesn't matter where you put the code, you only have to make sure you are creating a VerticalProgressBar in your Form.Designer.cs file.
You have to change
private System.Windows.Forms.ProgressBar progressBar1
to
private VerticalProgressBar progressBar1
(or whatever it is called) and
this.progressBar1 = new System.Windows.Forms.ProgressBar();
to
this.progressBar1 = new VerticalProgressBar();

Still don't have this control available in VS 2022 so I used a TablePanelLayout with one column and two rows. and just changed the RowStyle SizeType percent. You can easily add code to change the panel color when a limit is reached etc.
public class BarGraph : Panel
{
private Panel panel1;
private Panel panel2;
private TableLayoutPanel table;
private float percentValue;
public BarGraph()
{
table = new TableLayoutPanel();
table.ColumnCount = 1;
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
table.Name = "tableLayoutPanel1";
table.RowCount = 2;
table.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
table.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
table.Dock = DockStyle.Fill;
table.Margin = new Padding(0);
table.Padding = new Padding(0);
table.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;
panel1 = new Panel();
panel2 = new Panel();
panel1.AutoSize = false;
panel1.Dock = DockStyle.Fill;
panel1.Padding = new Padding(0);
panel1.Margin = new Padding(0);
panel2.AutoSize = false;
panel2.Dock = DockStyle.Fill;
panel2.Padding = new Padding(0);
panel2.Margin = new Padding(0);
panel1.BackColor = System.Drawing.Color.White;
panel2.BackColor = System.Drawing.Color.Blue;
table.Controls.Add(panel1, 0, 0);
table.Controls.Add(panel2, 0, 1);
this.Controls.Add(table);
this.Size = new System.Drawing.Size(150, 500);
SetValue("50");
}
public float GetValue()
{
return this.percentValue;
}
public void IncreaseValue()
{
IncrementValue(percentValue - 1);
}
public void DecreaseValue()
{
IncrementValue(percentValue + 1);
}
private void IncrementValue(float value)
{
SetValue(value.ToString());
}
public void SetValue(string value)
{
float.TryParse(value, out percentValue);
if ((percentValue >= 0) && (percentValue <=100))
{
table.RowStyles.RemoveAt(1);
table.RowStyles.RemoveAt(0);
table.RowStyles.Add(new RowStyle(SizeType.Percent, 100 - percentValue));
table.RowStyles.Add(new RowStyle(SizeType.Percent, percentValue));
}
}
}

If this is a brand new application, use WPF. vertical progress bars are built-in
<ProgressBar Orientation="Vertical" />

I found this post while looking for a way to make a vertical progress bar and used code in the question as the answer to my own problem. The accepted is right for sure so ✔ and ✔.
I'm adding this as just a bit of added value hopefully. In my own project I also needed to customize the color. I ended up using the OP's question to make my class and modified it so that ForeColor can be set to change the indicator color. All this and a working sample to boot.
class VerticalProgressBar : ProgressBar
{
public VerticalProgressBar() => SetWindowTheme(Handle, string.Empty, string.Empty);
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0x04;
return cp;
}
}
[DllImport("uxtheme", ExactSpelling = true, CharSet = CharSet.Unicode)]
public extern static Int32 SetWindowTheme(IntPtr hWnd,
String textSubAppName, String textSubIdList);
}
Designer code
"Where to put the code" (as stated in the accepted answer).
private void InitializeComponent()
{
this.progressBarTemperature = new weather_client.VerticalProgressBar();
this.SuspendLayout();
.
.
.
//
// progressBarTemperature
//
this.progressBarTemperature.Location = new System.Drawing.Point(35, 12);
this.progressBarTemperature.Name = "progressBarTemperature";
this.progressBarTemperature.Size = new System.Drawing.Size(20, 113);
this.progressBarTemperature.TabIndex = 2;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(385, 144);
this.Controls.Add(this.progressBarTemperature);
.
.
.
this.Name = "MainForm";
this.Text = "Main Form";
this.ResumeLayout(false);
}
private VerticalProgressBar progressBarTemperature;

Related

how to make touchscreen-like mainform C#

I want to make my mainform draggable like smartphone touchscreen.
So i put up a bunifugradientpanel in it and dock it in mainform. also put a bunifudragcontrol and set targetcontrol property = 'bunifugradientpanel' and vertical property to 'false' also fixed property to 'false'. however, whenever i drag my panel to the right in rumtime, the portion of the mainform is showing which is the white part in the picture.
The white part of the screen is the mainform. what i want is to stop the dragging activity if bunifugradientpanel location on mainform is = (x=0,y=0) so the the portion of the mainform wont appear. thanks for your help guys.
BunifuGradientPanel is a Third Party control and that's why it can present drawing and flickering problems, my recommendation is that you use a common Panel System.Windows.Forms.Panel
That said, I created this code for you, makes a control draggable only from right to left and from left to right while the mouse button is pressed:
using System;
using System.Windows.Forms;
namespace JeremyHelp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer,
true);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.SetDraggable(this.bunifuGradientPanel1, vertical: false);
}
protected override CreateParams CreateParams
{
get
{
CreateParams handleParam = base.CreateParams;
handleParam.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
return handleParam;
}
}
private void SetDraggable(Control target, bool horizontal = true, bool vertical = true)
{
bool IsDraggable = false;
int
X = 0, Y = 0,
A = 0, B = 0;
target.MouseUp += (s, e) =>
{
IsDraggable = false;
X = 0; Y = 0;
A = 0; B = 0;
};
target.MouseDown += (s, e) =>
{
IsDraggable = true;
A = Control.MousePosition.X - target.Left;
B = Control.MousePosition.Y - target.Top;
};
target.MouseMove += (s, e) =>
{
X = Control.MousePosition.X;
Y = Control.MousePosition.Y;
if (IsDraggable)
{
if (horizontal) target.Left = X - A;
if (vertical) target.Top = Y - B;
}
};
}
}
}

C# - Hide created form on switch window (alt + tab) from code

I have a form that can create another form like this.
private void AEGISBot(String option) {
if (AEGIS == null) {
AEGIS = new Form();
AEGIS.ShowInTaskbar = false;
AEGIS.TopMost = true;
AEGIS.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
AEGIS.Size = new Size(396, 191);
//AEGIS.Size = new Size(720, 720);
AEGIS.StartPosition = FormStartPosition.CenterScreen;
AEGIS.BackColor = Color.LightBlue;
AEGIS.TransparencyKey = AEGIS.BackColor;
Label AEGISLabel = new Label();
AEGISLabel.Location = new Point(0, 0);
AEGISLabel.Size = new Size(AEGIS.Size.Width, AEGIS.Size.Height);
AEGISLabel.TextAlign = ContentAlignment.MiddleCenter;
AEGISLabel.Text = "AEGIS";
AEGISLabel.Font = new Font("Agency FB", 120, FontStyle.Bold);
AEGISLabel.ForeColor = System.Drawing.Color.Navy;
AEGIS.Controls.Add(AEGISLabel);
}
if (option == "show"){
AEGIS.Show();
}
}
But how to hide it from alt tab. I tried to add code like this.
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x80;
return cp;
}
}
My main form was successfully hidden from alt tab. But how to use it to created form ??
Thank you
-Edit
I am using Windows Form Application. With some form setting
this.ShowInTaskbar = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ShowIcon = false;
this.WindowState = FormWindowState.Minimized;
Try using AEGIS.ShowDialog() instead of AEGIS.Show().

ToolStripDropDown transparent background and smooth Region

I'm trying to create a custom popup window in my winforms project, which looks like so:
The problem with this is that the edges are smooth...
I'm achieving this like so:
public partial class BubblePopup : ToolStripDropDown
{
private const int BORDERWIDTH = 6;
private SolidBrush _backgroundBrush;
private int _borderRadius = 20;
public BubblePopup()
{
this.BackColor = Color.Transparent;
InitializeComponent();
//Method 1
Region = new Region(ControlUtilities.CreateBubblePath(new Rectangle(BORDERWIDTH - 1, BORDERWIDTH - 1, ClientSize.Width - (BORDERWIDTH * 2), ClientSize.Height - (BORDERWIDTH * 2)), _borderRadius));
/////////////
_backgroundBrush = new SolidBrush(Color.Blue);
}
//Method 1
protected override void OnSizeChanged(EventArgs e)
{
Region = new Region(ControlUtilities.CreateBubblePath(new Rectangle(BORDERWIDTH - 1, BORDERWIDTH - 1, ClientSize.Width - (BORDERWIDTH * 2), ClientSize.Height - (BORDERWIDTH * 2)), _borderRadius));
base.OnSizeChanged(e);
}
////////
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle = cp.ExStyle | 0x20;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
}
}
If I don't use the code enclosed by Method1 comments and I'm setting the background to transparent and setting the CreateParams flag to support transparency im getting the following result(I also tried overriding the OnPaintBackground event as well not to call the base.OnPaintBackground I got a black background then):
So I would either need a popup with my custom path with smooth region, or a transparent background would also work for me.
Also here's the code how I'm showing the popup:
private GeneralPopup _popupItem = new GeneralPopup();
private ToolStripControlHost _popupControlHost;
private BubblePopup _popup;
public Form1()
{
InitializeComponent();
_popupControlHost = new ToolStripControlHost(_popupItem);
_popupControlHost.Padding = new Padding(0);
_popupControlHost.Margin = new Padding(0);
_popupControlHost.AutoSize = false;
_popupControlHost.BackColor = Color.Transparent;
_popup = new BubblePopup();
_popup.Padding = new Padding(0);
_popup.Margin = new Padding(0);
_popup.AutoSize = true;
_popup.DropShadowEnabled = false;
_popup.Items.Add(_popupControlHost);
}
private void button1_Click(object sender, EventArgs e)
{
_popup.Show(button1,10,10);
}
Thanks in advance

keep button position on panel resize

I've been messing around with something that should be simple.
I've moved jobs and trying to get some of the basic tools I have used before, but of course I don't have the old source to look at.
We had extended panel to have some standard properties and functions (save, close, save and close).
But I can't get the buttons to be positioned correctly on a resize. I put this ExtPanel on a form but the buttons keep disappearing as I resize, or don't move as expected (frozen on bottom right).
The class
public partial class ExtPanel: UserControl
{
private System.Windows.Forms.Button btnSaveandClose;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnSave;
public ExtPanel ()
{
InitializeComponent ();
}
// misc things this class does...
}
public partial class ExtPanel
{
private void InitializeComponent ()
{
this.btnSaveandClose = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// btnSaveandClose
//
this.btnSaveandClose.Location = new System.Drawing.Point(899, 689);
this.btnSaveandClose.Name = "btnSaveandClose";
this.btnSaveandClose.Size = new System.Drawing.Size(100, 30);
this.btnSaveandClose.TabIndex = 0;
this.btnSaveandClose.Text = "Save and Close";
this.btnSaveandClose.UseVisualStyleBackColor = true;
this.btnSaveandClose.Click += new System.EventHandler(this.Click_SaveandClose);
this.btnSaveandClose.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(687, 689);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(100, 30);
this.btnCancel.TabIndex = 1;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.Click_Close);
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(793, 689);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(100, 30);
this.btnSave.TabIndex = 2;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.Click_Save);
this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.btnSave);
this.panel1.Controls.Add(this.btnCancel);
this.panel1.Controls.Add(this.btnSaveandClose);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(1008, 730);
this.panel1.TabIndex = 0;
//
// ExtPanel
//
this.Controls.Add(this.panel1);
this.Name = "ExtPanel";
this.Size = this.panel1.Size;
this.Click += new System.EventHandler(this.click_this);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
}
Have you tried anchoring the button?
...I missed the anchoring code.
If you have a button in the lower right-hand corner that you want to stay in the lower right-hand corner when resizing the form, set it's anchor properties to Bottom, Right.
Update:
I loaded your code. You have a panel inside of ExtPanel. If you dock (Fill) that panel then you should be working fine by resizing ExtPanel.

Adding custom components programmatically at runtime

I'm having a problem using custom components at runtime.
I have this custom FlowLayoutPanel component:
public partial class UserControl1 : System.Windows.Forms.FlowLayoutPanel
{
[Browsable(false)]
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e){}
public UserControl1(){}
}
it has nothing complicated. All it does is making the background transparent.
I added this component to my Toolbox using it's dll and it works perfectly fine when I drag and drop it to my Form. The problem is, I can't add it programmatically at runtime.
When I run the code below it should draw the picture on top of my custom FlowLayoutControl.
But unfortunately it does nothing.
The custom component is under the WindowsFormsControlLibrary1 namespace.
namespace MyFilm_v2._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
UserControl1 test = new UserControl1();
test.BackColor = Color.Transparent;
test.Location = new Point(0, 110);
test.Width = 660;
test.Height = 478;
PictureBox b = new PictureBox();
b.Location = new Point(100, 100);
b.Width = 320;
b.Height = 475;
b.Image = Properties.Resources.movie;
this.Controls.Add(b);
//this.customScrollbar1.Minimum = 0;
//this.customScrollbar1.Maximum = test.DisplayRectangle.Height;
//this.customScrollbar1.LargeChange = customScrollbar1.Maximum / customScrollbar1.Height + test.Height;
//this.customScrollbar1.SmallChange = 15;//when click the arrows
//this.customScrollbar1.Value = Math.Abs(test.AutoScrollPosition.Y);
}
....
You adding picturebox to your test controls, but you not adding test to forms controls
public Form1()
{
InitializeComponent();
UserControl1 test = new UserControl1();
test.BackColor = Color.Transparent;
test.Location = new Point(0, 110);
test.Width = 660;
test.Height = 478;
PictureBox b = new PictureBox();
b.Location = new Point(100, 100);
b.Width = 320;
b.Height = 475;
b.Image = Properties.Resources.movie;
test.Controls.Add(b);
this.Controls.Add(test);//<- here
}

Categories