I want to show and hide a label continuously (using sleep in a for loop). Here is what I am doing:
for (i = 0; i < 25; i++)
{
label1.Visible = true;
Thread.Sleep(1000);
label1.Visible = false;
Thread.Sleep(2000);
}
However, the above code is not working as expected. I don't see the label at all. Any idea how to achieve this
Using Thread.Sleep freezes the Windows of youre interface you must run a separated tread or use a Timer that do it for you
Example:
void blinkLabel()
{
int blink_times = 25;
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 1000;//every one second
timer1.Tick += new System.EventHandler((s, e) =>
{
if (blink_times >= 0)
{
label1.Visible = !label1.Visible;
blink_times--;
}
else
{
timer1.Stop();
}
}
);
timer1.Start();
}
Related
I've a realtime wpf application and sometimes I need to printout a document on a LAN printer. This is a part of my code:
using (PrintDocument prnDocument = new PrintDocument())
{
prnDocument.PrintPage += new PrintPageEventHandler(prnDocument_PrintPage);
if (!string.IsNullOrEmpty(printFile))
{
ImageFormat format = ImageFormat.Png;
float scale = 1; // Convert.ToSingle(_Scale.Value) / 100f;
long quality = 75; // Convert.ToInt64(_Quality.Value);
//string output = ""; // _TextBoxOutput.Text;
PaperSize ps = new PaperSize("label", panel.Width, panel.Height);
panel.BorderStyle = panelBorder ? BorderStyle.FixedSingle : BorderStyle.None;
prnDocument.DefaultPageSettings.PaperSize = ps;
prnDocument.DefaultPageSettings.Margins.Left = 0;
prnDocument.DefaultPageSettings.Margins.Top = 0;
PrintController controller = new PrintControllerFile(format, scale, quality, printFile);
prnDocument.PrintController = new PrintControllerWithStatusDialog(controller, "Exporting");
}
else
{
PaperSize ps = new PaperSize("label", panel.Width, panel.Height);
panel.BorderStyle = panelBorder ? BorderStyle.FixedSingle : BorderStyle.None;
prnDocument.DefaultPageSettings.PaperSize = ps;
prnDocument.DefaultPageSettings.Margins.Left = 0;
prnDocument.DefaultPageSettings.Margins.Top = 0;
if (!string.IsNullOrEmpty(printerName))
prnDocument.PrinterSettings.PrinterName = printerName;
prnDocument.PrintController = new StandardPrintController();
}
prnDocument.Print();
}
Basically each time the Print() is invoked, my application freeze up till 2-3 seconds, then it resume. I think that during that time I send data to the print spooler.
I've also tryied to use a thread:
System.Threading.Tasks.Task.Run(() => { prnDocument.Print(); });
But nothing seems to change. Any advice?
Need help.
so i was making a game with my friend for college using visual studio 2017 and there's something weird about the program.
we're set it so that when the life is zero, a message box would show up and chose whether to retry the game or not, but when we tried the game, the message box show's up at least at 20 - 30 second after playing the game and the timer for the game is still going even though we add "timergame.enable = false;".
where's seems to be the problem?
private void timerGame_Tick(object sender, EventArgs e)
{
for (int i = 0; i < listOfSardine.Count; i++)
{
listOfSardine[i].Top += (int)listOfSardine[i].Tag;
if (listOfSardine[i].Bounds.IntersectsWith(pictureBoxGrass.Bounds))
{
userLives--;
labelLives.Text = "Lives: " + userLives;
listOfSardine[i].Dispose();
listOfSardine.RemoveAt(i);
if (userLives == 0)
{
highScore = userScore;
timerBonusSpeed.Enabled = false;
timerGame.Enabled = false;
timerHealth.Enabled = false;
timerMatatabi.Enabled = false;
timerSardine.Enabled = false;
DialogResult dialogResultLose = MessageBox.Show
("Sorry.... you have lost, continue?", "Continue??", MessageBoxButtons.YesNo);
if (dialogResultLose == DialogResult.Yes)
{
for (int j = 0; j < listOfHealth.Count; j++)
{
listOfHealth[j].Dispose();
}
listOfHealth.Clear();
for (int q = 0; q < listOfSardine.Count; q++)
{
listOfSardine[q].Dispose();
}
listOfSardine.Clear();
for (int k = 0; k < listOfMatatabi.Count; k++)
{
listOfMatatabi[k].Dispose();
}
listOfMatatabi.Clear();
userLives = USER_LIVES;
userScore = USER_SCORE;
timerBonusSpeed.Enabled = true;
timerGame.Enabled = true;
timerHealth.Enabled = true;
timerMatatabi.Enabled = true;
timerSardine.Enabled = true;
}
else
{
this.Visible = false;
FormMainMenu formMainMenu = new FormMainMenu();
formMainMenu.Owner = this;
formMainMenu.ShowDialog();
}
}
}
else if (listOfSardine[i].Bounds.IntersectsWith(pictureBoxMainCharacter.Bounds))
{
listOfSardine[i].Dispose();
listOfSardine.RemoveAt(i);
userScore += 1;
labelScore.Text = "Score: " + userScore;
if (userScore % 100 == 0)
{
listOfSardine[i].Top += (int)listOfSardine[i].Tag * 4;
}
if(userScore == 1000)
{
timerBonusSpeed.Enabled = false;
timerGame.Enabled = false;
timerHealth.Enabled = false;
timerMatatabi.Enabled = false;
timerSardine.Enabled = false;
highScore = userScore;
}
}
else
{
listOfSardine[i].Refresh();
}
}
for (int i = 0; i < listOfHealth.Count; i++)
{
listOfHealth[i].Top += (int)listOfHealth[i].Tag;
if (listOfHealth[i].Bounds.IntersectsWith(pictureBoxGrass.Bounds))
{
listOfHealth[i].Dispose();
listOfHealth.RemoveAt(i);
}
else if (listOfHealth[i].Bounds.IntersectsWith(pictureBoxMainCharacter.Bounds))
{
listOfHealth[i].Dispose();
listOfHealth.RemoveAt(i);
userLives++;
labelLives.Text = "Lives: " + userLives;
}
else
{
listOfHealth[i].Refresh();
}
Not enough clear what you did. However, I think you have a counter for life, and which are decreasing as per your game's event. On timer event just check life counter is equal or less than Zero or not. If so, just stop timer, give user confirmation for retry. If user want to retry, then reset life counter and start timer.
Ahh already found the answer
our problem is about the ownership of the form, we just incorrectly insert the ownership for each form
thanks for the people who have answered my question earlier
I have this AS3 script (it works fine), I just want to make the loop to pause for seconds each time then it can continue looping. Like if I like it to stop for Milliseconds.Thanks
var myText:String;
var counter:int = 0;
var format : TextFormat = new TextFormat();
format.size = 16;
format.font = "Verdana";
format.bold = true;
format.color = 0x000000;
var textField : TextField = new TextField();
textField.width = 200;
textField.height = 50;
textField.selectable = false;
textField.wordWrap = true;
textField.defaultTextFormat = format;
textField.x = textField.y =0;
addChild(textField);
var textLoader:URLLoader = new URLLoader(new URLRequest("text.txt"));
textLoader.addEventListener(Event.COMPLETE, function(e:Event){initText(e.target.data);});
function initText(string:String):void{
myText = string;
addEventListener(Event.ENTER_FRAME, writeText);
}
function writeText(e:Event):void{
if (counter <= myText.length){
textField.text = myText.substr(0,counter);
counter++;
/*What I can put here to make it pause for a while*/
}
else{
removeEventListener(Event.ENTER_FRAME,writeText);
}
}
Your code is fine, you need to tweak it a little bit.
function initText(string:String):void
{
myText = string;
addEventListener(Event.ENTER_FRAME, writeText);
}
// Variable to keep the next print time in milliseconds.
var nextPrint:int;
function writeText(e:Event):void
{
// Function getTimer() returns time in milliseconds since app start.
// Skip this frame if time is not right.
if (getTimer() < nextPrint) return;
// Variable nextPrint is initially 0 so the first char will print immediately.
if (counter <= myText.length)
{
textField.text = myText.substr(0, counter);
counter++;
/*What I can put here to make it pause for a while*/
// Print next character in ~100 ms.
nextPrint = getTimer() + 100;
}
else
{
removeEventListener(Event.ENTER_FRAME, writeText);
}
}
I am using TeeChart for WinForm version: 4.1.2016.5122. But when the mouse is move to the series and click on them, the related event is not fired.
Has anyone ever met this problem?
for (int i = 0; i < 4; i++)
{
HorizBar bar = new HorizBar(m_barChart.Chart);
bar.MultiBar = MultiBars.Stacked;
bar.Cursor = Cursors.Hand;
bar.BarHeightPercent = 100;
bar.CustomBarWidth = 20;
//Event registered below will not be fired up
bar.Click += Bar_Click1;
bar.Depth = 50;
bar.DepthPercent = 50;
bar.Marks.Visible = false;
bar.Color = Color.Blue;
bar.Add(i, 0);
bar.Title = string.Format(" {0} {1}", i, i);
}
//Event registered below is not fired up
m_barChart.ClickSeries += M_barChart_ClickSeries;
Using the code below using TeeChart.Net v4.1.2016.5122, the ClickSeries fires without problems.
public Form1()
{
InitializeComponent();
InitializeChart();
}
private void InitializeChart()
{
for (int i = 0; i < 4; i++)
{
HorizBar bar = new HorizBar(tChart1.Chart);
bar.MultiBar = MultiBars.Stacked;
bar.Cursor = Cursors.Hand;
bar.BarHeightPercent = 100;
bar.CustomBarWidth = 20;
bar.Depth = 50;
bar.DepthPercent = 50;
bar.Marks.Visible = false;
bar.Color = Color.Blue;
bar.Add(i, 0);
bar.Title = string.Format(" {0} {1}", i, i);
}
tChart1.ClickSeries += TChart1_ClickSeries;
}
private void TChart1_ClickSeries(object sender, Series s, int valueIndex, MouseEventArgs e)
{
this.Text = s.Title;
}
Could you check again the code and confirm us if it works in your end?
I need a text editor control for column edit in my application.
Like notepad++ alt+mouse drag to select a text block , or pull down a long vertical cursor and press a key to insert a char to every lines over cursor.
I tried ScintillaNET but it not suppor the column-modet for insert, just can remove selected text block
I need below effect(notepad++), But ScintillaNET got:
I found a way to solve. Still using ScintillaNET.
But coding not enough beauty :)
class SelWrap
{
public int Begin { get; set; }
public int Length { get; set; }
}
//...
editor.KeyDown += (s, e) =>
{
// filter alt we will hold down alt to make vertical selection
if (e.Alt) return;
var tb = editor;
if (tb.Selections.Count < 2) return; // no in column mode
e.SuppressKeyPress = true; //block input, handle by below code
//refered from post #5825820
var input = Utility.GetCharFromKey(e.KeyCode).ToString();
if (input == "\0")
{
//SystemSounds.Beep.Play();
return;
}
var array = tb.Selections
.OrderBy(p => p.Start)
.Select(p => new SelWrap{Begin=p.Start, Length=p.End - p.Start })
.ToArray();
//do process every caret(or selection)
for (var i = 0; i < array.Length; i++)
{
var item = array[i];
if (item.Length > 0)
{
//if has selected text, just clean
tb.DeleteRange(item.Begin, item.Length);
for (var j = i + 1; j < array.Length; j++)
{
array[j].Begin -= item.Length;
}
}
if (input == "\b") //backspace
{
if (item.Length != 0) continue;
//delete a char before caret
tb.DeleteRange(item.Begin - 1, 1);
for (var j = i; j < array.Length; j++)
{
array[j].Begin--;
}
}
else //just insert that
{
tb.InsertText(item.Begin, input);
for (var j = i; j < array.Length; j++)
{
array[j].Begin++;
}
}
}
//restore caret status to keep column mode
tb.ClearSelections();
tb.SetSelection(array[0].Begin, array[0].Begin);
for (var i = 1; i < array.Length; i++)
{
var item = array[i];
tb.AddSelection(item.Begin, item.Begin);
}
};
//...
You need to turn on Scintilla rectangular selection by sending required messages to initialize rectangular selection. Example,
CallScintilla(SCI_SETSELECTIONMODE, SC_SEL_STREAM);
CallScintilla(SCI_SETMOUSESELECTIONRECTANGULARSWITCH, true);
CallScintilla(SCI_SETADDITIONALSELECTIONTYPING, true); // so you can type in the selection
CallScintilla(SCI_SETMULTIPLESELECTION, false);
// on paste, paste into rectangular selection
CallScintilla(SCI_SETMULTIPASTE, SC_MULTIPASTE_EACH);
After that, it works like Visual Studio, you hold down the Alt key and drag with the mouse to start a rectangular selection.