I have a Button in a SplitContainer, on panel2.
I have created dynamically a ContextMenuStrip which I have attached to this Button. I want to positioning the context menu under the button like in this image
But what I got is not like that.
This is what I tried:
private void SelectContentGroup_Click(object sender, EventArgs e) {
ContextMenuStrip x = selectContentGroup.ContextMenuStrip;
if (x is null) return;
// this will show contextmenu near the mouse arrow
//x.Show(Control.MousePosition);
// I have tried to get MousePosition and to compare to my button location and Y is a lot of difference, about 200 pixels
//Console.WriteLine("MousePosition: {0}, ButtonLocation: {1}", Control.MousePosition, PointToScreen(selectContentGroup.Location));
x.Show(PointToScreen(selectContentGroup.Location));
// I tried with e.Location also, but none of those points will give the button Left-Bottom position for contextmenu
}
Use the method that includes the control, and adjust by the height of that control to have the menu show below it:
x.Show(SelectContentGroup, new Point(0, SelectContentGroup.Height));
I'm assuming SelectContentGroup is the name of the button.
Related
First off, the title might not make much sense. Suggestions for changing it are appreciated.
I am clicking into a TextBox that is inside a ScrollViewer. When that happens, the ScrollViewer will shrink in height (from the bottom up), it doesn't scroll at all, and some controls near the bottom of the viewport get covered up (cause the viewport is now smaller). If the TextBox gets covered up, I need to scroll such that it is still visible.
I have checked several SO questions, and none seem to capture my problem. This one is close, but I don't have a canvas to work with. Also, given my specific scenario, I cannot use Dispatcher to wait for the UI to load, and then use BringIntoView().
The TextBox's share an event, TextBox_GotFocus,
TextBox_GotFocus(object sender, RoutedEventArgs e)
{
myScrollViewer.Height = 400; //used to be 600
//if sender was in the 401-600 range, bring it into view
}
How do I scroll the ScrollViewer only if the entered TextBox is now hidden after the height change?
No thanks to the random downvote, but I managed to figure a roundabout way to do this.
TextBox_GotFocus(object sender, RoutedEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
//Get the distance from the top and bottom of the ScrollViewer
double offsetTop = element.TranslatePoint(new Point(), myScrollViewer).Y;
double offsetBottom = myScrollViewer.Height - offsetTop;
//Get total height needed to show the whole element
double height = 200 + element.Height;
//If the control would be hidden...
if (offsetBottom < height)
{
//Scroll down the difference
double change = myScrollViewer.VerticalOffset + (height - offsetBottom);
myScrollViewer.ScrollToVerticalOffset(change);
}
myScrollViewer.Height = 400; //used to be 600
}
I have this idea of this world map image and when I click a country on it, it shows information of that country in a MessageBox for example does anyone have an idea how to do that?
I have a rectangle and a button and when i click the button it shows the image in the rectangle but i thought if i use polygons to shape the country's but I'm a little stuck.
I would like to have every country apart and maybe that the borders light up when clicked
You can do this pretty easily using WPF:
Find a nice World Map in SVG format. I used this one from Wikipedia:
Download and install Inkscape then open the SVG you've just downloaded. Inkscape has a nice feature that makes it possible to save an SVG as a XAML.
Import the ViewBox from the XAML file into your WPF window/etc:
For each Path in the XAML you can add a MouseEnter/MouseLeave event handler or you can use the same one for all the Paths
Sample code:
private void CountryMouseEnter(object sender, MouseEventArgs e)
{
var path = sender as Path;
if (path != null)
{
path.Fill = new SolidColorBrush(Colors.Aqua);
}
}
private void Country_MouseLeave(object sender, MouseEventArgs e)
{
var path = sender as Path;
if (path != null)
{
path.Fill = new SolidColorBrush(Colors.Black);
}
}
Now it's just a matter of changing colors/showing MessageBoxes etc.
GitHub
My first thought: You could bind a command to the view that will be triggered by a click on a position. If you're using WPF you can bind command parameters to the command to get the x and y of your click...
After that you have to handle the content of your messagebox and the highlighting of the borders depending on the position xy.
have fun :D
Option 1
There is a project on Code Project that someone created that defines hotspots that are clickable with events. You could use that to overlay your map and define the hotspots where you need them.
C# Windows Forms ImageMap Control
Option 2
You could bind to the MouseUp Event on the Image and use the following to see if it is in a Rectangle
private void mapMouseUp(object sender, MouseEventArgs e) {
Rectangle rect1 = new Rectangle(0, 0, 100, 100);
Rectangle rect2 = new Rectangle(0, 100, 100, 100);
if (rect1.Contains(e.Location)) {
// Do your stuff
} else if (rect2.Contains(e.Location)) {
// Do your stuff
}
}
I have created 4 buttons dynamically and placed them horizontally using c# win forms.Now i want show a custom tooltip(actually its a borderless form) under each of the 4 buttons on mouse hover event.But how do i position my tooltip form under the buttons??
I have tried the code below but it does not work the desired way.
tooltip.Location = new System.Drawing.Point(b.Left, b.Top);
Where 'tooltip' is tooltip form object & 'b' is the dynamic button.Please advise with some code snippet.
private void B_MouseHover(object sender, EventArgs e)
{
var b = sender as Button;
//MessageBox.Show(Cursor.Position.ToString());
if(b!= null)
{
if (tooltip == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = new System.Drawing.Point(b.Left, b.Bottom);
tooltip.data(b.Tag.ToString());
tooltip.Show();
}
}
The way you named it is a bit misleading. As I understand, what you call a tooltip is just a Form. You need to consider 2 things
(1) Form.StartPosition must be set to FormStartPosition.Manual
(2) Form.Location must be in screen coordinates. Note that the Button.Location you are trying to use is in button's parent client coordinates. Control.PointToScreen has to be used for conversion.
In your case, it should be something like this
tooltip.StartPosition = FormStartPosition.Manual;
var topLeft = b.PointToScreen(new Point(0, 0));
tooltip.Location = new Point(topLeft.X, topLeft.Y + b.Height);
When you show the tooltip you can control its location, check show method overloads: https://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.show.aspx
I have created a little program in WPF where I click on a button and a popup text box arrive. I would like to make this movable - drag and drop.
In the code I have created an object for a textbox named x, and used the command x.AllowDrop = true;, but without success.
I have tried MSN, Youtube and other sources, but without success.
private void button1_Click_1(object sender, RoutedEventArgs e) {
TextBox x = new TextBox();
x.Name = "new_textboxqq";
x.TextWrapping = TextWrapping.Wrap;
x.Text = "asfsadfasfsadfasff";
x.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
x.Background = Brushes.Yellow;
x.AcceptsReturn = true
x.Margin = new Thickness(5, 10, 0, 0);
x.AllowDrop = true;
HouseCanvas.Children.Add(x);
this.AllowDrop = true;
Canvas.SetLeft(x, 20);
Canvas.SetTop(x, 20);
}
Drag and Drop is a data transfer technique. From one control or files to another control or window.
If you need to move your control inside window, you need using mouse events: MouseDown, MouseUp, MouseMove. Look this.
You want to drag a TextBox and move it around on a Canvas, but AllowDrop property is for Drag-and-Drop operation. Drag-and-Move and Drag-and-Drop are different operations.
This is an example to do what you want.
The idea is handling the MouseMove event of the Canvas, calculating the position of the mouse cursor, and by setting the position of the TextBox to that position, you can make the TextBox move following the mouse cursor.
I have a system tray context menu with 26 items and an additional ToolStripTextBox menu item. When the user enters text to the filter textbox it continuously filters the menu items as the user types and hides categories on the fly by setting the ToolStripMenuItem Visible property to false.
It's working!
The issue is that when it gets filtered, the height of the context menu gets shorter from the bottom towards the top. The origin point for the menu is the upper right corner, causing it to shrink upwards. Since it is a system tray related context menu, I expect it to shrink downwards (bottom gravity).
How to make this happen?
Still not sure if there is a "proper" built-in method to do this...
In the mean-time, here's a hack that changes the Bounds() of the ContextMenuStrip whenever the size changes. It simply shifts the ContextMenuStrip down/up by however much the height changed. I've wired up the Opened() and SizeChanged() events of my ContextMenuStrip and store the last Bounds() in the "lastBounds" variable at class level:
private Rectangle lastBounds;
private void contextMenuStrip1_Opened(object sender, EventArgs e)
{
lastBounds = contextMenuStrip1.Bounds;
}
private void contextMenuStrip1_SizeChanged(object sender, EventArgs e)
{
Rectangle rc = contextMenuStrip1.Bounds;
int diff = lastBounds.Height - rc.Height;
if (diff > 0)
{
contextMenuStrip1.Bounds = new Rectangle(new Point(rc.X, rc.Y + diff), rc.Size);
lastBounds = contextMenuStrip1.Bounds;
}
else
{
contextMenuStrip1.Bounds = new Rectangle(new Point(rc.X, rc.Y - diff), rc.Size);
lastBounds = contextMenuStrip1.Bounds;
}
}