I need to draw some things on the screen for only 1 frame.
Things such as a line of text.
I tried to achieve this with moving windows, but I failed. I cannot get them to show for only 1 frame (8-16 msec), either by showing/hiding or moving in and out of place.
Is there any way to do this?
(Urgh, for the curious, I'm doing this for someone else, so rationalizing over the reason why this needs to be done is useless.)
Edit: Last thing I tried:
public partial class Form2 : Form
{
static Random rand = new Random();
public void ShowString(string s)
{
this.label1.Text = s; // Has .AutoSize = true
this.Size = this.label1.Size;
var bnds = Screen.PrimaryScreen.WorkingArea;
bnds.Size -= this.Size;
this.Location = new Point(rand.Next(bnds.X, bnds.Right), rand.Next(bnds.Y, bnds.Bottom));
}
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Location.X != -10000 && Location.Y != -10000)
{
Location = new Point(-10000, -10000);
timer1.Interval = Program.interval; // How long to wait before showing two things.
}
else
{
timer1.Interval = Program.delay; // For how long to show.
ShowString("just something to test");
}
}
}
And before that:
public partial class Form2 : Form
{
static Random rand = new Random();
public void ShowString(string s)
{
this.label1.Text = s; // Has .AutoSize = true
this.Size = this.label1.Size;
var bnds = Screen.PrimaryScreen.WorkingArea;
bnds.Size -= this.Size;
this.Location = new Point(rand.Next(bnds.X, bnds.Right), rand.Next(bnds.Y, bnds.Bottom));
}
public Form2()
{
InitializeComponent();
timer1.Interval = Program.interval;
}
private void Form2_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void Form2_Move(object sender, EventArgs e)
{
if (Location.X != -10000 && Location.Y != -10000)
{
Thread.Sleep(Program.delay); // Dirty cheat, but I was just trying.
this.Location = new Point(-10000, -10000);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
ShowString("just something to test");
}
}
Related
Animation not working when enabled double buffer in my form. Here is my form code with simple animation. A panel slide-out animation is not working like animation. Slid-in animation is working well. Any idea?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Tick += new System.EventHandler(timer1_Tick);
}
Timer timer1 = new Timer
{
Interval = 100
};
private void timer1_Tick(object sender, EventArgs e)
{
int currentheight = panel1.Height;
int maxheight = 137;
int minheight = 0;
if (currentheight == maxheight)
{
do
{
panel1.Height--;
}
while (panel1.Height > minheight);
}
else
{
do
{
panel1.Height++;
}
while (panel1.Height < maxheight);
}
timer1.Stop();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
}
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
private void timer3_Tick(object sender, EventArgs e)
{
}
Here is the full code to do that. Just add a Label named "label1" on your window.
public partial class Form1 : Form
{
private Timer timer3;
private int counter;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
counter = 0;
timer3 = new Timer();
timer3.Interval = 3000;
timer3.Tick += Timer3_Tick;
timer3.Start();
}
private void Timer3_Tick(object sender, EventArgs e)
{
counter++;
label1.Text = counter.ToString();
}
}
I want to create a C# form in which two text box show two different numbers.
After clicking on start button both numbers should start incrementing at same time should increment slowly so we can see them increment and clicking on stop button should stop increment.
Both text box are not related to each other any way.
public partial class Form1 : Form
{
Thread t1 = new Thread(new ThreadStart(increment1));
public static int fNumber = 0, sNumber = 0,flag = 0;
public Form1()
{
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
t1.Start();
}
private void button4_Click(object sender, EventArgs e)
{
}
private void number1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
public static void increment1()
{
Form1 frm = new Form1();
for (int i = fNumber;i<1000;i++)
{
frm.number1.Text = Convert.ToString(i);
}
}
}
public void Increment1()
{
for (int i = fNumber;i<1000;i++)
{
number1.Text = Convert.ToString(i);
number2.Text = Convert.ToString(i);
}
}
And just generally avoid static for threads. You can access your textboxes directly without the word this if you named your textboses number1 and number2. In a static method the variables need to be static aswell or you will get a compilation error.
For visibility, functions should have first letter in upper case. So you can distinguish them more easily from variables.
You are creating a new instance of Form1 which is wrong. Use the current Form1 that started the Thread.
Try
public partial class Form1 : Form
{
private Timer timer1;
public static int fNumber = 0, sNumber = 0,flag = 0;
public Form1()
{
timer1 = new Timer();
timer1.Interval = 1000;
timer1.Tick += timer1_Tick;
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button4_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void number1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
int i = 0;
int.TryParse(this.number1.Text, out i);
i++;
if(this.number1.InvokeRequired)
{
this.number1.BeginInvoke((MethodInvoker) delegate()
{
this.number1.Text = Convert.ToString(i);
});
}
else
{
this.number1.Text = Convert.ToString(i);
}
}
}
For safety measures, check if invoking is required.
Replace this.number1.Text = Convert.ToString(i); with this block of code.
if(this.number1.InvokeRequired)
{
this.number1.BeginInvoke((MethodInvoker) delegate()
{
this.number1.Text = Convert.ToString(i);
});
}
else
{
this.number1.Text = Convert.ToString(i);
}
I have listbox with some items in it, what I'm looking for this:
always previous items keep selected and if i click an item that isn't selected it get selected too but if it's already selected it get's unselected.
I used these code but it doesn't work very well!
here's my code(this one does not work at all):
public partial class Options_Form : Form
{
public Options_Form()
{
InitializeComponent();
}
private void Options_Load(object sender, EventArgs e)
{
AceMP_Class cl = new AceMP_Class();
listBox1.Items.AddRange(cl.SupportedFiles_stringarray());
listBox1.SelectionMode = SelectionMode.MultiExtended;
listBox1.Size = listBox1.PreferredSize;
listboxitemsState_array = new bool[cl.SupportedFiles_stringarray().Length];
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.ClearSelected();
//selecteditemsindex_list.Clear();
}
//List<int> selecteditemsindex_list = new List<int>();
bool[] listboxitemsState_array;
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < listboxitemsState_array.Length; i++)
{
if (listboxitemsState_array[i])
{
listBox1.SetSelected(i, true);
}
else
{
listBox1.SetSelected(i, false);
}
}
//if (listBox1.GetSelected(listBox1.IndexFromPoint(e.X, e.Y)))
if (listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)])
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), false);
listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)] = false;
}
else
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), true);
listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)] = true;
}
}
private void listBox1_MouseUp(object sender, MouseEventArgs e)
{
for (int i = 0; i < listboxitemsState_array.Length; i++)
{
if (listboxitemsState_array[i])
{
listBox1.SetSelected(i, true);
}
else
{
listBox1.SetSelected(i, false);
}
}
//if (listBox1.GetSelected(listBox1.IndexFromPoint(e.X, e.Y)))
if (listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)])
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), false);
listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)] = false;
}
else
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), true);
listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)] = true;
}
}
}
but this one works but not very well!
public partial class Options_Form : Form
{
public Options_Form()
{
InitializeComponent();
}
private void Options_Load(object sender, EventArgs e)
{
AceMP_Class cl = new AceMP_Class();
listBox1.Items.AddRange(cl.SupportedFiles_stringarray());
listBox1.SelectionMode = SelectionMode.MultiExtended;
listBox1.Size = listBox1.PreferredSize;
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.ClearSelected();
selecteditemsindex_list.Clear();
}
List<int> selecteditemsindex_list = new List<int>();
private void listBox1_Click(object sender, EventArgs e)
{
listBox1.ClearSelected();
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < selecteditemsindex_list.Count; i++)
{
listBox1.SetSelected(selecteditemsindex_list[i], true);
}
}
private void listBox1_MouseUp(object sender, MouseEventArgs e)
{
for (int i = 0; i < selecteditemsindex_list.Count; i++)
{
listBox1.SetSelected(selecteditemsindex_list[i], true);
}
if (listBox1.GetSelected(listBox1.IndexFromPoint(e.X, e.Y)))
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), false);
}
else
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), true);
selecteditemsindex_list.Add(listBox1.IndexFromPoint(e.X, e.Y));
}
}
}
How can I solve that!?
I want to mention that you achieved this feature with SelectionMode.MultiExtended but you need to press CTRL + Click or SHIFT + Click.
To achieve this only with mouse:
1) A better solution comes up as when i discuss with #ACE (he come up with idea to use virtualkey) is to press CTRL key when you make clicks. On MouseDown you press CTRL and on MouseUp release the CTRL key.
public partial class Options_Form : Form
{
public Options_Form()
{
InitializeComponent();
}
private void Options_Load(object sender, EventArgs e)
{
AceMP_Class cl = new AceMP_Class();
listBox1.Items.AddRange(cl.SupportedFiles_stringarray());
listBox1.SelectionMode = SelectionMode.MultiSimple;
listBox1.Size = listBox1.PreferredSize;
}
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const uint KEYEVENTF_KEYUP = 0x02;
public const uint VK_CONTROL = 0x11;
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
keybd_event(Convert.ToByte(VK_CONTROL), 0, 0, 0);
}
private void listBox1_MouseUp(object sender, MouseEventArgs e)
{
keybd_event(Convert.ToByte(VK_CONTROL), 0, Convert.ToByte(KEYEVENTF_KEYUP), 0);
}
}
2) The easiest solution that comes up in my mind (with some drawbacks if you pres click very fast) is to use on MouseClick event and to have an array of bool that every index from that array tells me if selected or not.
selecteditemsindex_list[selectedIndex] = !selecteditemsindex_list[selectedIndex];
How it works?
Take the current index and negate (!) the value at that index. This way you obtain select/unselect feature with mouse.
Because i work only with the current index the others values from array remains unmodified and i can use SetSelected for all values from array. This way you can make multiselect with mouse.
public partial class Options_Form : Form
{
public Options_Form()
{
InitializeComponent();
}
bool[] selecteditemsindex_list;
private void Options_Load(object sender, EventArgs e)
{
AceMP_Class cl = new AceMP_Class();
listBox1.Items.AddRange(cl.SupportedFiles_stringarray());
listBox1.SelectionMode = SelectionMode.MultiExtended;
listBox1.Size = listBox1.PreferredSize;
selecteditemsindex_list = new bool[listBox1.Items.Count];
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
var selectedIndex = listBox1.SelectedIndex;
selecteditemsindex_list[selectedIndex] = !selecteditemsindex_list[selectedIndex];
for (int i = 0; i < selecteditemsindex_list.Count(); i++)
{
listBox1.SetSelected(i, selecteditemsindex_list[i]);
}
}
}
The ListBox control has a property 'SelectionMode'
You can set it to :
One
MultiSimple
MultiExtended
see this link for details