I'm trying to implement a fade in/out to main form open/close, open fade in works fine, however close fade out requires 2 button clicks to work.
Current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace DEMO
{
public partial class MainForm : Form
// fade in timer set
System.Windows.Forms.Timer t1 = new System.Windows.Forms.Timer();
System.Windows.Forms.Timer t2 = new System.Windows.Forms.Timer();
private void Form_Load(object sender, EventArgs e)
{
//fade in function
Opacity = 0;
t1.Interval = 10;
t1.Tick += new EventHandler(fadeIn);
t1.Start();
}
// fade in
void fadeIn(object sender, EventArgs e)
{
if (Opacity >= 1)
t1.Stop();
else
Opacity += 0.05;
}
// fade out
void fadeOut(object sender, EventArgs e)
{
if (Opacity <= 0)
{
t2.Stop();
Close();
}
else
Opacity -= 0.15;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
e.Cancel = true;
t2.Tick += new EventHandler(fadeOut);
t2.Start();
if (Opacity <= 0)
e.Cancel = false;
}
private void customImageButton1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
I first tried to make it like the button to do the tick start with the e.Cancel but changing the EventArgs e to FormClosingEventArgs e only gave the error: No overload for 'customImageButton1_Click' matches delegate 'System.EventHandler' on which I couldn't find a solution
Related
I have a primitive app with 2 comboBoxes. They work fine after first starting the app.
However after typing in text in the search bar and pressing enter, the comboboxes loop their contents.
It happens after I type in the textbox, even if I do not press enter. Every time I press a key another repeat list of options appends to the comboBox.
How do I prevent this comboBox malfunction? Here is my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//Nick Knapp
//CSCI 363 Fall 2019
namespace c363_hw3_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Library Literature Search";
this.BackColor = Color.White;
comboBox2.Items.Add("All");
comboBox2.Items.Add("Books");
comboBox2.Items.Add("Papers");
comboBox2.Items.Add("Films");
comboBox2.Items.Add("CDs");
comboBox2.Items.Add("Other");
comboBox2.SelectedIndex = 0;
comboBox1.Items.Add("Title");
comboBox1.Items.Add("Author");
comboBox1.Items.Add("Publisher");
comboBox1.Items.Add("ISBN");
comboBox1.SelectedIndex = 0;
// this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnter);
//this.Controls.Add(textBox1);
this.ActiveControl = textBox1;
textBox1.KeyPress += new KeyPressEventHandler(keypressed);
}
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
textBox1.Text = "";
e.Handled = true;
}
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
I think your code is correct but have you ever tried these 2 method after calling combo boxes?
comboBox1.ResetText();
comboBox1.Items.Clear();```
i think it works
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
textBox1.Text = "";
comboBox1.Items.Clear();
e.Handled = true;
}
}
I have a problem I made a new form, with background img, and all I need and its working like I wanted, but I also need to auto close it after 5 or 10 seconds.
I searched on google all day ... but no tutorial was good.
I use Visual Studio 2013.
Can you boys help me please...
I'm desperate right now... its almost 10 hours since I'm trying.
You are my last hope.
Thanks
this.close() dosen't did it, or I made it wrong but i doubt that.
Application.Exit fail
timers give errors...
//form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Cerum_HS
{
public partial class CERUM_HS : Form
{
public CERUM_HS()
{
InitializeComponent();
Rectangle r = Screen.PrimaryScreen.WorkingArea;
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
}
}
}
//main.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
//using System.Windows.Forms;
namespace Cerum_HS
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
private static System.Timers.Timer aTimer;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CERUM_HS());
aTimer = new System.Timers.Timer();
aTimer.Interval = 10;
aTimer = new System.Timers.Timer(10);
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = false;
aTimer.Enabled = true;
}
private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
//Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
Application.Exit();
//this.close();
}
}
}
Since my comment seemed to help, I thought I write it down as an answer.
public partial class CERUM_HS :
{
// here is the timer for the automatic closing
private static System.Timers.Timer aTimer;
public CERUM_HS()
{
InitializeComponent();
Rectangle r = Screen.PrimaryScreen.WorkingArea;
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
}
private void Form_Load(object sender, System.EventArgs e)
{
// start here the timer when the form is loaded
aTimer = new System.Timers.Timer();
aTimer.Interval = 10;
aTimer = new System.Timers.Timer(10);
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = false;
aTimer.Enabled = true;
}
private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
// Close the Application when this event is fired
Application.Exit();
}
}
Bogdan please comment if this implementation is how it worked for you in the end.
I would put a PictureBox and timer on your form (set to 5000 ms), click on the Tick event, and use this code:
namespace Image
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// set picture box to image of interest
// size and position form appropriately
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
this.Close();
}
}
}
I'm new to c# and Multithreading. I have this code to getting started with Multithreading but clock tick isn't getting started. What's wrong with this code? No error occurs because its a logical error I guess. Any help would be appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace Implementing_Databases
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
picturebox1.Location=new Point(0,20);
pictureBox2.Location = new Point(0, 60);
}
int B1 = 0;
int B2 = 0;
private void Form1_Load(object sender, EventArgs e)
{
Thread Th1 = new Thread(Go1);
Thread Th2 = new Thread(Go2);
Th1.Start();
Th2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
picturebox1.Left = B1;
B1 += 5;
}
private void timer2_Tick(object sender, EventArgs e)
{
pictureBox2.Left = B2;
B2 += 5;
}
void Go1()
{
timer1.Start();
}
void Go2()
{
timer2.Start();
}
}
}
First of all, try declaring threads as properties of form rather than declaring them as local function variables. Because otherwise they may be collected by GC straight away after Load handler exits.
Secondly, UI not updating may be due to the fact that you cannot update UI data from non GUI thread. See InvokeRequired/Invoke feature of WinForms programming. See https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired(v=vs.110).aspx for more details
I have a metronome project set up. I have a tap button which should check the tempo of your beat and average it out. Every bit of math works properly because I checked it with a calculator. Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
namespace Metronome
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer3_Tick(object sender, EventArgs e)
{
// Convert tempo to timer1.Tick (miliseconds between each beat)
timer1.Interval = Convert.ToInt32(60000 / numericUpDown1.Value);
}
private void button1_Click(object sender, EventArgs e)
{
// Play / Pause button
if (button1.Text == "Go!") { timer1.Enabled = true; button1.Text = "Stop!"; }
else if (button1.Text == "Stop!") { timer1.Enabled = false; button1.Text = "Go!"; }
}
private void timer1_Tick(object sender, EventArgs e)
{
// The 'ding' sound for the metronome
SystemSounds.Beep.Play();
}
private void button2_Click(object sender, EventArgs e)
{
// Set the tempo to be the average of the convertion from miliseconds between 2 beats and the current tempo
if (timer2.Enabled) { numericUpDown1.Value = ((60000 / Tap) + numericUpDown1.Value) / 2; Tap = 0; }
else timer2.Enabled = true;
}
int Tap = 0;
private void timer2_Tick(object sender, EventArgs e)
{
// Get the amount of miliseconds between each beat
Tap++;
}
private void button3_Click(object sender, EventArgs e)
{
// Reset the tap timer
timer2.Enabled = false;
Tap = 0;
}
}
}
The problem is in timer2_Tick, because it should add 1 to Tap every milisecond, instead, when I tried it it goes to a tiny number like 20 or 30. How can I fix this?
There is a really good article I always rely on when selecting which timer to use:
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
I would suggest using one of the threaded options. Specifically, the article says of the the windows forms timer (System.Windows.Forms.Timer):
If you're looking for a metronome, you've come to the wrong place.
If you only need to check the amount of time passed between button taps, use a StopWatch. It gives you a high precision timing mechanism. There is no need for you to count milliseconds yourself.
I'm working on a c# project with WPF but I've a problem and this makes me crazy :)
Here is the problem. I'm trying to change new window's opacity with timer. But when I run the project, "this.Opacity += .1;" code throws an exception like "Invalid operation etc..."
I'm opening a window from MainWindow.cs file with this code:
private void MenuItemArchiveClick(object sender, RoutedEventArgs e)
{
var archiveWindow = new ArchiveWindow();
var screenSize = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
archiveWindow.Width = (screenSize.Width * 95) / 100;
archiveWindow.Height = (screenSize.Height * 90) / 100;
archiveWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
archiveWindow.Margin = new Thickness(0, 10, 0, 0);
archiveWindow.AllowsTransparency = true;
archiveWindow.Opacity = 0.1;
archiveWindow.Topmost = true;
archiveWindow.Show();
}
My ArchiveWindow code is,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace POCentury
{
/// <summary>
/// Interaction logic for ArchiveWindow.xaml
/// </summary>
public partial class ArchiveWindow : Window
{
Timer timer1 = new Timer();
public ArchiveWindow()
{
InitializeComponent();
timer1.Interval = 1 * 1000;
timer1.Elapsed += new ElapsedEventHandler(opacityChange);
timer1.Enabled = true;
timer1.AutoReset = false;
timer1.Start();
}
private void opacityChange(object sender, EventArgs a)
{
if (this.Opacity == 1)
{
timer1.Stop();
}
else
{
this.Opacity += .1;
}
}
private void ArchiveWindowClose()
{
timer1.Stop();
this.Close();
}
private void btnArchiveWindowClose(object sender, RoutedEventArgs e)
{
ArchiveWindowClose();
}
private void imgPatternClick(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("sd");
}
}
}
Can you help me with doing that?
Thank you so much!
Basically, you can't access the timer inside the opacityChange event because it's happening in a different thread. You need the Dispatcher to do that.
Dispatcher.BeginInvoke(new Action(() =>
{
if (this.Opacity == 1)
{
timer1.Stop();
}
else
{
this.Opacity += .1;
}
}));