I have Label in Panel. In timer.Tick new value write in Label.Text, but in form not change.
Program.cs:
static void Main(string[] args)
{
MainForm mainForm = new MainForm();
Engine engine = new Engine(mainForm);
Application.Run(mainForm);
}
MainForm.cs
public partial class MainForm : Form
{
public MatchesPanel panel;
public MainForm()
{
InitializeComponent();
this.Load += MainForm_Load;
this.panel = new MatchesPanel("panel", new System.Drawing.Point(0, 52));
this.panel.Name = "panel";
this.panel.TabIndex = 8;
this.Controls.Add(this.panel);
}
}
MatchesPanel.cs
public class MatchesPanel : Panel
{
public string Name;
int VWheelSize = 0;
Dictionary<string, Label> Items = new Dictionary<string, Label>();
Label InFocus = null;
public MatchesPanel(string name, Point location)
{
Name = name;
this.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Location = location;
this.Margin = new System.Windows.Forms.Padding(0);
this.Size = new System.Drawing.Size(400, 323);
this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.MouseWheel += This_MouseWheel;
this.Click += This_Click;
this.Invalidated += MatchesPanel_Invalidated;
}
void Label_Click(object s, EventArgs e)
{
Label l = (Label)s;
l.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
l.BackColor = System.Drawing.Color.MediumTurquoise;
if (InFocus != null && InFocus != l)
{
InFocus.BorderStyle = System.Windows.Forms.BorderStyle.None;
InFocus.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
}
InFocus = l;
l.Focus();
}
public void SetText(string key, string text)
{
Label l = Items[key];
l.Text = text;
}
public void Add(string key, string text)
{
Label label = new Label();
label.AllowDrop = true;
label.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
label.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
label.Location = new Point(0, Items.Count * 37 + VWheelSize);
label.Margin = new System.Windows.Forms.Padding(0);
label.Padding = new System.Windows.Forms.Padding(3);
label.Size = new System.Drawing.Size(400, 37);
label.Text = text;
label.Click += Label_Click;
label.LostFocus += Label_LostFocus;
label.MouseLeave += Label_MouseLeave;
label.MouseMove += Label_MouseMove;
this.Controls.Add(label);
Items.Add(key, label);
}
public void UpdateMatches(Dictionary<string, Match> Matches)
{
string newStr;
// calculate newStr...
// in debugger i check newStr value and this right
// but text not updating
this.SetText(key, newStr);
}
}
Engine.cs
public class Engine
{
System.Windows.Forms.Timer timer = null;
public Dictionary<string, Match> Matches;
MainForm MainForm = null;
public Engine(MainForm mainForm)
{
MainForm = mainForm;
Matches = new Dictionary<string, Match>();
timer = new System.Windows.Forms.Timer();
timer.Tick += timer_Tick;
timer.Interval = 1000;
timer.Start();
}
public void timer_Tick(object s, EventArgs e)
{
System.Windows.Forms.Timer timer = (System.Windows.Forms.Timer)s;
timer.Stop();
Proc();
timer.Start();
}
public async void Proc()
{
// do something...
MainForm.Invoke((System.Windows.Forms.MethodInvoker)delegate()
{
MainForm.panel.UpdateMatches(Matches);
});
}
}
I try use:
Label.Update();
Label.Refresh();
Label.Invalidate();
but it not work.
If I click in label, when it not in focus (InFocus != sourceLabel in clickHandler), text value updating in sourceLabel one time.
Help me pls. I read another topics and not find solve.
If need more code, tell me.
Thx.
[EDIT]
I simplified my code.
Program.cs:
static void Main(string[] args)
{
MainForm mainForm = new MainForm();
Engine engine = new Engine(mainForm);
Application.Run(mainForm);
}
MatchesPanel.cs
public class MatchesPanel : Panel
{
Dictionary<string, Label> Items = new Dictionary<string, Label>();
public MatchesPanel(Point location)
{
this.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Location = location;
this.Margin = new System.Windows.Forms.Padding(0);
this.Size = new System.Drawing.Size(400, 323);
this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
}
public void SetText(string key)
{
Label l = Items[key];
l.Text = DateTime.Now.ToString();
}
public void Add(string key)
{
Label label = new Label();
label.Name = key;
label.AllowDrop = true;
label.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
label.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
label.Location = new Point(0, Items.Count * 37 + VWheelSize);
label.Margin = new System.Windows.Forms.Padding(0);
label.Padding = new System.Windows.Forms.Padding(3);
label.Size = new System.Drawing.Size(400, 37);
label.Text = DateTime.Now.ToString();
this.Controls.Add(label);
Items.Add(key, label);
}
}
Engine.cs
public class Engine
{
System.Windows.Forms.Timer timer = null;
public MatchesPanel panel = null;
MainForm MainForm = null;
public Engine(MainForm mainForm)
{
MainForm = mainForm;
timer = new System.Windows.Forms.Timer();
timer.Tick += timer_Tick;
timer.Interval = 1000;
timer.Start();
}
public void timer_Tick(object s, EventArgs e)
{
System.Windows.Forms.Timer timer = (System.Windows.Forms.Timer)s;
timer.Stop();
MainForm.Invoke((System.Windows.Forms.MethodInvoker)delegate()
{
if (panel == null)
{
panel = new MatchesPanel(new System.Drawing.Point(0, 52));
panel.Name = "key1";
// add label in main form
panel.Add("key1");
MainForm.Controls.Add(panel);
}
panel.SetText("key1");
});
timer.Start();
}
}
Problem:
Label.Text not updating.
Solve:
public class UpdatableLabel : Button
{
public UpdatableLabel() : base()
{
FlatAppearance.BorderSize = 0;
FlatStyle = System.Windows.Forms.FlatStyle.Flat;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen pen = new Pen(FlatAppearance.BorderColor, 1);
Rectangle rectangle = new Rectangle(0, 0, Size.Width - 1, Size.Height - 1);
e.Graphics.DrawRectangle(pen, rectangle);
}
}
Now my "label" is updated.
Related
Hey I have two classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BMIcalculator
{
class BmrCalculator
{
BMIcalculator BMICalc = new BMIcalculator();
private int age;
private double BMR;
private Gender Gender;
private double weight;
private double height;
public void setWeight(double weight)
{
this.weight = weight;
}
public void setHeight(double height)
{
this.height = height;
}
public double GetWeight()
{
return weight;
}
public double GetHeight()
{
return height;
}
public void SetGender(Gender Gender)
{
this.Gender = Gender;
}
public Gender GetGender()
{
return this.Gender;
}
public double CalculateBMR()
{
if (Gender == Gender.Female)
{
BMR = (10 * weight) + (6.25 * height) - (5 * age) - 161;
}
else
{
BMR = (10 * BMICalc.getWeight()) + (6.25 * BMICalc.getHeight()) - (5 * age) + 5;
}
return BMR;
}
public void SetAge(int age)
{
this.age = age;
}
}
}
2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BMIcalculator
{
class BMIcalculator
{
private double height;
private double weight;
private UnitTypes unit;
public BMIcalculator()
{
unit = UnitTypes.Metric;
}
#region Setters and getters
public double getHeight()
{
return height;
}
public void setHeight(double height)
{
if(height >= 0.0)
this.height = height;
}
public double getWeight()
{
return weight;
}
public void setWeight(double weight)
{
if (weight >= 0.0)
this.weight = weight;
}
public UnitTypes getUnit()
{
return unit;
}
public void setUnit(UnitTypes unit)
{
this.unit = unit;
}
#endregion
}
}
Main form:
namespace BMIcalculator
{
public partial class MainForm : Form
{
private string name = string.Empty; // instance variable
BMIcalculator bmiCalc = new BMIcalculator();
BmrCalculator bmrCalc = new BmrCalculator();
public MainForm()
{
InitializeComponent();
InitializeGUI();
}
private void InitializeGUI()
{
rbtnMetric.Checked = true;
lblBmr.Text = String.Empty;
}
private void MainForm_Load(object sender, EventArgs e)
{
CenterToScreen();
}
#region BMICalculator
private void UpdateHeightText()
{
if(rbtnMetric.Checked == true)
{
lblHeight.Text = "Height (Cm)";
lblWeight.Text = "Weight (Kg)";
}
else
{
lblHeight.Text = "Height (Ft)";
lblWeight.Text = "Weight (lbs)";
}
}
private void rbtnMetric_CheckedChanged(object sender, EventArgs e)
{
UpdateHeightText();
}
private void rbtnImperial_CheckedChanged(object sender, EventArgs e)
{
UpdateHeightText();
}
private bool ReadInputBMI()
{
ReadUnit();
bool weightOK = ReadWeight();
bool heightOK = ReadHeight();
return weightOK && heightOK;
}
private void ReadUnit() // this void looks which one is checked
{
if(rbtnMetric.Checked)
{
bmiCalc.setUnit(UnitTypes.Metric);
}
else
{
bmiCalc.setUnit(UnitTypes.Imperial);
}
}
private bool ReadWeight()
{
double weight = 0.0;
bool ok = double.TryParse(txtWeight.Text, out weight);
if (!ok)
{
MessageBox.Show("Invalid weight value , error");
}
bmiCalc.setWeight(weight);
return ok;
}
private bool ReadHeight()
{
double height = 0.0;
bool ok = double.TryParse(txtCmFt.Text, out height);
if (!ok)
{
MessageBox.Show("Invalid height value , error");
}
double inch = 0.0;
// cm -> m ft -> inches
if(bmiCalc.getUnit() == UnitTypes.Metric)
{
height = height / 100; // cm -> m
}
else
{
height = height * 12.0 + inch; // ft -> inch
}
bmiCalc.setHeight(height);
return ok;
}
#endregion
#region BMRCalculator
private void UpdateGenderStatus()
{
if(rbtnFemale.Checked)
{
bmrCalc.SetGender(Gender.Female);
}
else
{
bmrCalc.SetGender(Gender.Male);
}
}
private void rbtnFemale_CheckedChanged(object sender, EventArgs e)
{
UpdateGenderStatus();
}
private void rbtnMale_CheckedChanged(object sender, EventArgs e)
{
UpdateGenderStatus();
}
private void ReadAge()
{
int age;
bool ok = int.TryParse(txtAge.Text , out age);
if(!ok)
{
MessageBox.Show("Error Wrong Age");
}
else
{
bmrCalc.SetAge(age);
}
}
#endregion
private void btnCalculateBMR_Click(object sender, EventArgs e)
{
double weight = double.Parse(txtWeight.Text);
double height = double.Parse(txtCmFt.Text);
bmrCalc.setWeight(weight);
bmrCalc.setHeight(height);
ReadAge();
//bmrCalc.CalculateBMR();
lblBmr.Text = bmrCalc.CalculateBMR().ToString();
}
}
}
designer:
namespace BMIcalculator
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblHeight = new System.Windows.Forms.Label();
this.txtCmFt = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.rbtnMetric = new System.Windows.Forms.RadioButton();
this.lblWeight = new System.Windows.Forms.Label();
this.txtWeight = new System.Windows.Forms.TextBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.btnCalculateBMR = new System.Windows.Forms.Button();
this.groupBox6 = new System.Windows.Forms.GroupBox();
this.lblBmr = new System.Windows.Forms.Label();
this.txtAge = new System.Windows.Forms.TextBox();
this.lblAge = new System.Windows.Forms.Label();
this.groupBox5 = new System.Windows.Forms.GroupBox();
this.rbtnMale = new System.Windows.Forms.RadioButton();
this.rbtnFemale = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout();
this.groupBox4.SuspendLayout();
this.groupBox6.SuspendLayout();
this.groupBox5.SuspendLayout();
this.SuspendLayout();
//
// lblHeight
//
this.lblHeight.AutoSize = true;
this.lblHeight.Location = new System.Drawing.Point(27, 39);
this.lblHeight.Name = "lblHeight";
this.lblHeight.Size = new System.Drawing.Size(54, 20);
this.lblHeight.TabIndex = 1;
this.lblHeight.Text = "Height";
//
// txtCmFt
//
this.txtCmFt.Location = new System.Drawing.Point(165, 32);
this.txtCmFt.Name = "txtCmFt";
this.txtCmFt.Size = new System.Drawing.Size(51, 27);
this.txtCmFt.TabIndex = 3;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.rbtnMetric);
this.groupBox1.Location = new System.Drawing.Point(319, 23);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(163, 88);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Unit";
//
// rbtnMetric
//
this.rbtnMetric.AutoSize = true;
this.rbtnMetric.Location = new System.Drawing.Point(26, 35);
this.rbtnMetric.Name = "rbtnMetric";
this.rbtnMetric.Size = new System.Drawing.Size(121, 24);
this.rbtnMetric.TabIndex = 0;
this.rbtnMetric.TabStop = true;
this.rbtnMetric.Text = "Metric(kg,cm)";
this.rbtnMetric.UseVisualStyleBackColor = true;
this.rbtnMetric.CheckedChanged += new System.EventHandler(this.rbtnMetric_CheckedChanged);
//
// lblWeight
//
this.lblWeight.AutoSize = true;
this.lblWeight.Location = new System.Drawing.Point(27, 77);
this.lblWeight.Name = "lblWeight";
this.lblWeight.Size = new System.Drawing.Size(56, 20);
this.lblWeight.TabIndex = 9;
this.lblWeight.Text = "Weight";
//
// txtWeight
//
this.txtWeight.Location = new System.Drawing.Point(165, 70);
this.txtWeight.Name = "txtWeight";
this.txtWeight.Size = new System.Drawing.Size(125, 27);
this.txtWeight.TabIndex = 10;
//
// groupBox4
//
this.groupBox4.Controls.Add(this.btnCalculateBMR);
this.groupBox4.Controls.Add(this.groupBox6);
this.groupBox4.Controls.Add(this.txtAge);
this.groupBox4.Controls.Add(this.lblAge);
this.groupBox4.Controls.Add(this.groupBox5);
this.groupBox4.Location = new System.Drawing.Point(27, 130);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(392, 314);
this.groupBox4.TabIndex = 15;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "BMR calculator";
//
// btnCalculateBMR
//
this.btnCalculateBMR.Location = new System.Drawing.Point(68, 274);
this.btnCalculateBMR.Name = "btnCalculateBMR";
this.btnCalculateBMR.Size = new System.Drawing.Size(212, 29);
this.btnCalculateBMR.TabIndex = 4;
this.btnCalculateBMR.Text = "Calculate BMR";
this.btnCalculateBMR.UseVisualStyleBackColor = true;
this.btnCalculateBMR.Click += new System.EventHandler(this.btnCalculateBMR_Click);
//
// groupBox6
//
this.groupBox6.Controls.Add(this.lblBmr);
this.groupBox6.Location = new System.Drawing.Point(177, 55);
this.groupBox6.Name = "groupBox6";
this.groupBox6.Size = new System.Drawing.Size(185, 213);
this.groupBox6.TabIndex = 3;
this.groupBox6.TabStop = false;
this.groupBox6.Text = "how many calories you need";
//
// lblBmr
//
this.lblBmr.AutoSize = true;
this.lblBmr.Location = new System.Drawing.Point(45, 67);
this.lblBmr.Name = "lblBmr";
this.lblBmr.Size = new System.Drawing.Size(58, 20);
this.lblBmr.TabIndex = 5;
this.lblBmr.Text = "label10";
//
// txtAge
//
this.txtAge.Location = new System.Drawing.Point(98, 182);
this.txtAge.Name = "txtAge";
this.txtAge.Size = new System.Drawing.Size(50, 27);
this.txtAge.TabIndex = 2;
//
// lblAge
//
this.lblAge.AutoSize = true;
this.lblAge.Location = new System.Drawing.Point(31, 185);
this.lblAge.Name = "lblAge";
this.lblAge.Size = new System.Drawing.Size(36, 20);
this.lblAge.TabIndex = 1;
this.lblAge.Text = "Age";
//
// groupBox5
//
this.groupBox5.Controls.Add(this.rbtnMale);
this.groupBox5.Controls.Add(this.rbtnFemale);
this.groupBox5.Location = new System.Drawing.Point(17, 42);
this.groupBox5.Name = "groupBox5";
this.groupBox5.Size = new System.Drawing.Size(131, 125);
this.groupBox5.TabIndex = 0;
this.groupBox5.TabStop = false;
this.groupBox5.Text = "Gender";
//
// rbtnMale
//
this.rbtnMale.AutoSize = true;
this.rbtnMale.Location = new System.Drawing.Point(14, 63);
this.rbtnMale.Name = "rbtnMale";
this.rbtnMale.Size = new System.Drawing.Size(63, 24);
this.rbtnMale.TabIndex = 1;
this.rbtnMale.TabStop = true;
this.rbtnMale.Text = "Male";
this.rbtnMale.UseVisualStyleBackColor = true;
this.rbtnMale.CheckedChanged += new System.EventHandler(this.rbtnMale_CheckedChanged);
//
// rbtnFemale
//
this.rbtnFemale.AutoSize = true;
this.rbtnFemale.Location = new System.Drawing.Point(14, 33);
this.rbtnFemale.Name = "rbtnFemale";
this.rbtnFemale.Size = new System.Drawing.Size(78, 24);
this.rbtnFemale.TabIndex = 0;
this.rbtnFemale.TabStop = true;
this.rbtnFemale.Text = "Female";
this.rbtnFemale.UseVisualStyleBackColor = true;
this.rbtnFemale.CheckedChanged += new System.EventHandler(this.rbtnFemale_CheckedChanged);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(512, 484);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.txtWeight);
this.Controls.Add(this.lblWeight);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.txtCmFt);
this.Controls.Add(this.lblHeight);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.Load += new System.EventHandler(this.MainForm_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox4.ResumeLayout(false);
this.groupBox4.PerformLayout();
this.groupBox6.ResumeLayout(false);
this.groupBox6.PerformLayout();
this.groupBox5.ResumeLayout(false);
this.groupBox5.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private Label lblHeight;
private TextBox txtCmFt;
private GroupBox groupBox1;
private RadioButton rbtnMetric;
private Label lblWeight;
private TextBox txtWeight;
private GroupBox groupBox4;
private Button btnCalculateBMR;
private GroupBox groupBox6;
private TextBox txtAge;
private Label lblAge;
private GroupBox groupBox5;
private RadioButton rbtnMale;
private RadioButton rbtnFemale;
private Label lblBmr;
}
}
The problem is i cant acess another class by creating new BMIcalculator(); because then its empty or 0.
So my question is how can i access the getter from the BMIcalculator without creating new object. I want to get access to instance variables height and weigh and then use it in Bmrcalculator.
I have very similar problem to this one: Call Methods from another class c#
but i still cant solve it. I began programming 2 weeks ago, sorry if its an obvious question.
I created a minimal WinForm Application in C#.
Everything Works Fine but for some reason a "Green border" Surrounds My Application.
I Pasted the code down below.
[program.cs]
namespace WinFormsApp1
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}
[Form1.cs]
namespace WinFormsApp1
{
public partial class Form1 : Form
{
int counter = 0;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.textanimationWorker = new System.ComponentModel.BackgroundWorker();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Font = new System.Drawing.Font("Segoe UI", 40F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
this.label1.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.label1.Location = new System.Drawing.Point(543, 209);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(466, 72);
this.label1.TabIndex = 0;
this.label1.Text = "Uploading RootKit.";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// button1
//
this.button1.Font = new System.Drawing.Font("Segoe UI", 25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
this.button1.Location = new System.Drawing.Point(43, 508);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(222, 70);
this.button1.TabIndex = 1;
this.button1.Text = "Upload RootKit";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(43, 284);
this.progressBar1.MarqueeAnimationSpeed = 1;
this.progressBar1.Maximum = 10;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(1037, 23);
this.progressBar1.Step = 1;
this.progressBar1.TabIndex = 2;
this.progressBar1.Click += new System.EventHandler(this.progressBar1_Click);
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
//
// textanimationWorker
//
this.textanimationWorker.WorkerReportsProgress = true;
this.textanimationWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.textanimationWorker_DoWork);
this.textanimationWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.textanimationWorker_ProgressChanged);
this.textanimationWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.textanimationWorker_RunWorkerCompleted);
//
// label2
//
this.label2.AutoSize = true;
this.label2.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.label2.Location = new System.Drawing.Point(1036, 594);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(86, 15);
this.label2.TabIndex = 3;
this.label2.Text = "DECRAT V1.0.0";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// Form1
//
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.ClientSize = new System.Drawing.Size(1134, 618);
this.Controls.Add(this.label2);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Loading Form1.. Please wait..");
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
progressBar1.Maximum = 10;
backgroundWorker1.RunWorkerAsync();
textanimationWorker.RunWorkerAsync();
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
for (int i = 0; i < 10; i++) {
backgroundWorker1.ReportProgress(0);
Thread.Sleep(1000);
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
counter = 50;
MessageBox.Show("Uploaded RootKit Successfully!");
counter = 0;
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value += 1;
}
private void textanimationWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
while (counter <= 10)
{
if (counter <= 3)
{
counter++;
textanimationWorker.ReportProgress(0);
Thread.Sleep(500);
}
else if(counter == 50){
break;
}
else {
counter = 0;
textanimationWorker.ReportProgress(0);
}
}
}
private void textanimationWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
if (counter == 1)
{
label1.Text = "Uploading RootKit";
}
else {
label1.Text += ".";
}
}
private void textanimationWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}
FYI: I also published it to an exe file but still the green border does not disappear!
I Thought the green border appears just when I code so that I know where the border is but the issue seems with C# ? or probably with something else ?
EDIT [Adding More information]: I want the green border to disappear.
Here is a photo of the Application..
Green box Indicates The Executable or the program is sandboxed.
I am using COMODO Antivirus and it sandboxed it.
By Turning the antivirus off for a min and running the program the green box disappeared indicating that the executable is not sandboxed.
Background: I want to show a custom message box to the user with Yes/No buttons and if the user clicks each of these buttons I will return the result to the caller. Also, if the user doesn't show any reaction again I want to return a third result (using a Timer event). In a word, either after some time elapsed or after a button click the method (Display in my code) should return a value; and I want to wait for either of them to happen.
Problem: The UI looks frozen and only the Timer event triggers.
Code which will be used in the real project (with descent names!):
namespace MessageBox
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyForm.Display();
}
}
public class MyForm : Form
{
public MyForm()
{
List<Button> _buttonCollection = new List<Button>();
FlowLayoutPanel _flpButtons = new FlowLayoutPanel();
Panel _plFooter = new Panel();
_plFooter.Dock = DockStyle.Bottom;
_plFooter.Padding = new Padding(5);
_plFooter.BackColor = Color.FromArgb(41, 47, 139);
_plFooter.Height = 75;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.FromArgb(20, 37, 105);
this.StartPosition = FormStartPosition.CenterScreen;
this.Padding = new Padding(3);
this.Width = 400;
_flpButtons.FlowDirection = FlowDirection.RightToLeft;
_flpButtons.Dock = DockStyle.Fill;
_plFooter.Controls.Add(_flpButtons);
this.Controls.Add(_plFooter);
Button btnYes = new Button();
btnYes.Click += ButtonClick;
btnYes.Text = "Yes";
Button btnNo = new Button();
btnNo.Click += ButtonClick;
btnNo.Text = "No";
_buttonCollection.Add(btnYes);
_buttonCollection.Add(btnNo);
foreach (Button btn in _buttonCollection)
{
btn.ForeColor = Color.FromArgb(170, 170, 170);
btn.Font = new System.Drawing.Font("Eurostile", 12, FontStyle.Bold);
btn.Padding = new Padding(3);
btn.FlatStyle = FlatStyle.Flat;
btn.Height = 60;
btn.Width = 150;
btn.FlatAppearance.BorderColor = Color.FromArgb(99, 99, 98);
_flpButtons.Controls.Add(btn);
}
}
static Task taskA;
private void ButtonClick(object sender, EventArgs e)
{
_event.Set();
Button btn = (Button)sender;
if (btn.Text == "Yes")
Result = 1;
else
{
Result = 2;
}
this.Close();
this.Dispose();
}
static AutoResetEvent _event = new AutoResetEvent(false);
private static MyForm form;
public static int Display()
{
form = new MyForm();
StartTimer();
form.Show();
_event.WaitOne();
form.Close();
form.Dispose();
return Result;
}
private static void StartTimer()
{
_timer = new System.Timers.Timer(10000);
_timer.Elapsed += SetEvent;
_timer.AutoReset = false;
_timer.Enabled = true;
}
private static System.Timers.Timer _timer;
public static int Result { get; set; }
private static void SetEvent(Object source, System.Timers.ElapsedEventArgs e)
{
_timer.Start();
Result = -1;
var timer = (System.Timers.Timer) source;
timer.Elapsed -= SetEvent;
_event.Set();
}
}
}
Note: I know the problem is that the UI control is created in a thread which is frozen but what's the solution?
Create a Dialog for MessageBox at the end of your process and show the alert you want to show the user.
You can look at the sources like and issue with Thread
I want to move a rectangle in WPF application using the following code. However, I am getting the following error:
System.InvalidOperationException: Cannot use a DependencyObject that belongs to a different thread
I looked at other problems in stackoverflow but nothing worked.
public partial class MainWindow : Window
{
private Rectangle rect;
int count = 1;
Timer timer;
public MainWindow()
{
InitializeComponent();
Rectangle movedRectangle = new Rectangle();
movedRectangle.Width = 200;
movedRectangle.Height = 50;
movedRectangle.Fill = Brushes.Blue;
movedRectangle.Opacity = 0.5;
TranslateTransform translateTransform1 = new TranslateTransform(50, 20);
movedRectangle.RenderTransform = translateTransform1;
this.can.Children.Add(movedRectangle);
this.rect = movedRectangle;
timer = new Timer(500);
timer.Elapsed += OnTimedEvent;
timer.Enabled = true;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
count++;
TranslateTransform translateTransform1 = new TranslateTransform(50 + count * 2, 20);
this.rect.Dispatcher.Invoke(new Action(()=>
rect.RenderTransform = translateTransform1));
//this.can.UpdateLayout();
this.can.Dispatcher.Invoke(new Action(()=>
this.can.UpdateLayout()
));
}
I would suggest you to use DispatcherTimer than a normal timer.
Please see the below solution. enjoy.
Note: for DispatcherTimer you will need to add the assembly reference for System.Windows.Threading
public partial class MainWindow : Window
{
private Rectangle rect;
int count = 1;
private DispatcherTimer timer = null;
public MainWindow()
{
InitializeComponent();
Rectangle movedRectangle = new Rectangle();
movedRectangle.Width = 200;
movedRectangle.Height = 50;
movedRectangle.Fill = Brushes.Blue;
movedRectangle.Opacity = 0.5;
TranslateTransform translateTransform1 = new TranslateTransform(50, 20);
movedRectangle.RenderTransform = translateTransform1;
this.can.Children.Add(movedRectangle);
this.rect = movedRectangle;
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
timer.Tick += timer_Tick;
timer.Start();
timer.IsEnabled = true;
}
void timer_Tick(object sender, EventArgs e)
{
count++;
TranslateTransform translateTransform1 = new TranslateTransform(50 + count * 2, 20);
Dispatcher.BeginInvoke(new Action<TranslateTransform>(delegate(TranslateTransform t1)
{
rect.RenderTransform = t1;
this.can.UpdateLayout();
}), System.Windows.Threading.DispatcherPriority.Render, translateTransform1);
}
}
You are constructing a TranslateTransform (which is a DependencyObject) outside the UI thread. Easy fix:
this.rect.Dispatcher.Invoke(new Action(
() =>
{
TranslateTransform translateTransform1 = new TranslateTransform(50 + count * 2, 20);
rect.RenderTransform = translateTransform1;
}));
Arguably a better fix: use a DispatcherTimer instead and get rid of all your Dispatcher.Invoke calls.
I'd like to run a piece of code which keeps changing the picture of a picturebox (like a rotating propeller) until the form is closed.
I managed to change the picture of the PictureBox with an EventHandler, but I don't know how to go along.
public Form1()
{
InitializeComponent();
this.Controls.Add(pb);
}
PictureBox pb = new PictureBox
{
Location = new Point(0, 0),
SizeMode = PictureBoxSizeMode.Zoom,
Size = new Size(300,300),
ImageLocation = #"E:\folder\gas_jo.png"
};
private void Form1_Click(object sender, EventArgs e)
{
if (pb.ImageLocation == #"E:\folder\gas_jo.png")
{
pb.ImageLocation =#"E:\folder\gas_jo_1.png";
}
else if (pb.ImageLocation == #"E:\folder\gas_jo_1.png")
{
pb.ImageLocation = #"E:\folder\gas_jo.png";
}
}
System.Windows.Forms.Timer timer;
public Form1()
{
InitializeComponent();
this.Controls.Add(pb);
timer = new System.Windows.Forms.Timer();
timer.Interval = 1000;
timer.Tick += (sender, args) => {
if (pb.ImageLocation == #"E:\folder\gas_jo.png")
{
pb.ImageLocation =#"E:\folder\gas_jo_1.png";
}
else if (pb.ImageLocation == #"E:\Real-time_Imi\gas_jo_1.png")
{
pb.ImageLocation = #"E:\Real-time_Imi\gas_jo.png";
}
};
timer.Start();
}
PictureBox pb = new PictureBox
{
Location = new Point(0, 0),
SizeMode = PictureBoxSizeMode.Zoom,
Size = new Size(300,300),
ImageLocation = #"E:\folder\gas_jo.png"
};
string[] pictureBoxArray = new string[] { #"E:\folder\gas_jo.png", #"E:\folder\gas_jo_1.png", #"E:\folder\gas_jo_2.png" };
int pctIndex = 0;
private void timer1_Tick(object sender, EventArgs e)
{
pb.ImageLocation = pictureBoxArray[pctIndex];
pctIndex ++;
if(pctIndex==pictureBoxArray.Length)
pctIndex =0 ;
}