I have DataGrid with columns inside it.
I want the DataGrid to be refreshed every 3 seconds, with data from the service.
I need that the Scroll will not jump to the top of the DataGrid, every time it refreshes, and stay on the same place of the Scroll. Is it possible?
Here is my code :
<sdk:DataGrid Grid.Row="1" FlowDirection="RightToLeft" ColumnHeaderHeight="32"
AutoGenerateColumns="False" Name="RealTimeReportDataGrid"
RowEditEnded="LinesDataGrid_RowEditEnded" RowHeight="40">
<sdk:DataGrid.Columns>...</sdk:DataGrid.Columns>
</sdk:DataGrid>
This is the code behind:
public RealTimeReport()
{
InitializeComponent();
Loaded += OnViewLoaded;
RealTimeReportService.RealTimeReportServiceClient RTws = new RealTimeReportService.RealTimeReportServiceClient();
RTws.GetRealTimeReportAsync();
RTws.GetRealTimeReportCompleted += new EventHandler<RealTimeReportService.GetRealTimeReportCompletedEventArgs>(RTws_GetRealTimeReportCompleted);
}
void RTws_GetRealTimeReportCompleted(object sender, RealTimeReportService.GetRealTimeReportCompletedEventArgs e)
{
var t = e.Result.ToList();
RealTimeReportDataGrid.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<ShahalDialerRT.RealTimeReportService.RealTimeReport>(e.Result.ToList());
}
private void OnViewLoaded(object sender, RoutedEventArgs e)
{
Start();
}
private void OnUpdaterTimerTick(object sender, EventArgs e)
{
RealTimeReportService.RealTimeReportServiceClient RTws = new RealTimeReportService.RealTimeReportServiceClient();
RTws.GetRealTimeReportAsync();
RTws.GetRealTimeReportCompleted += new EventHandler<RealTimeReportService.GetRealTimeReportCompletedEventArgs>(RTws_GetRealTimeReportCompleted);
}
public void Start()
{
InitializeRefreshDataTimer();
}
public void InitializeRefreshDataTimer()
{
_updaterTimer.Interval = new TimeSpan(0, 0, 0, 0, 3000);
_updaterTimer.Tick += OnUpdaterTimerTick;
_updaterTimer.Start();
}
Thanks ahead.
Update:
#StepUp, this is the only change I made, according to what you said, but still no change :(
void RTws_GetRealTimeReportCompleted(object sender, RealTimeReportService.GetRealTimeReportCompletedEventArgs e)
{
var t = e.Result.ToList();
RealTimeReportDataGrid.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<ShahalDialerRT.RealTimeReportService.RealTimeReport>(e.Result.ToList());
this.RealTimeReportDataGrid.Dispatcher.BeginInvoke(() =>
{
RealTimeReportDataGrid.ScrollIntoView(
RealTimeReportDataGrid.SelectedItem,
RealTimeReportDataGrid.CurrentColumn);
});
//RealTimeReportDataGrid.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<ShahalDialerRT.RealTimeReportService.RealTimeReport>(e.Result.ToList());
//RealTimeReportDataGrid.UpdateLayout();
//RealTimeReportDataGrid.ScrollIntoView(RealTimeReportDataGrid.SelectedItem, RealTimeReportDataGrid.Columns[0]);
}
To refresh every 3 seconds just use Timer class:
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval=3000;
aTimer.Enabled=true;
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
//add new data
}
The Elapsed event will be raised every X amount of seconds, specified in milliseconds by the Interval property on the Timer object. It will call the Event Handler method you specify, in the example above it is OnTimedEvent.
To stay Scroll on the same position in DataGrid:
yourDataGrid.ItemsSource = data;
yourDataGrid.UpdateLayout();
yourDataGrid.ScrollIntoView(theDataGrid.SelectedItem, theDataGrid.Columns[0]);
Update:
yourDataGrid.Dispatcher.BeginInvoke(() =>
{
yourDataGrid.Focus();
yourDataGrid.SelectedItem=whateverYouWant;
yourDataGrid.CurrentColumn=yourDataGrid.Columns[0];
yourDataGrid.ScrollIntoView(yourDataGrid.SelectedItem, yourDataGrid.CurrentColumn);
});
OR:
yourDataGrid.SelectedIndex = 5; // the index you want to select
yourDataGrid.UpdateLayout();
yourDataGrid.ScrollIntoView(Grid.SelectedItem, 0);
Related
I try to move a grid in my window with an animation, but the animation doesn't happen, so I put a timer so that you can see the animation little by little but it still dont work :/
Here is my code:
class Animation
{
private int x_dropGridToBottom;
private Grid grid_dropGridToBottom;
private System.Timers.Timer t;
public Animation()
{
t = new System.Timers.Timer();
t.Interval = 500;
}
private void TimerElapsed_DropGridToBottom(object sender, ElapsedEventArgs e)
{
while (x_dropGridToBottom > 0)
{
x_dropGridToBottom--;
Application.Current.Dispatcher.Invoke(() =>
{
grid_dropGridToBottom.Margin = new Thickness(0, x_dropGridToBottom, 0, x_dropGridToBottom);
});
}
if (x_dropGridToBottom == 0)
t.Stop();
}
internal void DropGridToBottom(Grid grid, Window window)
{
grid.Margin = new Thickness(0, window.Height, 0, window.Height);
// 0 x 0 x (x = window.height)
// x = -y (y=412)
// x (=) 0
grid_dropGridToBottom = grid;
x_dropGridToBottom = Convert.ToInt32(window.Height);
t.Elapsed += new ElapsedEventHandler(TimerElapsed_DropGridToBottom);
t.Start();
}
}
And here is the code when I press the button that is supposed to start the animation :
private void Button_Click(object sender, RoutedEventArgs e)
{
Animation anim = new Animation();
anim.DropGridToBottom(grid_2, this);
}
Somebody has an idea of why and how I can make my grid move little by little but not directly in one go (as it does nowadays).
Thank you
I'm trying to hold down mouse event using PreviewMouseDown and DispatcherTimer as following:
private void button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
_sec = _sec + 1;
if (_sec == 3)
{
dispatcherTimer.Stop();
MessageBox.Show(_sec.ToString());
_sec = 0;
return;
}
}
This code works, BUT the first mouse down takes 3 seconds to display the message, after that the time to show the message is decreased (less that 3 seconds)
You don't need a DispatcherTimer to do this. You could handle the PreviewMouseDown and the PreviewMouseUp events.
Please refer to the following sample code.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
PreviewMouseDown += Window3_PreviewMouseDown;
PreviewMouseUp += Window3_PreviewMouseUp;
}
DateTime mouseDown;
private void Window3_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
mouseDown = DateTime.Now;
}
readonly TimeSpan interval = TimeSpan.FromSeconds(3);
private void Window3_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if (DateTime.Now.Subtract(mouseDown) > interval)
MessageBox.Show("Mouse was held down for > 3 seconds!");
mouseDown = DateTime.Now;
}
}
The second time this gets called
dispatcherTimer.Tick += dispatcherTimer_Tick; // try without that new EventHandler(...)
a second handled will be attached. So, after the first second, sec will be 2, since the event is called twice.
You can try to dispose and set to null the dispatcherTimer variable on the PreviewMouseUp & create a new instance on the PreviewMouseDown.
Or another option would be, on the PreviewMouseUp, you can
dispatcherTimer.Tick -= dispatcherTimer_Tick;
sec = 0;
-= will detach the event handler.
Here I have a chart (graph1) that normally should add a random point every 1second. but it doesn't... I tried to find out what the problem is but here I don't have anymore ideas...
The timer is started, label1 change every seconds but the chart doesn't change... with button one when I click it adds a new point.
what did I miss? please help... thanks a lot.
namespace Test_Chart1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
graph1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
graph1.ChartAreas[0].AxisX.IsLabelAutoFit = true;
graph1.ChartAreas[0].AxisX.ScaleView.Size = 40;
System.Timers.Timer _Timer1s = new System.Timers.Timer(1000); //object
_Timer1s.Elapsed += _Timer1sElapsed; //event in object
_Timer1s.Start(); //start counting
}
private void _Timer1sElapsed(object sender, EventArgs e)//Timer each 100ms
{
if (label1.BackColor == Color.Red)
{
label1.BackColor = Color.Blue;
PutValueInGraph1();
}
else label1.BackColor = Color.Red;
}
private void button1_Click(object sender, EventArgs e)
{
PutValueInGraph1();
}
private void PutValueInGraph1()
{
graph1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
graph1.ChartAreas[0].AxisX.IsLabelAutoFit = true;
graph1.ChartAreas[0].AxisX.ScaleView.Size = 100;
Random Rand_Value = new Random();
int ValueToAdd = Rand_Value.Next(1, 100);
listBox1.Items.Add(ValueToAdd.ToString());
graph1.Series["Data1"].Points.AddY(ValueToAdd);
if (graph1.ChartAreas[0].AxisX.Maximum-10 > graph1.ChartAreas[0].AxisX.ScaleView.Size)
{
graph1.ChartAreas[0].AxisX.ScaleView.Scroll(graph1.ChartAreas[0].AxisX.Maximum);
graph1.Series["Data1"].Points.RemoveAt(0);
}
}
}
}
ok here is the new one:
public partial class Form1 : Form
{
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
myTimer.Tick += new EventHandler(TimerEventProcessor);
myTimer.Interval = 1;
}
private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
Random Rand_Value = new Random();
int ValueToAdd = Rand_Value.Next(1, 100);
listBox1.Items.Add(ValueToAdd.ToString());
graph1.Series["Data1"].Points.AddY(ValueToAdd);
if (graph1.ChartAreas[0].AxisX.Maximum - 10 > graph1.ChartAreas[0].AxisX.ScaleView.Size)
{
graph1.ChartAreas[0].AxisX.ScaleView.Scroll(graph1.ChartAreas[0].AxisX.Maximum);
graph1.Series["Data1"].Points.RemoveAt(0);
}
}
private void btn_Start_Click_1(object sender, EventArgs e)
{
graph1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
graph1.ChartAreas[0].AxisX.IsLabelAutoFit = true;
graph1.ChartAreas[0].AxisX.ScaleView.Size = 100;
myTimer.Start();
BlinkLed.BackColor = Color.YellowGreen;
}
private void btn_Stop_Click(object sender, EventArgs e)
{
myTimer.Stop();
BlinkLed.BackColor = Color.AliceBlue;
}
}
Do you think it's better?
What about the changing thread?
If I had a button:
private void PutValueInGraph1()
{
Random Rand_Value = new Random();
int ValueToAdd = Rand_Value.Next(1, 100);
listBox1.Items.Add(ValueToAdd.ToString());
graph1.Series["Data1"].Points.AddY(ValueToAdd);
if (graph1.ChartAreas[0].AxisX.Maximum-10 > graph1.ChartAreas[0].AxisX.ScaleView.Size)
{
graph1.ChartAreas[0].AxisX.ScaleView.Scroll(graph1.ChartAreas[0].AxisX.Maximum);
graph1.Series["Data1"].Points.RemoveAt(0);
}
}
private void button1_Click(object sender, EventArgs e)
{//try to raise exception
PutValueInGraph1();
}
and I change the event like this:
private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{//try to raise exception
PutValueInGraph1();
}
The data input accelerate when I'm started the timer and I click all the time on the button1.
Why is there no exception as tom_imk said??
because we can access the same function at the same time....?
Thanks for your answers.
I tried below sample code and it is working fine for me.
public Form7()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.Maximum = 100;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Interval = 1;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Random Rand_Value = new Random();
int ValueToAdd = Rand_Value.Next(1, 100);
chart1.Series[0].Points.AddY(ValueToAdd);
}
I'm surprised you didn't get an exception. You are manipulating UI elements outside the UI thread, something you musn't do, ever.
Refer to the answer in this question:
How to update the GUI from another thread in C#?
EDIT:
To make clear why the timerelapsed method does not run on the UI thread: It's simply the wrong class that is used here. So the easy solution would be to not created a System.Timers.Timer in the Form-constructor but to drop a timer on the form in the form designer and use that instead. The solution by sowjanya attaluri should be marked as the correct answer.
I have four buttons that are called "ship1,ship2" etc.
I want them to move to the right side of the form (at the same speed and starting at the same time), and every time I click in one "ship", all the ships should stop.
I know that I need to use a timer (I have the code written that uses threading, but it gives me troubles when stopping the ships.) I don't know how to use timers.
I tried to read the timer info in MDSN but I didn't understand it.
So u can help me?
HERES the code using threading.
I don't want to use it. I need to use a TIMER! (I posted it here because it doesnt give me to post without any code
private bool flag = false;
Thread thr;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
flag = false;
thr = new Thread(Go);
thr.Start();
}
private delegate void moveBd(Button btn);
void moveButton(Button btn)
{
int x = btn.Location.X;
int y = btn.Location.Y;
btn.Location = new Point(x + 1, y);
}
private void Go()
{
while (((ship1.Location.X + ship1.Size.Width) < this.Size.Width)&&(flag==false))
{
Invoke(new moveBd(moveButton), ship1);
Thread.Sleep(10);
}
MessageBox.Show("U LOOSE");
}
private void button1_Click(object sender, EventArgs e)
{
flag = true;
}
Have you googled Windows.Forms.Timer?
You can start a timer via:
Timer timer = new Timer();
timer.Interval = 1000; //one second
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
timer.Start();
You'll need an event handler to handle the Elapsed event which is where you'll put the code to handle moving the 'Button':
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
MoveButton();
}
I am trying to get my media controls to auto hide after 2 seconds. However the way I have it set up is that it only works when my mouse leaves the StackPanel I have the media controls in. And if I keep moving the in and out of the StackPanel then it will start to flicker as it is firing the hide even multiple times. I am unsure on how to go about this logically. Anyone have any tips or suggestions?
Here is what I got right now (StackPanel is named controls).
...
controls.MouseMove += new MouseEventHandler(control_unhide);
controls.MouseLeave += new MouseEventHandler(control_hide);
...
void control_hide(object sender, MouseEventArgs e)
{
var miniTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
miniTimer.Tick += (s, i) => { miniTimer.Stop(); controls.Opacity = 0; };
miniTimer.Start();
}
//Unhide controls
void control_unhide(object sender, MouseEventArgs e)
{
controls.Opacity = 100;
}
Also from some sample code I have seen people say to use Collapse and Visible to make the controls hide and reappear. This however doesn't work as the Collapse seems to make the boundaries unresponsive to the mouse entering.
Thanks!
**Edit
I asked this question because I spent a lot of time trying to figure this out yesterday only to sit down today and think of a really easy solution. What I did was this:
//global
private DispatcherTimer hideTimer;
....
//init
hideTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
hideTimer.Tick += (s, i) => { hideTimer.Stop(); controls.Opacity = 0; };
hideTimer.Start();
controls.MouseMove += new MouseEventHandler(control_unhide);
controls.MouseLeave += new MouseEventHandler(control_hide);
...
void control_hide(object sender, MouseEventArgs e)
{
hideTimer.Start();
}
//Unhide controls
void control_unhide(object sender, MouseEventArgs e)
{
controls.Opacity = 100;
hideTimer.Stop();
}
A simple solution would be to put in a guard variable and only hide if you weren't currently hiding:
bool currentlyHiding;
void control_hide(object sender, MouseEventArgs e)
{
if (!currentlyHiding)
{
var miniTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
miniTimer.Tick += (s, i) =>
{
miniTimer.Stop();
controls.Opacity = 0;
currentlyHiding = false;
};
miniTimer.Start();
currentlyHiding = true;
}
}
You will also need to do something similar for the unhide.