I'm developing a C# WinForms application. I can't find a solution to this probably because I'm new.
I need to create a teleprompter like text that scrolls from the bottom and goes up in a loop.
is there any simple solution or a code snippet?
An example would be nice so that I can understand how it is being done.
You could also create a Label control with text, and simply decrease its Vertical position with 1 (in pixels) every 1/20th of a second or so.
The idea is you could use the timer control, handle it Tick event
myTimer.Tick += new EventHandler(TimerEventProcessor);
Set myTimer.Interval = 1000;// event will fire every sec
private static void TimerEventProcessor(Object myObject,EventArgs myEventArgs) {
/// your logic to add new text, and change text position to give scroll effect
}
In TimerEventProcessor, put you logic to change text position that is to change it y coordinate, add new text in the bottom, this way you can create the scroll effect
In the timer.tick event handler you could do
if(label.Location.Y < 20)
label.Location = new Point(label.Location.X, this.ClientSize.Height);
else
label.Location = new Point(label.Location.X, label.Location.Y - 1);
Hope this help
Related
I am trying to use this plugin for C#, which works great. However I am having difficulty getting the image to center when zooming in. By default, whenever zooming, the picture box focuses on the top left corner. I would like it to focus on the centre.
Any idea? I have searched far and wide and cannot find an example which does this within this plugin.
Ok, this getting complicated so I will write complete answer:
The plugin is not open in the way You can hook on the zooming events. Actualy thing You can do (as You have open sources) is to "hack" inside - change the source code and adjust to Your needs.
Options:
1. Change inner implementation:
Get to file ScalablePictureBoxImp.cs , find method ScalePictureBoxToFit(), and add the codes after lines:
//some previous code
this.pictureBox.Left = left;
this.pictureBox.Top = top;
this.AutoScroll = true;
// add this line under:
this.AutoScrollPosition = new Point(this.Width / 2, this.Height / 2);
Now You can simply use the component as before, and it will do all the things just with resizing them on middle (it possibly need some more math to fit the screen properly).
2. Change outer implementation:
Author is ussing wrapper for the inner implementation, where he is hooking on the events and You are just using the wrapped object (it is like pressing the buttons on a mouse instead of connecting 1 wire with some other).
In file ScalablePictureBox.cs edit the Constructor and add following implementation:
public ScalablePictureBox()
{
//some code ...
this.pictureTracker.ScrollPictureEvent += new PictureTracker.ScrollPictureEventHandler(this.scalablePictureBoxImp.OnScrollPictureEvent);
this.pictureTracker.PictureTrackerClosed += new PictureTracker.PictureTrackerClosedHandler(this.pictureTracker_PictureTrackerClosed);
//Enter the line below to hook on event
this.scalablePictureBoxImp.ZoomRateChangedEvent += ScalablePictureBoxImp_ZoomRateChangedEvent;
}
Now You have hooked on the event of zooming, and You just need to adjust the scrolling:
private void ScalablePictureBoxImp_ZoomRateChangedEvent(int zoomRate, bool isFullPictureShown)
{
this.scalablePictureBoxImp.AutoScrollPosition = new Point(this.Width / 2, this.Height / 2);
}
Inner implementation of the ScalablePictureBox is invoking event ZoomRateChangedEvent -> Once this zoom change, You will get the size of a window and move the scroll buttons to the middle position. This can be adjusted in any way You requiring.
I believe, originaly autor wanted to write ScalablePictureBoxImp only and the wrapper was added just for test/simply case purpose. It is completely up to You if You write it all Yourself or adjust -> Correct way should be using the outer implementation then.
I have a user control which consists of two controls and four buttons, arranged on a Form: the two controls are on the sides and the buttons in the middle in a vertical row.
When using the control in an app, I place it on a form.
Now, when re-sizing the form horizontally, the two controls just move left or right w/o changing their size.
What I need is that the controls stay anchored to the middle of the form and grow to the sides (sorry about the lack of clarity, I prepared screenshots but the site wouldn't let me attach them).
Is there a way to accomplish this without overriding the Resize event?
Use a TableLayoutPanel as base for your user control.
You need 3 columns and one row. The middle column needs to have a fixed size, and the other 2 you set to 50%. Not to worry, .Net is smart enough to calculate the percent they actually take.
Inside the right and left columns you put your controls and set the Dock property of both to fill. In the middle column you put a panel and set it's Dock property to fill as wall, and In that panel you put the buttons in the middle.
Set your table layout panel Dock to fill as well, and when adding the user control to the form use Dock top, bottom or fill as well.
Erratum:
The above code works most of the time, but it fails for certain Move-Resize sequences. The solution is to respond to the Move and Resize events of the parent form (the consumer of the control), not of the control itself.
One more thing: due to the event firing order (Move first followed by Resize, had to move the working code from Resize() to Move(), which seems counterintuitive but it seems the right way nevertheless.
It seems indeed that it cannot be done in the Designer, but here is the solution using overrides.
It works ok, except for some control flickering which I haven't been able to overcome.
public partial class SB : UserControl
{
//variables to remember sizes and locations
Size parentSize = new Size(0,0);
Point parentLocation = new Point (0,0);
......
// we care only for horizontal changes by dragging the left border;
// all others take care of themselves by Designer code
public void SB_Resize(object sender, EventArgs e)
{
if (this.Parent == null)
return;//we are still in the load process
// get former values
int fcsw = this.parentSize.Width;//former width
int fclx = this.parentLocation.X;//former location
Control control = (Control)sender;//this is our custom control
// get present values
int csw = control.Parent.Size.Width;//present width
int clx = control.Parent.Location.X;//present location
// both parent width and parent location have changed: it means we
// dragged the left border or one of the left corners
if (csw != fcsw && clx != fclx)
{
int delta = clx - fclx;
int lw = (int)this.tableLayoutPanel1.ColumnStyles[0].Width;
int nlw = lw - delta;
if (nlw > 0)
{
this.tableLayoutPanel1.ColumnStyles[0].Width -= delta;
}
}
this.parentSize = control.Parent.Size;//always update it
this.parentLocation = control.Parent.Location;
}
//contrary to documentation, the Resize event is not raised by moving
//the form, so we have to override the Move event too, to update the
//saved location
private void SB_Move(object sender, EventArgs e)
{
if (this.Parent == null)
return;//we are still in the load process
this.parentSize = this.Parent.Size;//always update it
this.parentLocation = this.Parent.Location;
}
}
The above code works most of the time, but it fails for certain Move-Resize sequences. The solution is to respond to the Move and Resize events of the parent form (the consumer of the control), not of the control itself.
One more thing: due to the event firing order (Move first followed by Resize, had to move the working code from Resize() to Move(), which seems counterintuitive but it seems the right way nevertheless.
I've created Windows forms and I'm using the textbox control for input, but I like to use it without border and other layout for textbox etc. I just want to use a underscore line and blinking cursor.
I played with the borderStyle (Fixed3D, None), backcolor=InactiveBorder etc. But I still do net get the underline... like this-> _____________ result like this: This is underline______________
I think Backcolor=InactiveBorder and BorderStyle=None is ok to use, but how to get the underline and blinking cursor?
Requirement:
blinking cursor and underline. (The doesn't blink by default, I just see a vertical line))
To fake this, you could add a label below the text box with the content being _____________________. My preferred solution would be to create a simple custom control that just draws a line.
Doesn't the caret on your system blink by default? It does on my system if the focus is on the text box.
If the caret doesn't blink by default, go to the Windows Control Panel and check your Keyboard Settings there - this is the place where you can adjust the caret blink rate.
For creating a underline for your textbox you can do like this,
First add a panel which is in the height of text box's height + underline's height.
Now add your textbox inside of that panel and set its dock to TOP.
Then set the textbox's border to none.
Now set the backcolor of the panel, according to the color need of underline.
Update:
This is VB code, i hope that you can easily convert it into c#
[ Concept: You just need to set the border for all of your textboxes as none.then In forms paint event track those text boxes and draw a line under it. ]
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint
Using xPen As Pen = New Pen(Color.Blue)
' Here we are using LINQ to filter the controls.
' If you don't want it, you just check all the controls by using typeof
' inside the For Each loop.
For Each xTxtBox In Me.Controls.OfType(Of TextBox)()
e.Graphics.DrawLine(xPen,
xTxtBox.Location.X,
xTxtBox.Location.Y + xTxtBox.Height,
xTxtBox.Location.X + xTxtBox.Width,
xTxtBox.Location.Y + xTxtBox.Height)
Next
End Using
End Sub
Use Masked TextBox and set Focus , e.g. maskedtextbox1.Focus(); <== this is for the blinking cursor and the masked textbox to the underline !
try :
To set logical focus to an input control
FocusManager.SetFocusedElement(this, textboxJack);
To set keyboard focus to an input control
Keyboard.Focus(textboxJill);
and for the masked textbox you can set a mask that will not be changed when you delete the text from it not like the simple textbox :)
Good luck
To do this, I would recommend creating a custom control (which is accomplished in the WinForms world by inheriting from one of the provided control classes). That custom control would then:
Provide its own drawing logic (by overriding OnPaint) in order to draw the underline and skip drawing anything else you don't want to see (e.g., the borders of the control).
Create its own caret when it receives the focus, and destroy that caret when it loses the focus. You'll find all the details on how to do this in my answer here.
You can also configure the blink rate of the caret by calling the SetCaretBlinkTime function. But note that this is not recommended, as it changes the global system setting and therefore affects other applications. It is best to do as Thorsten suggests and modify the setting on your machine if you wish to see something different. You should always respect a user's settings. There's a reason that they (or someone else) set up their system to not blink the caret.
Naturally, you will need to use P/Invoke to call these Win32 API functions related to caret management from a C# application. That shouldn't be too difficult if you know what you're doing. If you need a complete solution, consider setting a bounty on this question to persuade me to write one up for you.
I faced the same issue and built something that works fine:
public class TextBox : System.Windows.Forms.TextBox
{
public TextBox()
{
BorderStyle = BorderStyle.None;
Text = "__________"; //Sometime this doesn't work while creating the control in design mode ; don't know why
}
//protected override void OnFontChanged(EventArgs e)
//{
// base.OnFontChanged(e);
// RefreshHeight();
//}
bool loaded = false;
protected override void OnCreateControl()
{
if(!loaded)
RefreshHeight();
base.OnCreateControl();
}
private void RefreshHeight()
{
loaded = true;
Multiline = true;
Size s = TextRenderer.MeasureText(Text, Font, Size.Empty, TextFormatFlags.TextBoxControl);
MinimumSize = new Size(0, s.Height + 1);
Multiline = false;
}
}
I used bool loaded = false to avoid the app to crash in a loop because of OnCreateControl. TextBox control doesn't have OnLoad event (I'm open to another approach).
OnFontChanged can be uncommented if your app change the font size in run time
MinimumSize = new Size(0, s.Height + 1); I added 1 to avoid any error of MeasureText
I have a form and couple of labels on it. I am creating a screensaver with the said form. So far, I have been able to have the text labels appear at different intervals.
Howveer, I want to take it notch further. I would like to have a text scroll in from the top or bottom in right or left direction. Can I do this directly with C#? How?
I would appreciate a helpful answer. Thanks.
You want your text to "fly" on the form? If yes, why just not changing the label position?
Havent seen something like it... But if you know what is Expression Blend, you can make your own element styles (color, form, size, ANIMATION). You can try to realize it there...
Create a timer with a low interval and just use:
//Move right:
label1.Location = new Point(label1.Location.X + 1, label1.Location.Y);
//Move left:
label1.Location = new Point(label1.Location.X - 1, label1.Location.Y);
Maybe something like this:
private void timer1_Tick(object sender, EventArgs e)
{
if (label1.Location.X <= Width)
label1.Location = new Point(label1.Location.X + 1, label1.Location.Y);
else
label1.Location = new Point(0, label1.Location.Y);
}
You and any one else wanting to do something like that can simple use the following framework:
WinForm Animation Library [.Net3.5+]
A simple library for animating controls/values in .Net WinForm (.Net
3.5 and later). Key frame (Path) based and fully customizable.
https://falahati.github.io/WinFormAnimation/
new Animator2D(
new Path2D(new Float2D(-100, -100), lbl_label.Location.ToFloat2D(), 500))
.Play(lbl_label, Animator2D.KnownProperties.Location);
This slides the lbl_label label from -100, -100 to the location it was in first place in 500 ms.
i have made a cool progressbar with text in the middle of it. Here is my code:
private void timer2_Tick(object sender, EventArgs e)
{
progressBar1.Increment(+1);
int percent = progressBar1.Value;
progressBar1
.CreateGraphics()
.DrawString(
percent.ToString() + "%",
new Font("Arial", (float)8.25, FontStyle.Regular),
Brushes.Black,
new PointF(progressBar1.Width / 2 - 10,
progressBar1.Height / 2 - 7)
);
if (progressBar1.Value >= 99)
{
timer2.Stop();
this.Close();
}
}
For some reason, the text shows up then disappears and other weird stuff. Why is that, and how do i fix it?
Try moving the drawing code into the Paint event, you are basically modifying the control's visuals, so you need to handle this instead of the default painting behaviour.
Your drawing gets erased whenever the control is repainted.
Instead, you need to set a flag indicating whether to draw the text right now, then handle the Paint event, and, if the flag is true, draw the text.
In the timer tick handler, toggle the flag and call Invalidate().
Another approach would be to create a UserControll and use a label on top of the progress bar, and use each separately, this might prove easier to do, hardly any additional code will be needed for this.