Chart Horizontal Scroll with MouseWheel c# winform - c#

i have a problem with my code that i cannot solve:
i have a stacked column chart with indexed x value for the two series that make the chart, i need to scroll horizontally the chart, so i have written this code:
(first in the initialization part)
this.CHART.MouseWheel += CHART_MouseWheel1;
(and then the function part)
private void CHART_MouseWheel1(object sender, MouseEventArgs e)
{
try
{
if (e.Delta > 0)
{
if (this.CHART.ChartAreas[0].AxisX.ScaleView.Position < this.CHART.ChartAreas[0].AxisX.Maximum)
{
this.CHART.ChartAreas[0].AxisX.ScaleView.Position = this.CHART.ChartAreas[0].AxisX.ScaleView.Position + 1;
}
}
else if (e.Delta < 0)
{
if (this.CHART.ChartAreas[0].AxisX.ScaleView.Position > this.CHART.ChartAreas[0].AxisX.Minimum)
{
this.CHART.ChartAreas[0].AxisX.ScaleView.Position = this.CHART.ChartAreas[0].AxisX.ScaleView.Position - 1;
}
}
}
catch { }
}
but for some reason my maximum value this.CHART.ChartAreas[0].AxisX.Maximum exceed the maximum number of the points in the series so i end up with something like this when i scroll to much:
chart visualization in runtime
and i cannot see any relation between my maximum value for the scrolling and the number of point in the series
i also think that i need to tell you that i have this.CHART.ChartAreas[0].AxisX.IsMarginVisible = true; enabled.
hope to solve this also because i am very close to finish my project.
ty all in advance

Hiii guys i found an answer to my problem by myself after trying lots of things, and the main things is that the series X values needs to be indexed, and that make a lot of things easier because you will now work with number of point and not with, in my case, a datetime value, soo that is the code:
private void CHART1_MouseWheel(object sender, MouseEventArgs e)
{
try
{
if (this.CHART1.Series["S1"].Points.Count > 10)
{
if (e.Delta > 0)
{
if (this.CHART1.ChartAreas[0].AxisX.ScaleView.Position < this.CHART1.ChartAreas[0].AxisX.Maximum - this.CHART1.ChartAreas[0].AxisX.ScaleView.Size - 1)
{
this.CHART1.ChartAreas[0].AxisX.ScaleView.Position = this.CHART1.ChartAreas[0].AxisX.ScaleView.Position + 1;
}
}
else if (e.Delta < 0)
{
if (this.CHART1.ChartAreas[0].AxisX.ScaleView.Position > 1)
{
this.CHART1.ChartAreas[0].AxisX.ScaleView.Position = this.CHART1.ChartAreas[0].AxisX.ScaleView.Position - 1;
}
}
}
}
catch (System.Exception ex)
{
throw ex;
}
}
i hope that i have helped same problem tyall

Related

Horizontal swipe gesture on UWP

is there any way to get swipe gesture same as windows phone 8 with wptoolkit.
Because wptoolkit nuget package is not available for uwp, so i am unable to get similar swipe gesture on UWP
In windows Phone 8 with the help of WPtoolkit nugetget package
i placed this
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Flick="OnSwipe"/>
</toolkit:GestureService.GestureListener>
over text block, so i can swipe left to right or right to left over textbox1.
and swipe gesture help me to implement this
private static int i;
private void OnSwipe(object sender, FlickGestureEventArgs e)
{
if (e.HorizontalVelocity < 0)
{
i++;
txtBox1.Text = i.ToString();
}
if (e.HorizontalVelocity > 0)
{
i--;
txtBox1.Text = i.ToString();
}
}
i tried Manupulation method with scrollViewer on uwp but it continuously increase the value untill it scroll viewer stopped
and this is codes
private static int i;
private Point initialpoint;
private void scrollview_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
initialpoint = e.Position;
}
private void scrollview_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (e.IsInertial)
{
Point currentpoint = e.Position;
if (currentpoint.X - initialpoint.X >= 2)
{
i++;
txtBox1.Text = i.ToString();
}
if (currentpoint.Y - initialpoint.Y >= 2)
{
i--;
txtBox1.Text = i.ToString();
}
}
}
Any other way to implement same functionality.
Actually you don't need to handle ManipulationStarted in this case and you don't need the initialPoint property. Assuming you have already defined your ScrollViewer's ManipulationMode to the following
ManipulationMode="TranslateX,TranslateInertia,System"
Then you simply use e.Cumulative.Translation.X to tell how long you have swiped in total
private void scrollview_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
if (e.IsInertial)
{
var swipedDistance = e.Cumulative.Translation.X;
if (Math.Abs(swipedDistance) <= 2) return;
if (swipedDistance > 0)
{
i++;
}
else
{
i--;
}
txtBox1.Text = i.ToString();
}
}
Update
Now that I understand your question better, I think you should handle gesture manipulation on the TextBox itself. If you want instant feedback, simply subscribe to the ManipulationDelta event and create a flag to only run the swipe logic once per touch.
private bool _isSwiped;
private void txtBox1_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (e.IsInertial && !_isSwiped)
{
var swipedDistance = e.Cumulative.Translation.X;
if (Math.Abs(swipedDistance) <= 2) return;
if (swipedDistance > 0)
{
i++;
}
else
{
i--;
}
txtBox1.Text = i.ToString();
_isSwiped = true;
}
}
private void txtBox1_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
_isSwiped = false;
}
Make sure you move all the handlers and set the ManipulationMode onto the TextBox.
<TextBox x:Name="txtBox1"
ManipulationMode="TranslateX,TranslateInertia,System"
ManipulationDelta="txtBox1_ManipulationDelta"
ManipulationCompleted="txtBox1_ManipulationCompleted" />

Shift listView tiles?

I have been attempting to understand the tiled listView in c#.net for weeks now with no succession.
I have shrunk my form's height/width (154x154) to the same tile size as the picture(s) (items with pictures) in my listView (which is docked in the form2); when my form2 is shown, you see item[0] in the listView...that is it! (which I want so far, so good!)
I have set the scrollable property to false to do away with both scrollbars (works perfectly fine so far...)
I have hooked both the left and right arrow keys GLOBALLY (which works as it has been thoroughly debugged) and when clicking the RIGHT arrow key, it should VERTICALLY bring up item[1], and so fourth.
The LEFT arrow key would bring up the previous item till it hits 0.
I have tried the following, but it does NOTHING.
private void HotkeyHandler(int i)
{
{
if (this.listView1.InvokeRequired)
{
SetHotkeyCallback p = new SetHotkeyCallback(HotkeyHandler);
this.Invoke(p, new object[] { i });
}
else
{
switch (i)
{
case 1:
listView1.View = View.List;
if (listView1.TopItem.Index > 0)
{
listView1.TopItem = listView1.Items[listView1.TopItem.Index - 1];
}
listView1.View = View.Tile;
break;
case 2:
listView1.View = View.List;
if (listView1.TopItem.Index < listView1.Items.Count)
{
listView1.TopItem = listView1.Items[listView1.TopItem.Index + 1];
}
listView1.View = View.Tile;
break;
}
}
}
}
Please help me, I been loosing my mind for weeks now.
EDIT: The switch within the function above does go off, I have debugged it; therefore, it is not the invoking that is the problem...
I don't think you can do it directly.
The way to get rid of the ScrollBars is indeed to set Scrollable = false;.
But that means what it says: Now the ListView will not scroll.
Here is a common workaround for many scrolling issues:
Place the Listview in a Panel and make it as large as needed to show all Items.
Then in order to scroll simply move the LV up and down:
private void prepare_Click_1(object sender, EventArgs e)
{
// we sit inside a Panel
listView1.Parent = panel1;
// initially they have the same size
listView1.Size = panel1.Size;
listView1.Location = Point.Empty;
// a few test items
for (int i = 0; i < 100; i++)
listView1.Items.Add("Item " + i);
// now grow the height to display all items:
int cols = listView1.Width / listView1.TileSize.Width;
listView1.Height = (listView1.Items.Count / cols) * listView1.TileSize.Height;
}
// moving the LV up looks like scrolling down..
private void scrollDown_Click(object sender, EventArgs e)
{
listView1.Top -= listView1.TileSize.Height;
if (listView1.Bottom < panel1.Height)
listView1.Top = -listView1.Height + panel1.Height;
}
// moving the LV down looks like scrolling up..
private void scrollUp_Click_1(object sender, EventArgs e)
{
listView1.Top += listView1.TileSize.Height;
if (listView1.Top > 0) listView1.Top = 0;
}

Change fontSize of ListView with ctrl + mousewheel

I have a ListView (auftraegeView). Of this ListView I whish to change the FontSize of its Items through Ctrl + MouseWheel aka. a simple zoom like in excel or a browser.
In the form's ctor I subscribed my method to the event
this.MouseWheel += scrollZoom;
My EventHandler calculates the new FontHeight and applies it, if it doesn't exceed the bounds. The RowHeight is always kept a little bigger, finally I resize the columns so the zoom also works on the horizontal scale.
private void scrollZoom(object sender, MouseEventArgs e)
{
if(Control.ModifierKeys != Keys.Control)
return;
int currFontHeight = ListViewFontHeight;
int delta = (e.Delta)/120;
int newFontHeight = currFontHeight + delta;
if(newFontHeight < 1 || newFontHeight > 150)
return;
ListViewFontHeight = newFontHeight;
ListViewRowHeight = ListViewFontHeight + 4;
auftraegeView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
ListViewFontHeight gets the Font.Height of the first Item. (Value is identical across all Items, so the first is as good as any.)
The set is where the issue seems to be (see below). My idea is that I just go through each Item and change the Font.
private int ListViewFontHeight
{
get { return auftraegeView.Items[0].Font.Height; }
set
{
foreach (ListViewItem line in auftraegeView.Items)
{
line.Font = new Font(line.Font.FontFamily, value);
}
}
}
ISSUE / QUESTION
Regardless of the direction I scroll in, the FontSize only increases till it hits the ceiling. The rest works fine (setting ListViewRowHeight, detecting the event at all,...).
What might be causing this?
Try this:
delta = (e.Delta > 0? 1 : -1);
to be on the safe side for different mouse settings.
This works for me:
float delta = (e.Delta > 0 ? 2f : -2f);
listView1.Font = new Font (listView1.Font.FontFamily, listView1.Font.Size + delta);
Found it myself:
In the ListViewFontHeight - property the get accessor used Item.Font.Height instead of Item.Font.Size
private int ListViewFontHeight
{
get { return (int)auftraegeView.Items[0].Font.Size; } //works now

Moving a user control around within another user control

I'm trying to write a tetris clone, and after doing some research I've come across an example that uses small user controls to form the block and a bigger user control that contains the grid.
Everything I've written already seems to work just fine (blocks are being generated and placed on the grid, i can even place them somewhere else if I change the code), but I can't seem to get the blocks to move while the program is running. The example I use does this by changing the control.left property of each of the blocks. I've tried this, debugged it and while the property changes, the block doesn't move.
I've searched around for about 4 hours now. I'm a novice programmer, so I know it's probably something stupid, but I cant find what it is.
Here's the methods I've written:
//Class TetrisGame.cs
public void MoveRight()
{
blok.MoveBlock("x", 1);
}
//Class Shape.cs
public void MoveBlock(string pos, int Amount)
{
if (pos == "x")
{
for (int i = 0; i < this.Shape().Count; i++)
{
((Blokje)this.Shape()[i]).MoveSide(1);
}
}
if (pos == "y")
{
for (int i = 0; i < this.Shape().Count; i++)
{
((Blokje)this.Shape()[i]).MoveDown(1);
}
}
//And, the code that should actually move the block in Block.cs:
public void MoveSide(int Step)
{
this.Left += (Step * 20);//Blocks are 20*20 pixels so should move in steps of 20 pixels
}
Shape is actually an arraylist which just contains the 4 Blocks. Block.cs is a partial class since it's the code behind the usercontrol that is the small squares, Shape.cs makes the shapes out of the blocks and tetrisgame is just the gamelogic
the Keypress event:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
if (e.KeyChar == 'q')//left
{
if (!paused)
{
Game.MoveLeft();
}
}
else if (e.KeyChar == 'd')//right
{
if (!paused)
{
Game.MoveRight();
}
}
else if (e.KeyChar == 'p')//pause
{
if (paused)
{
tmrGame.Start();
}
else
{
tmrGame.Stop();
}
}
else if (e.KeyChar == 'z')//rotate
{
if (!paused)
{
Game.Rotate();
}
}
else if (e.KeyChar == 'h')//help
{
Help.Show();
}
else if (e.KeyChar == 'f')//save
{
}
else if (e.KeyChar == 's')//Drop
{
if (!paused)
{
Game.Drop();
}
}
}
catch
{
//no error message has to be displayed, this is just to prevent runtime Errors when pressing keys before the game has started
}
}
It seems that the "bigger user control that contains the grid" with it's children is not redrawn.
Change MoveSide to:
public void MoveSide(int Step)
{
this.Left += (Step * 20);
Update();
}
so everything is redrawn correctly.

how to make a text boxes text move back and forth?

hello atm I have this code
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
x = 0;
timer1.Enabled = true;
timer1.Start();
}
else
{
timer1.Enabled = false;
}
}
private int x = 0;
private int y = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (x <= 10)
{
x++;
string ping = new string(' ', x) + "hello";
label1.Text = ping;
if (x == 10)
{
y = 10;
}
}
else if (y > 0)
{
y--;
string pong = new string(' ', y) + "hello";
label1.Text = pong;
if (y == 0)
{
x = 0;
}
}
}
at the moment the label has a maximum length of 15 characters and i want it to stay that way.
but i want it to instead of using "hello" to take the text i input into a textbox and do it.
however it has to take 15 and subtract the length of the textboxes text in order to keep the labels max length of 15 intact while displaying the entire word in the textbox aswell but i cant figure out how to do it i have tried plenty of things but i cannot figure it out any help would be greatly appreciated. :D
Your use of the words "ping" and "pong", plus your title saying "move back and forth" leads me to believe the result you want can be achieved by changing the TextAlign property of the label upon each tick.
If this is the result you want, you won't have to add spaces at all. The text will appear to go from left to right edges in the label. You might consider trimming the text property with TRIM() to ensure no spaces exist on either side that would make it appear aligned incorrectly.

Categories

Resources