Look at the following codes
namespace WindowsFormsApplication1
{
public partial class myprogram : Form
{
public myprogram()
{
InitializeComponent();
}
WebKitBrowser wb1 = new WebKitBrowser();
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
wb1.Navigate("site.com");
timer2.Enabled = true;
}
private void timer2_Tick(object sender, EventArgs e)
{
timer2.Enabled = false;
wb1=null;
timer1.Enabled = true;
}
}
}
To empty RAM every time, wb1 must be null. But then it gets null and no longer hits and says it has already been null. So how can I get null in timer2 and then Navigate in timer1?
Add wb1 = new WebKitBrowser(); to timer1_Tick
Related
My problem is exactly that the inheritance of the forms, when opening, closing or compiling the child form the buttons behave as if they had a life of their own, they move from place
Here I attach a link to see what happens to me, I know that putting links is a bad practice, but I need to explain my problem as best as possible
https://1drv.ms/v/s!ApYO6DZzi_l-8zp-Fvr4nFkgta5J?e=d482SE
These are the information for a minimum and reproducible example, I use DevExpress, hopefully it is not a problem
Inherit Parent Form
public partial class BaseMantenimiento1 : DevExpress.XtraEditors.XtraForm
{
public BaseMantenimiento1()
{
InitializeComponent();
}
private void Nuevo_Click(object sender, EventArgs e)
{
Guardar.Enabled = true;
Cancelar.Enabled = true;
Editar.Enabled = false;
Buscar.Enabled = false;
Nuevo.Enabled = false;
}
public void Guardar_Click(object sender, EventArgs e)
{
Nuevo.Enabled = true;
Guardar.Enabled = false;
Cancelar.Enabled = false;
Buscar.Enabled = true;
Editar.Enabled = true;
}
private void Cancelar_Click(object sender, EventArgs e)
{
Guardar.Enabled = false;
Cancelar.Enabled = false;
Nuevo.Enabled = true;
Editar.Enabled = true;
Buscar.Enabled = true;
}
private void Editar_Click(object sender, EventArgs e)
{
Nuevo.Enabled = false;
Buscar.Enabled = false;
Editar.Enabled = false;
Guardar.Enabled = true;
Cancelar.Enabled = true;
}
}
}
Inherit Child Form
public partial class Form1 : BaseMantenimiento1
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
My guess (I cannot access your link to see what the actual problem is) is that the assignment of the event handlers is only happening in the parent form's InitializeComponent, not the child form. Since it is generated by the designer you could either wire them up in the designer for the child form or in the constructor of the child form after InitializeComponent is called.
So, i try to learn how to Drag and Drop an Image from one PictureBox to another and that works well. But how can i drag and drop the Image TAG from pictureox1 to picturebox2?
i currently have 3 source images and 3 drop boxes.
the dropbox6 is locked with a countdown after a buttonclick (see button2)
(later i will lock all dropboxes)
after i hit this button a countdown starts and if the countdown is 0 (ZERO) only then i can drag one of the 3 images to this box.
however, i have given each of these 3 images in the souceboxes a TAG name.
how can i drop this tagname also to the dropbox?
here is what i have so far:
(the Label labeled "Counter" is actually Label4 in 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;
namespace Game
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
// pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox4.AllowDrop = true;
pictureBox5.AllowDrop = true;
pictureBox6.AllowDrop = true;
pictureBox6.Enabled = false;
}
private void pictureBox4_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox4_DragLeave(object sender, EventArgs e)
{
}
private void pictureBox4_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap) e.Data.GetData(DataFormats.Bitmap);
pictureBox4.Image = getPicture;
}
private void pictureBox5_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox5_DragLeave(object sender, EventArgs e)
{
}
private void pictureBox5_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox5.Image = getPicture;
}
private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox6.Image = getPicture;
timer1.Enabled = false;
}
private void pictureBox6_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
timer1.Enabled = false;
}
}
private void pictureBox6_DragLeave(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void pictureBox4_Click(object sender, EventArgs e)
{
MessageBox.Show("test");
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
if (counter == 0)
timer1.Stop();
label4.Text = counter.ToString();
if(counter == 0)
{
pictureBox6.Enabled = true;
label4.Enabled = false;
timer1.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void pictureBox6_Click(object sender, EventArgs e)
{
MessageBox.Show(pictureBox6.Tag.ToString());
}
private int counter = 3;
private void button2_Click(object sender, EventArgs e)
{
int counter = 3;
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000; // 1 second
timer1.Start();
label4.Text = counter.ToString();
if(label4.Text == "0")
{
timer1.Stop();
}
pictureBox6.Image = Properties.Resources.shoreSite_d_1_l_x_x;
button2.Visible=false;
}
}
internal class bild1
{
private Bitmap shoreSite_d_1_l_x_x;
public bild1(Bitmap shoreSite_d_1_l_x_x)
{
this.shoreSite_d_1_l_x_x = shoreSite_d_1_l_x_x;
}
}
}
In MouseDown you are saying what you want to transmit in the DoDragDrop call. In your current case you are transmitting the image ((PictureBox)sender).Image but you can chose to transmit the tag as well...
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Tag, DragDropEffects.Copy);
}
...Then make sure to parse for each possible input type in DragDrop
private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
var image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
var tag = e.Data.GetData(DataFormats.Text) as string;
if (image != null)
{
pictureBox4.Image = image;
}
if (tag != null)
{
pictureBox4.Tag = tag;
}
timer1.Enabled = false;
}
Beware, that you will need to update all the code that checks that the data is Bitmap before it arrives at DragDrop ie in DragEnter and write code that handles both Bitmap and Text, otherwise the Drag functionality will break.
AWESOME!!
It works now. I will continue this Project.
how i can make a loop timer that check if in main form topmost.enable is false until a label is visible and then set to true when the label deactive?
If tried this code but not work:
private void InitializeAlive()
{
alive = new System.Timers.Timer();
alive.Interval = 1000;
alive.AutoReset = true;
alive.Elapsed += Alive_Tick;
alive.Start();
}
private void Alive_Tick(object sender, EventArgs e)
{
if (lblPassword.Enabled)
{
this.TopMost = false;
}
else
{
this.TopMost = true;
alive.Dispose();
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
if (txtPassword.Text == pswd)
{
TopMost = false;
webPrintSetting.ShowPageSetupDialog();
InitializeAlive();
}
else
{
btnPrint.Enabled = false;
btnPrint.Visible = false;
lblPassword.Visible = false;
txtPassword.Enabled = false;
txtPassword.Visible = false;
txtPassword.Clear();
}
}
If you only need to do something when 'Enabled' property of the label changes, then you can simply add handler to the 'EnabledChanged' property, like this:
public Form1()
{
InitializeComponent();
lblPassword.EnabledChanged += new System.EventHandler(this.LblPassword_EnabledChanged);
}
And implement the handler like this:
private void LblPassword_EnabledChanged(object sender, EventArgs e)
{
TopMost = !lblPassword.Enabled;
}
I find a solution to toggle on/off topmost (off until a target process is running).
private Timer check;
public MyForm()
{
InitializeCheck();
}
private void InitializeCheck()
{
check = new Timer();
check.Interval = 5000;
check.Tick += Check_Tick;
check.Enabled = false;
}
private void Check_Tick(object sender, EventArgs e)
{
CheckProgram();
}
private void CheckProgram()
{
Process[] program = rocess.GetProcessesByName("notepad");
if (program.Length == 0)
{
check.Enabled = false;
TopMost = true;
}
private void button1_Click(object sender, EventArgs e)
{
TopMost = false;
check.Enabled = true;
}
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 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");
}
}