Scroll into a RichTextBox using buttons c# - c#

How can I code two buttons for scrolling into a RichTextBox up and down ? What I try :
private void btnScrollTop_Click(object sender, EventArgs e) {
if (rtbDefinitie.SelectionStart >= 30) {
rtbDefinitie.SelectionStart -= 30;
rtbDefinitie.ScrollToCaret();
}
}
private void btnScrollBottom_Click(object sender, EventArgs e) {
if (rtbDefinitie.SelectionStart <= 30) {
rtbDefinitie.SelectionStart += 30;
rtbDefinitie.ScrollToCaret();
}
}
But it's seems to get stuck after I press scroll down button twice. What I need to do ?

The second click seems to be interpreted as DoubleClick, so you could just register this event too and put the same code behind it (or replace the 30 by 60)
EDIT:
If the Application stucks, because it's working and does not have the time to update the GUI, you could try to call Application.DoEvents(); after every raised Clickevent:
private void btnScrollBottom_Click(object sender, EventArgs e) {
if (rtbDefinitie.SelectionStart <= rtbDefinitie.TextLength) {
rtbDefinitie.SelectionStart += 30;
rtbDefinitie.ScrollToCaret();
Application.DoEvents();
}
}

Related

How to move a winforms tool to the right and then to the left continuously in the forms by using point class and timer tools?

Purpose of this code was to move a title(label) first rightwards until it hits the 600th pixel on the X axis and then leftwards until it hits the 27th pixel on the X axis of the form by using 2 timer tools and the Point class. One timer for going right and the other timer for going left. They should've work by swithing on and off consecutively after one another, however it does not work.
The label is stuck at 600th X location and does not move back to where it was.
The timer interval is 100 so it moves with a decent speed that allows us to see it moving.
namespace AlanCevreHesabiUygulamasi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (label11Point.X == 27)
{
timer2.Stop();
timer1.Start();
}
if (label11Point.X == 599)
{
timer1.Stop();
timer2.Start();
}
}
Point label11Point = new Point(27, 32);
private void timer1_Tick(object sender, EventArgs e)
{
while (label11Point.X <= 600)
{
label12.Text = label11Point.X.ToString();
label11Point.X += 1;
label11.Location = label11Point;
break;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
while (label11Point.X >= 27)
{
label12.Text = label11Point.X.ToString();
label11Point.X -= 1;
label11.Location = label11Point;
break;
}
}
}
}
Label is stuck at 600th pixel of the form, does not move back. How to make it work?
I'm surprised you see movement resulting from a while loop in a timer tick handler. Why have a timer if your are going to do it that way.
In this solution, I have a timer, and the movements happen during a timer tick. I also have three possible directions, Right, Left and Stopped (in case you want to have a start/stop button).
In the Windows Forms designer, I dropped both a label and a timer on the form. I left their properties alone except for the timer's Interval property (that I set to 10 (ms))
Then I added an enum and an instance of the enum as a field to the Form class:
public partial class Form1 : Form
{
private enum Direction { MoveRight, MoveLeft, Stopped }
Direction _direction;
public Form1()
{
InitializeComponent();
}
}
I double-clicked the Caption area of the form in the designer to create a form Load handler, and added a call to start the timer:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
Finally, I double-clicked the timer to get a timer Tick handler and added some code:
private void timer1_Tick(object sender, EventArgs e)
{
var curLocation = label1.Location;
if (_direction == Direction.MoveRight && curLocation.X > 600)
{
_direction = Direction.MoveLeft;
}
else if (_direction == Direction.MoveLeft && curLocation.X < 27)
{
_direction = Direction.MoveRight;
}
int offset = _direction switch
{
Direction.MoveRight => 1,
Direction.MoveLeft => -1,
_ => 0,
};
curLocation.X += offset;
label1.Location = curLocation;
}
The _direction field determines if the label is moving to the right or the left.
How can you write the code without a timer?"
You asked "How can you write the code without a timer?" I'm still flabbergasted that your label moves as the result of a while loop in an event handler - something must have changed from my good-old understanding of Win32 processing.
Anyways, I cheat and await a call to Task.Delay instead of using a timer. Take my existing code and do the following:
Add two buttons to your form (One labeled Start (named StartBtn) and the other labeled Stop (named StopBtn).
Add another Direction-typed field to the class: Direction _previousDirection;
Comment out the call to timer1.Start(); in the Form1_Load handler
Comment out all the code in the timer1_Tick method (at this point, you could remove the timer from the form if you want)
Select both buttons (Start and Stop) and press <Enter>. This will bring up click handlers for both buttons.
Change the StopBtn button's handler to look like:
New Stop Button code:
private void StopBtn_Click(object sender, EventArgs e)
{
_previousDirection = _direction;
_direction = Direction.Stopped;
}
Change the StartBtn's handler to look like the following. Note that nearly everything in the while loop (except the call to Task.Delay) is the same as the previous timer tick handler code. Also note that I made the handler async void to allow for the await keyword to do it's magic.
Start Button code:
private async void StartBtn_Click(object sender, EventArgs e)
{
_direction = _previousDirection;
while (_direction != Direction.Stopped)
{
var curLocation = label1.Location;
if (_direction == Direction.MoveRight && curLocation.X > 600)
{
_direction = Direction.MoveLeft;
}
else if (_direction == Direction.MoveLeft && curLocation.X < 27)
{
_direction = Direction.MoveRight;
}
int offset = _direction switch
{
Direction.MoveRight => 1,
Direction.MoveLeft => -1,
_ => 0,
};
curLocation.X += offset;
label1.Location = curLocation;
await Task.Delay(10);
}
}
I solved it, I don't know why but in my opening post the Form1() function only makes one of the if() conditions work. However, putting the if() statements into the timers solved the problem. Now, the title goes back and forth in the specified x axis intervals.
namespace AlanCevreHesabiUygulamasi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Start();
}
Point label11Point = new Point(27, 32);
private void timer1_Tick(object sender, EventArgs e)
{
while (label11Point.X >= 27)
{
label12.Text = label11Point.X.ToString();
label11Point.X += 1;
label11.Location = label11Point;
break;
}
if (label11Point.X == 600)
{
timer2.Start();
timer1.Stop();
}
}
private void timer2_Tick(object sender, EventArgs e)
{
while (label11Point.X <= 600)
{
label12.Text = label11Point.X.ToString();
label11Point.X -= 1;
label11.Location = label11Point;
break;
}
if (label11Point.X == 27)
{
timer1.Start();
timer2.Stop();
}
}

Button long click [duplicate]

This question already has answers here:
Long pressed button
(5 answers)
Closed 2 years ago.
I have a timer developed with c# and WinForms controlled by an external button
when I press the button, the time starts, this is the main action! I would like to know
if is it possible to recognize a long press in this button so I can program the action to stop
I am very new to C#, by programming language side is it possible to recognize this long press?
my solution:
create a public datetime variable and add button_mousedown and button_mouseup event to your form.
put the following code in there:
private void button1_MouseDown(object sender, MouseEventArgs e)
{
dt = DateTime.Now;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
var diff= ( DateTime.Now-dt).TotalMilliseconds;
if(diff > x) // x is the minimum time you want the mouse to be down on the button
{
// do stuff
}
}
when you do a click on the button, the exact time is stored in the "dt" variable.
when the click is over, the mouseup event is triggered and there you can compare the time, when you pressed the button with the currenttime and you can get a difference in milliseconds with my code
You can use the button MouseDown and MouseUp events.
Here is one way to create a counter with textbox and button with "Long Press" event:
private bool IsTargetButtonClicked = false;
private Timer HoldButtonTimer;
private void TargerButton_MouseUp(object sender, MouseEventArgs e)
{
IsTargetButtonClicked = false;
}
private void TargerButton_MouseDown(object sender, MouseEventArgs e)
{
IsTargetButtonClicked = true;
InitHoldButtonTimer();
}
private void InitHoldButtonTimer()
{
if (HoldButtonTimer == null)
{
HoldButtonTimer = new Timer();
HoldButtonTimer.Interval = 100;
HoldButtonTimer.Tick += HoldButtonTimer_Tick;
}
HoldButtonTimer.Start();
}
private int SomeIntValueToAdvance = 0;
private void HoldButtonTimer_Tick(object sender, EventArgs e)
{
if (IsTargetButtonClicked)
{
SomeIntValueToAdvance++;
textBox1.Invoke(new Action(() =>
{
textBox1.Text = SomeIntValueToAdvance.ToString();
}));
}
else
{
KillHoldButtonTimer();
}
}
private void KillHoldButtonTimer()
{
if (HoldButtonTimer == null)
{
HoldButtonTimer.Tick -= HoldButtonTimer_Tick;
HoldButtonTimer.Stop();
HoldButtonTimer.Dispose();
}
}
OUTPUT:

How do I stop an animation with a timer if the animated object came to a specific location?

I am trying to do some animations with panels and a timer in a C# Windows Forms project. Currently, there is a panel (in my code called 'pnSideBlock') which is located at the left edge of the window. Right next to it is a button.
The idea is that if the mouse enters the button, then the panel should relocate outside of the window (in my project the panel has a width of 20px, therefor I make its new location 20px outside of the window) and will move into it again with the help of a timer. As soon as the panel has reached a specific location, in my case 0px, then the timer should stop and the panel should stop moving.
But at the moment, the timer just keeps on going, which causes the panel to keep moving to the right.
private void btnMainMenu_MouseEnter(object sender, EventArgs e)
{
pnSideBlock.Left = -20;
timerForAnim.Enabled = true;
if(pnSideBlock.Left == 0)
{
timerForAnim.Enabled = false;
}
}
private void btnMainMenu_MouseLeave(object sender, EventArgs e)
{
timerForAnim.Enabled = false;
}
private void timerForAnim_Tick(object sender, EventArgs e)
{
pnSideBlock.Left += 1;
}
according to your code you need to change it to be like this:
private void btnMainMenu_MouseEnter(object sender, EventArgs e)
{
pnSideBlock.Left = -20;
timerForAnim.Enabled = true;
}
private void btnMainMenu_MouseLeave(object sender, EventArgs e)
{
timerForAnim.Enabled = false;
}
private void timerForAnim_Tick(object sender, EventArgs e)
{
pnSideBlock.Left += 1;
if(pnSideBlock.Left == 0)
{
timerForAnim.Enabled = false;
}
}
this change is required becouse btnMainMenu_MouseEnter happen only once when you enter to the button with the mouse.
and you need to check if the panel in place after every move.
Something similar, using a async Method.
Make your Button's MouseEnter handler async and call the async Task SlidePanel() method, specifying the initial and final position of the Control to slide and the speed of the animation (expressed in milliseconds).
When the Control is first entered, it's MouseEnter handler is detached (so it won't be raised again if the mouse pointer enters it while the animation is playing) and is be wired again when the animation finishes.
You can interact with the Button as usual.
The animation will be performed to its end even if the Button shows a modal Window when clicked (a MessageBox, for example).
Visual behaviour:
private async Task SlidePanel(Control control, int start, int end, int speed)
{
while (start < end)
{
await Task.Delay(speed);
start += 1;
control?.BeginInvoke(new Action(() => control.Left += 1));
}
}
private async void btnSlider_MouseEnter(object sender, EventArgs e)
{
btnMainMenu.MouseEnter -= btnMainMenu_MouseEnter;
pnSideBlock.Left -= pnSideBlock.Width;
await SlidePanel(pnSideBlock, pnSideBlock.Left, pnSideBlock.Width, 100);
btnMainMenu.MouseEnter += btnMainMenu_MouseEnter;
}

C# specified time label change the structure

int sn = 0;
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Konfigürasyon Yükleniyor.";
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (sn == 3)
{
label1.Text = "Ayarlar Alınıyor";
}
if (sn == 5)
{
label1.Text = "Program Başlatılıyor";
}
sn++;
timer1.Stop();
}
When I open the form I want to change the label when I select the text range.
I assume that event handler is attached in designer to this timer1.
As far as I can understand, this label is never set, because you stop Timer after it hits first time.
In this case variable sn = 0 so non of if condition from your event handler is met.
I think to solve problem you sould remove this timer1.Stop() from event handler.
You probably want
private void timer1_Tick(object sender, EventArgs e) {
if (sn == 3)
label1.Text = "Ayarlar Alınıyor";
else if (sn == 5) {
label1.Text = "Program Başlatılıyor";
timer1.Stop(); // <- stop timer here on the 5th second, not on the 1st one
}
sn++;
}

How to animate a PictureBox in a Win Forms application?

I have a windows form application with a PictureBox control containing an image. I want to move the PictureBox control to the right in a slow movement. Here is my code:
Point currentPoint = pictureBox_Logo.Location;
for (int i = 0; i < 25; i++)
{
pictureBox_Logo.Location = new Point(pictureBox_Logo.Location.X + 1, pictureBox_Logo.Location.Y);
Thread.Sleep(30);
}
The problem here is that when the code executes instead of seeing the picture move, I see a white picture move and the moving stops until the picture appears.
What am I missing and what can I do about it?
Code:
public partial class Form1 : Form
{
void timer_Tick(object sender, EventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
if (x > this.Width)
timer1.Stop();
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Show();
timer1.Start();
}
}
original thread is here Move images in C#
Try to use pictureBox_Logo.Refresh() after Thread.Sleep(30);
Or look for standard Timer control.
My code is good written, but what I did wrong was putting the code in an event:
private void Form1_Shown(object sender, EventArgs e);
But when I put my code in a button it works without any problems.

Categories