Troubles with setting ToolTip duration programmatically - c#

I've come across a situtation where I need to create a tooltip object and show it when the user hovers over specific areas in my application.
I can get the tooltip to show up just fine. The problem is I need it to go poof after a few seconds have passed. I'm aware of the ToolTipService.SetShowDuration and I've tried using it, but I have not been met with much luck.
Here's what I got in my MouseMove event handler:
_toolTip.Placement = PlacementMode.Relative;
_toolTip.Horizontal = e.X;
_toolTip.VerticalOffset = e.Y;
_toolTip.Content = stuffs;
_toolTip.IsOpen = true;
I've tried setting the following:
someObject.ToolTip = _toolTip;
ToolTipService.SetShowDuration(someObject, 5);
Nothing changes with the last two lines. The tooltip still is visible and stays visible. Am I using the service wrong or something? Any thoughts would be much appreciated!

Try this.
<Border Name="border" ToolTip="some message" MouseEnter="border_MouseEnter" Background="red" Margin="50"/>
ToolTip tool = new ToolTip();
private void border_MouseEnter(object sender, MouseEventArgs e)
{
tool.Placement = PlacementMode.Relative;
tool.HorizontalOffset = 100;
tool.VerticalOffset = 200;
tool.Content = "stuffs";
tool.IsOpen = true;
border.ToolTip = tool;
ToolTipService.SetShowDuration(border, 5000);
}

I developed a workaround for the issue.
To give a little more background, I have a 3D model of an aircraft inside of a WindowsFormsHost object. When the user hovers over a identified part, I needed a tooltip to appear.
I created an instance of tooltip and in my MouseMove event and I do something like this:
// selectedPart will be null if no part is selected
if(selectedPart != null && prevSelectedPart != selectedPart)
{
toolTip.IsOpen = false;
host.ToolTip = toolTip;
toolTip.IsOpen = true;
}
else if (prevSelectedPart == selectedPart && prevSelectedPart != null)
{
toolTip.IsOpen = true;
}
else
toolTip.IsOpen = false;
That does the trick for me.

Related

Bring a control to front

I have a user control that uses a textbox and a list box. List box isn't visible, it only becomes visible when user starts typing or click in text box.
I have added the user control to a group box which is on the form.
Now when the listox becomes visible, it stays inside the group box, and can't see the full height. I wan't it float on top so that i can see the full height.
I have looked around, implemented some solutions but nothing worked for me.
Constructor for the user control
namespace YarCustomControl
{
public partial class YarCustom : TextBox
{
public YarCustom()
{
InitializeComponent();
_code = "";
_id = -1;
//list box handling
listBox = new ListBox();
listBox.Visible = false;
listBox.Font = this.Font;
listBox.Location = this.Location;
listBox.BorderStyle = BorderStyle.Fixed3D;
listBox.Resize += new EventHandler(listBox_Resize);
//listBox.SelectedValueChanged += new EventHandler(listBox_SelectedValueChanged);
listBox.KeyDown += new KeyEventHandler(listBox_KeyDown);
listBox.Click += new EventHandler(listBox_Click);
//test => no affect on listbox
this.Controls.Add(listBox);
listBox.Visible = false;
}
}
}
and the following method makes the listbox visible. Both SetchildIndex (commented and not commented) throw an error
private void makeListBoxVisible()
{
Form parentForm = (this.FindForm() as Form);
//parentForm.Controls.SetChildIndex(listBox, 0);
this.Controls.SetChildIndex(listBox, 0);
listBox.Visible = true;
listBox.BringToFront();
}
What is the best approach for handling something like this?
My environment is VS2010 and WinForms.
Now when the listox becomes visible, it stays inside the group box,
and can't see the full height. I wan't it float on top so that i can
see the full height.
Simply put it directly on the Form.

FlowLayoutPanel AutoSize only in vertical?

I'm loading images dynamically inside a FlowLayoutPanel. I need for this panel to auto-size but only vertically.
Is this possible, and if so, how do I go about achieving it?
Simple, add a event of type control added:
private void flowLayoutPanel1_ControlAdded(object sender, ControlEventArgs e)
{
if (flowLayoutPanel1.Controls.Count % 10 == 0)
flowLayoutPanel1.SetFlowBreak(e.Control as Control, true);
}
set AutoSize = true
set flowdirection = LeftToRight
Maybe
FlowLayoutPanel1.WrapContents = False;
FlowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
will help you.
I did set the Size from panel dinamically. Example:
int newHeight= listImages.Count/10 * 100;
flowLayoutPanel1.Size = new Size(1143, newHeight);
It works for me. Thx all
This might look like an ugly solution, but it works for me:
Store current width of a panel in variable;
Set AutoSize mode to true;
Perform the action that require panel resize;
Restore previous panel's width from the variable.
int i = _panel1.Width;
_panel1.AutoSize = true;
_panel1.AutoSizeMode = AutoSizeMode.GrowOnly;
/*some action going on here*/
_panel1.AutoSize = false;
_panel1.Size = new Size(_panel1.Width, 80);

Why does auto Zoom/Scroll not work for my Chart?

to make it short I checked on the "WinFormsChartSamples" provided by Microsoft. What I wanted to know is how to enable zooming and scrolling for Chartcontrols. The example which is shown there is pretty short.
using System.Windows.Forms.DataVisualization.Charting;
...
// Set automatic zooming
chart1.ChartAreas["Default"].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas["Default"].AxisY.ScaleView.Zoomable = true;
// Set automatic scrolling
chart1.ChartAreas["Default"].CursorX.AutoScroll = true;
chart1.ChartAreas["Default"].CursorY.AutoScroll = true;
...
I tried this and nothing happened, no zooming and no scrolling. I tried two things:
In Form1.Designer.cs I added that information to the chart.
chartArea1.Name = "ChartArea1";
chartArea1.CursorX.AutoScroll = true;
chartArea1.CursorY.AutoScroll = true;
chartArea1.AxisX.ScaleView.Zoomable = true;
chartArea1.AxisY.ScaleView.Zoomable = true;
this.chart1.ChartAreas.Add(chartArea1);
this.chart1.Cursor = System.Windows.Forms.Cursors.Cross;
legend1.Name = "Legend1";
this.chart1.Legends.Add(legend1);
this.chart1.Location = new System.Drawing.Point(297, 62);
this.chart1.Name = "chart1";
series1.ChartArea = "ChartArea1";
series1.Legend = "Legend1";
series1.Name = "Series1";
this.chart1.Series.Add(series1);
this.chart1.Size = new System.Drawing.Size(963, 668);
this.chart1.TabIndex = 6;
this.chart1.Text = "chart1";
I tried to add it directly into the constructor in Form1.cs.
Perhaps it is important to mention that I am using OpenFileDialog in order to add data to the series:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream fileStream = null;
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open File..";
//First the description of the file separated by "|"
fDialog.Filter = "((ASC files)| *.asc";
fDialog.InitialDirectory = #"C:\";
//Show Messagebox if the file was loaded (Source: MSDN - FileDialog.FilterProperty)
if (fDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("The File was loaded successfully.");
try
{
if ((fileStream = fDialog.OpenFile()) != null)
{
using (fileStream)
{
//Insert code for reading the stream here.
Spectrum newSpectrum = new Spectrum(chart1.Series.Count, fDialog.FileName,
fDialog.SafeFileName, DataHandler.readSpectrumFromFile(fileStream));
addSpectrumToView(newSpectrum);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Any advice is welcome, thanks in advance,
BC++
I think you were actually really looking for this:
chart1.ChartAreas["Default"].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas["Default"].CursorY.IsUserSelectionEnabled = true;
used in conjunction with what you already have should work well, which should look like this:
// Set automatic zooming
chart1.ChartAreas["Default"].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas["Default"].AxisY.ScaleView.Zoomable = true;
// Set automatic scrolling
chart1.ChartAreas["Default"].CursorX.AutoScroll = true;
chart1.ChartAreas["Default"].CursorY.AutoScroll = true;
// Allow user selection for Zoom
chart1.ChartAreas["Default"].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas["Default"].CursorY.IsUserSelectionEnabled = true;
Have a look here: http://archive.msdn.microsoft.com/mschart There is an example there which does zooming/scrolling and much, much more! :)
To enable easy zooming, add a trackbar and use it to zoom:
private void trackBar1_Scroll(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.Size = trackBar1.Maximum - trackBar1.Value;
chart1.ChartAreas[1].AxisX.ScaleView.Size = trackBar1.Maximum - trackBar1.Value;
(etc for however many chart areas you have)
}
the "maximium - value" is to so that the higher the trackbar value, the fewer points are shown (closer zoom)
and make sure that in designer the 'chart1->ChartAreas->Axes->(whichever axes)->scaleview->zoomable' is set to true
A scroll bar will normally appear when a datapoint exceeds the scaleview size of an axis, if it has been set (scrolling doesn't really work reliably if left at 'auto'), if it hasn't, set it, if a scrollbar doesn't appear, a trackbar can yet again be used:
private void trackBar2_ValueChanged(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.Position = trackBar2.Value;
chart1.ChartAreas[1].AxisX.ScaleView.Position = trackBar2.Value;
(etc for however many chart areas you have)
}
Make sure you set the "Maximum" in the trackbars to a nice high number (eg 5000) and "Value" to what you desire it to load at.
Have yet to notice too much of a difference between "trackBar_Scroll" and "trackBar_ValueChanged", except "ValueChanged" works if the trackbar is moved by the program or user mouse click, whereas "Scoll" only works if moved by users mouse click.
Anything I missed?
My users dislikes the standard behavior of the mschart zooming and scrolling. That's why I implement a mouse based zoom/scroll that use dragging and mousewheel on axises.
The source code is here: https://bitbucket.org/controlbreak/mschartfriend
It is very simple and short, you can change it very quickly if you need.

highlighting the button that fired an event

Consider a SilverLight project that has 31 hyperlinkbuttons. Those represent the days of the month. I'm using this code to highlight the hyperlinkbutton that respresent today's day.
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
//Highlighting the day of the month
if (daynumberHyperButton != null)
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Gray);
}
Then if I click on this highlighted hyperlinkbutton, it will open a childwindow to write some report.
private void dayHyperLink_Click(object sender, RoutedEventArgs e)
{
//This will initite and show the report window
ReportWindow rapport = new ReportWindow();
rapport.Closed += new EventHandler(rapport_Closed);
rapport.Show();
}
When I close the childwindows by clicking the OK button, it changes the color of the hyperlinkbutton that was highlighted (todays day) because I'm using this code to do that:-
private void rapport_Closed(object sender, EventArgs e)
{
ReportWindow rapport = (ReportWindow)sender;
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
if (rapport.UsersValue == "Röd" && rapport.DialogResult==true)
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Red);
}
else if (rapport.UsersValue == "Gul")
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Yellow);
}
else
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Green);
}
}
But if I click on any other hyperlinkbutton that is not highlighted, it still only change the color of the highlighted hyperlinkbutton. I know this because my rapport_Closed event has:
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
How can I change the above code, which is part of my rapport_Closed event, so that it changes the color of the event firing (the one that opens the childwindow) hyperlinkbutton, no matter which hyperlinkbuttonis the one that fires the event?
Ok now i can say i've done it. Here is what i did if anyone have a similar problem.
In may Home.xaml.cs, i added a public property like this:-
public HyperlinkButton dayHyperLink { get; set; }
To the Click event i added this code:-
dayHyperLink = (HyperlinkButton)sender;
To the rapport_Closing event i changed the if statment to the code below:-
if (rapport.UsersValue == "Röd" && rapport.DialogResult == true)
{
dayHyperLink.Background = new SolidColorBrush(Colors.Red);
}
This made me feel happy ;)

AutoScrollPosition Always Returns (0,0) For SplitPanel Control

I am trying to syncronize the scrolling of two splitcontainers within a splitpanel control. I have the code below:
Point mPrevPan1Pos = new Point();
Point mPrevPan2Pos = new Point();
void PanelPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (splitContainer1.Panel1.AutoScrollPosition != mPrevPan1Pos)
{
splitContainer1.Panel2.AutoScrollPosition = new System.Drawing.Point(-splitContainer1.Panel1.AutoScrollPosition.X, -splitContainer1.Panel1.AutoScrollPosition.Y);
mPrevPan1Pos = splitContainer1.Panel1.AutoScrollPosition;
}
else if (splitContainer1.Panel2.AutoScrollPosition != mPrevPan2Pos)
{
splitContainer1.Panel1.AutoScrollPosition = new System.Drawing.Point(-splitContainer1.Panel2.AutoScrollPosition.X, -splitContainer1.Panel2.AutoScrollPosition.Y);
mPrevPan2Pos = splitContainer1.Panel2.AutoScrollPosition;
}
}
However the AutoScrollPosition is always (0,0). I have AutoScroll enabled for both split containers. Why is this? What can I do to get the scroll position?
It looks like you copied the code from this answer: Scroll 2 panels at the same time
Did you wire up the events:
this.splitContainer1.Panel1.Paint += new PaintEventHandler(PanelPaint);
this.splitContainer1.Panel2.Paint += new PaintEventHandler(PanelPaint);

Categories

Resources