I would like to have a progress bar, where the value of the bar would rise if the button was being pressed down(MouseDown Event), the bar needs to rise simultaneousely.
Any ideas? I tried with a timer but this is all I have currently
private void button1_Click(object sender, EventArgs e)
{
progressBar1.PerformStep();
progressBar1.UseWaitCursor = true;
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
}
public void button1_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start();
}
public void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
}
It should look like this:
void timer_Tick(object sender, EventArgs e) {
progressBar1.PerformStep();
}
void button1_MouseDown(object sender, MouseEventArgs e) {
timer.Start();
}
void button1_MouseUp(object sender, MouseEventArgs e) {
timer.Stop();
}
The following program will update the progress bar each 20 ms the mouse is held down and get 10 100% after 2 seconds
public partial class Form1 : Form
{
private Timer pbTimer;
private int pbProgress = 0;
public Form1()
{
InitializeComponent();
pbTimer = new Timer();
pbTimer.Tick += new EventHandler(ProgressUpdate);
pbTimer.Interval = 20;
this.MouseDown += Form1_MouseDown;
this.MouseUp += Form1_MouseUp;
}
private void ProgressUpdate(object sender, EventArgs e)
{
if (pbProgress < 100)
{
progressBar1.Value = ++pbProgress;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
pbTimer.Start();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
pbTimer.Stop();
progressBar1.Value = 0;
pbProgress = 0;
}
Related
I have problem, which consists in aesthetic sense, correctly - There is textBox to which i apply true condition of UseSystemPasswordChar.. It's work! But i get bold points. Try to change font size - decreases textbox's field. Below is the code (although why is it here?). Can anyone help, thank you in advance)
public partial class frmRegistr : Form
{
public frmRegistr()
{
InitializeComponent();
}
int counter = 0;
int a = 0;
string b;
private void frmRegistr_Load(object sender, EventArgs e)
{
b = label1.Text;
a = b.Length;
label1.Text = "";
timer1.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (counter < a)
{
counter++;
label1.Text = b.Substring(0, counter);
}
else
{
timer1.Stop();
}
}
private void label4_Click(object sender, EventArgs e)
{
timer3.Start();
}
private void label4_MouseHover(object sender, EventArgs e)
{
//if this.MouseLeave
label4.BackColor = Color.FromArgb(((int)(((byte)(154)))), ((int)(((byte)(181)))), ((int)(((byte)(101)))));
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Opacity == 1)
{
timer2.Stop();
}
Opacity += .2;
}
private void timer3_Tick(object sender, EventArgs e)
{
if (Opacity <= 0)
{
this.Close();
}
Opacity -= .2;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = true;
}
}
}
If you want to define your own password character, use property TextBox.PasswordChar. If you want this in a certain font, use Control.Font
As you only have to do this once, do this in the constructor:
public MyForm : Form
{
InitializeComponents(),
this.textBox1.PasswordChar = '_';
this.textBox11.Font = new Font(...)
};
You can also decide to do this using the visual studio designer.
You can setup this in VisualStudio designer, but this is code:
textBox1.PasswordChar = '*';
//* = password character
I tried to implement progressbar in windows form but progress bar is showing result after completion of whole execution.
public Status()
{
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (ForReporting.FileProcessed <= ForReporting.TotalNumberFiles)
{
Thread.Sleep(100);
int temp=ForReporting.FileProcessed*100;
temp = temp / ForReporting.TotalNumberFiles;
backgroundWorker1.ReportProgress(temp);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void Status_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
I am new to C#.
Just doing my first project.
This is my code.
I just want to fill the textbox once the progress bar finish loading.
But its not working now.
Can anyone tell me what I am doing worong.
My code is as below.
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
// int n = timer1.Interval;
int m = progressBar1.Value;
if(m==0)
{
textBox2.Text = "test";
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
}
}
Try This:
int progressVal=0;
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
// int n = timer1.Interval;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
progressVal= progressBar1.Value;
if(progressVal==progressBar1.Maximum)
{
timer1.Stop();
textBox2.Text = "Loading done!";
}
}
I greatly appreciate any help. I have 20+ buttons, each with a word, or a space or period. Each time I click on a button, the pre-existing word is wiped out and replaced with the new word. I need each word and/or space to remain in place until I click the "Clear" button.
Maybe this has been previously asked/answered under different search terms? I tend to believe I need to identify a string variable, but have no idea how to begin.
==============
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 WindowsFormsApplication1
{
public partial class frmSentenceBuilder : Form
{
public frmSentenceBuilder()
{
InitializeComponent();
}
private void frmSentenceBuilder_Load(object sender, EventArgs e)
{
}
private void btnA_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnA.Text;
}
private void btn_a_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_a.Text;
}
private void btnAn_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnAn.Text;
}
private void btn_an_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_an.Text;
}
private void btnThe_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnThe.Text;
}
private void btn_the_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_the.Text;
}
private void btnman_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnman.Text;
}
private void btnwoman_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnwoman.Text;
}
private void btndog_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btndog.Text;
}
private void btncat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btncat.Text;
}
private void btncar_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btncar.Text;
}
private void btnbicycle_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbicycle.Text;
}
private void btnbeautiful_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbeautiful.Text;
}
private void btnbig_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbig.Text;
}
private void btnsmall_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnsmall.Text;
}
private void btnstrange_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnstrange.Text;
}
private void btnlookedat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnlookedat.Text;
}
private void btnrode_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnrode.Text;
}
private void btnspoketo_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnspoketo.Text;
}
private void btnlaughedat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnlaughedat.Text;
}
private void btndrove_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btndrove.Text;
}
private void btnSpace_Click(object sender, EventArgs e)
{
lblSentenceText.Text = " ";
}
private void btnperiod_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnperiod.Text;
}
private void btnexclam_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnexclam.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
lblSentenceText.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Use lb1SentenceText.Text+=whatever.Text;.
+= is equivalent to lbSentence.Text = lblSentenceText.Text + whatever.Text.
Basically, it appends or concatenates the right hand side string to the string on the left hand side. Hope it makes sense?
So:
string rhs="Hello " ;
string lhs = "World";
string rhs = rhs + lhs;//Hello World
Please be inspired. You have a lot of redundant code.
btnA.Click += AppendButtonText;
btn_a.Click += AppendButtonText;
...
private void AppendButtonText(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null)
{
lblSentenceText.Text += button.Text;
}
}
On the button click events, change it to += instead of = (except in the clear button). This is equivalent to writing something = something + newValue;.
Try:
lb1SentenceText.Text = lb1SentenceText.Text + *something*.text
The += operator means add to, and the variable modified is equal to the added (Int, String) appended to the original value (x = 1; x += 3; x is now 4)
Try this:
button.Click += new System.EventHandler(ButtonClick);
button1.Click += new System.EventHandler(ButtonClick);
// And for each button, one of those.
private void ButtonClick(object sender, System.EventArgs e)
{
// Do whatever you want to do here, you can place the TEXT to be appended on the button, if so:
lb1SentenceText.Text += sender.Text;
}
//Simple.Create a global variable and within each button click event do this;
string yourStrVar = ""; //Must be global
yourStrVar+= ((Button)sender).Text
I have a simple bitmap editor for small monochrome bitmaps. And I have a menus which allows to choose thickness of the pen and robber. The problem is that I don't like the following code. Is there any way to make it more compact?
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 3;
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 2;
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 1;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 4;
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 5;
}
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 6;
}
private void toolStripMenuItem8_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 7;
}
private void toolStripMenuItem9_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 8;
}
private void toolStripButton3_Click_1(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.Clear;
}
private void toolStripDropDownButton2_Click(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.FreeHand;
}
private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.Clear;
}
private void toolStripMenuItem10_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 1;
}
private void toolStripMenuItem11_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 2;
}
private void toolStripMenuItem12_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 3;
}
private void toolStripMenuItem13_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 4;
}
private void toolStripMenuItem14_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 5;
}
private void toolStripMenuItem15_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 6;
}
private void toolStripMenuItem16_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 7;
}
private void toolStripMenuItem17_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 8;
}
private void toolStripMenuItem18_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 16;
}
private void toolStripMenuItem19_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 32;
}
private void toolStripMenuItem21_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 32;
}
private void toolStripMenuItem20_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 16;
}
private void toolStripMenuItemCommon_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem) sender;
int thickness = (int)item.Tag;
CurrentPanel.PenThickness = thickness ;
}
Of course you need to initialize Tag of each ToolStripMenuItem and set toolStripMenuItemCommon_Click as a handler for Click event. You can do it in a for cycle for example:
for(int i = 1; i < 8; i++)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = "Set thickness: " + i;
item.Click += toolStripMenuItemCommon_Click;
item.Tag = i;
// add item to strip container
}
Yes, there is:
private void SetPenThickness(int thickness)
{
CurrentPanel.PenThickness = thickness;
}
toolStripMenuItem1.Click += (s,e) => SetPenThickness(1);
toolStripMenuItem2.Click += (s,e) => SetPenThickness(2);
toolStripMenuItem3.Click += (s,e) => SetPenThickness(3);
toolStripMenuItem4.Click += (s,e) => SetPenThickness(4);
// ...
Try this:
private void toolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem) sender;
switch (toolStripMenuItem.ID)
{
case "toolStripMenuItem4":
{
CurrentPanel.PenThickness = 3;
break;
}
..........
..........
}
}