I'm new to C# and started making my own program. How ever, I want these (https://gyazo.com/8b4e0f4141b15e1ff204e1cfc8f41827) to disappear until my progress bar loads (https://gyazo.com/5064ebd9582593942c6e538e1ae516dc). I tried to use Visible but got nowhere.
ProgressBar code:
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
var before = this.progressBar1.Value;
this.progressBar1.Increment(1);
var after = this.progressBar1.Value;
if (after > before && after == this.progressBar1.Maximum)
{
MessageBox.Show("Successfully loaded...");
this.BackgroundImage = Properties.Resources.image1;
}
}
In "Form.Load" set Image.Visible = false and when you are showing the MessageBox set Image.Visible = true
Related
private void Form1_Load(object sender, EventArgs e)
{
if (MouseMove == button1;)
{
button1.Size= 100;70;
}
}
}
}
I couldn't find how to write code
Use the MouseEnter event to capture the mouse cursor hovering over the button borders,
and the MouseLeave event to detect when the cursor leaves the button borders, in order to return it to its original size.
Here is one way to implement such functionality:
private Size OriginalButtonSize;
private Size HoveredSize;
private void Form1_Load(object sender, EventArgs e)
{
OriginalButtonSize = this.button1.Size;
HoveredSize = new Size(OriginalButtonSize.Width + 30, OriginalButtonSize.Height + 30);
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.Size = HoveredSize;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Size = OriginalButtonSize;
}
Output:
You can use this code:
private void button1_MouseHover(object sender, EventArgs e)
{
button1.Width = 100;
button1.Height = 70;
}
if you want to set size to the previous size after leaving the mouse, you can use this (for example it was 60 and 30 at first place):
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Width = 60;
button1.Height = 30;
}
I have two TextBoxes and a button control in the form. When the button is clicked the name of the last entered TextBox should be displayed in a MessageBox. At the same time I need to reset the focus to last entered TextBox.
string str=string.Empty;
bool foc;
In button click I wrote the following code
if (MessageBox.Show("You want to reset or continue", "control",
MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
if (foc== true)
{
textBox1.Focus();
}
else
{
textBox2.Focus();
}
}
When I clicks on cancel button the focus should be into textbox which is entered at last
private void textBox1_Enter(object sender, EventArgs e)
{
str = textBox1.Name;
foc= textBox1.Focus();
}
private void textBox2_Enter(object sender, EventArgs e)
{
str= textBox2.Name;
foc= false;
}
Other than the above lines of code is there any other possibility to focus into the textbox, but when number of textboxes increases how i need to write the conditions.
If I am having textbox,combobox,listbox,checkbox or any other controls in the form then how to find in which control the user enterd at last and set focus to that control by using any function instead of writing in every control Enter event
You can handle Leave event of the TextBoxes to store the Last TextBox Control.
Try this:
this.btnSubmit.Click += new System.EventHandler(this.Submit_Click);
this.btnCancel.Click += new System.EventHandler(this.Cancel_Click);
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
TextBox txtLast = new TextBox();
private void textBox1_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void textBox2_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void Submit_Click(object sender, EventArgs e)
{
MessageBox.Show(txtLast.Text);
}
private void Cancel_Click(object sender, EventArgs e)
{
txtLast.Focus();
}
public bool textBox1WasLastFocused = false, textBox2WasLastFocused = false; // Global Declaration
void textBox2_GotFocus(object sender, EventArgs e)
{
textBox2WasLastFocused = true;
textBox1WasLastFocused = false;
}
void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1WasLastFocused = true;
textBox2WasLastFocused = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1WasLastFocused)
MessageBox.Show("textbox1 was ladst focused !");
else if(textBox2WasLastFocused)
MessageBox.Show("textbox2 was ladst focused !");
}
I want to change the radiobutton location and make it move up while i am clicking button
tried this
private void up_MouseDown(object sender, MouseEventArgs e)
{
while(P.Location.Y>0)
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
}
P is a radiobutton
I want it to keep moving up while I'm pressing, but it's just jumping up to the up of the form.
it's working good in debugging but it's really moving fast
I want to slow the movement of the radiobutton and make it visible
Actually you are starting a while loop that will not exit until your RadioButton is at the top of your Form wether you are still pressing the Button or not. You can slow it down by putting a Thread.Sleep in your loop that way it is slowed down visible.
private void up_MouseDown(object sender, MouseEventArgs e)
{
while (P.Location.Y > 0)
{
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
System.Threading.Thread.Sleep(10);
}
}
If you want to have better control I would use a Timer. In this example the Interval is set to 10.
private void up_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start();
}
private void up_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (P.Location.Y > 0)
{
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
}
}
You can use timer. Add a timer from the toolbox, say its name was timer1, then add following method:
private void P_MouseUp(object sender, MouseEventArgs e) {
timer1.Enabled=false;
}
private void P_MouseDown(object sender, MouseEventArgs e) {
timer1.Enabled=true;
}
private void timer1_Tick(object sender, EventArgs e) {
if(P.Location.Y>0)
P.Location=new System.Drawing.Point(P.Location.X, P.Location.Y-1);
}
You can change the interval of timer1 in properties window. I guess you write this for fun; so, have fun!
When I click on a textbox, I want the default text to disappear. Is there any other property that would work for this purpose?
The following Will set Default text in Gray color. Also When you leave the textbox as blank, again set default text to the textbox.
private void Form1_Load(object sender, EventArgs e)
{
this.textBox2.Enter += new EventHandler(textBox2_Enter);
this.textBox2.Leave += new EventHandler(textBox2_Leave);
textBox2_SetText();
}
protected void textBox2_SetText()
{
this.textBox2.Text = "Default Text...";
textBox2.ForeColor = Color.Gray;
}
private void textBox2_Enter(object sender, EventArgs e)
{
if (textBox2.ForeColor == Color.Black)
return;
textBox2.Text = "";
textBox2.ForeColor = Color.Black;
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Trim() == "")
textBox2_SetText();
}
Add a method to the GotFocus event of the TextBox that will change the Text property to ""
private void Form1_Load(object sender, EventArgs e) {
this.textBox1.GotFocus += new EventHandler(textBox1_Focus);
this.textBox1.Text = "some default text...";
}
protected void textBox1_Focus(Object sender, EventArgs e) {
textBox1.Text = "";
}
It sounds like you are wanting a Watermark in your Textbox.
See these articles.
http://www.codeproject.com/Articles/319910/Custom-TextBox-with-watermark
Watermark in System.Windows.Forms.TextBox
and if you are using wpf something like this http://msdn.microsoft.com/en-us/library/bb613590.aspx
I have a button on which a click and it takes a screenshot which i display in my Picture Box. I dont face issue with this code:
private void btnScreenShot_Click(object sender, EventArgs e)
{
btnSave.Visible = true;
sendto_bmpbox.Image = CaptureScreen();
}
However when i loop the entire Form freezes and i cannot click on anything:
private void btnScreenShot_Click(object sender, EventArgs e)
{
// Freezes here
btnSave.Visible = true;
while(flag == 0)
{
sendto_bmpbox.Image = CaptureScreen();
}
}
How do i fix this problem?
That's because your while() is infinite. What makes flag change from capture to capture?
In case you want to infinitely capture the screen - never use the main thread for such things, as it will cause it to hang and prevent your application from updating the UI.
Use the BackgroundWorker class for things like that, you can use this example.
private void button1_Click(object sender, EventArgs e)
{
btnSave.Visible = true;
Thread thread = new Thread(new ThreadStart(threadWork));
thread.Start();
}
int flag = 0;
private void threadWork()
{
while (flag == 0)
{
UpdateImage();
}
}
private void UpdateImage()
{
if (this.InvokeRequired)
{
this.Invoke(UpdateImage);
}
else
{
sendto_bmpbox.Image = CaptureScreen();
}
}
Try Application.DoEvents in loop. I think this can help you...
private void btnScreenShot_Click(object sender, EventArgs e)
{
// Freezes here
btnSave.Visible = true;
while(flag == 0)
{
Application.DoEvents();
sendto_bmpbox.Image = CaptureScreen();
}
}