Setting default position for new toolwindow in VsPackage - c#

I'm creating a VsPackage with a ToolWindow and having an issue setting the default position of the window when it is opened for the first time. I need it docked as a new tab in the 'main window' (not exactly sure what this is called - it is the center area of the IDE where the Code Editor window opens by default.), but instead, it is opening as a floating window on in the top-left.
I understand I can move the window to the right position and it will be saved for the next time in my settings, but I would like it defaulted to this position so the users don't have to do this.
[ProvideToolWindow(typeof(MyToolWindow),
Style = Microsoft.VisualStudio.Shell.VsDockStyle.Linked,
Window = "GUID Here")]
I know I have to set a specific guid, but I can't seem to find the right one - this list doesn't seem to list what I need: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.interop.toolwindowguids_fields(v=vs.140).aspx

You should use Style = VsDockStyle.MDI.

Related

How to control the space between two labels during run time

Im using Visual Studio 2015 Community C#.
I have two labels on a Windows form suppose Label1 and Label2.
These labels will get filled up with user input namely first name and last name.
How to put even space between them so that during runtime the first name doesn't over lap the last name.
AbrahLincoln Abraham Lincoln
(Label1^)(^Label2) (^Label1) (^Label2)
For example: how to make this ^ INTO that >>>>>>>>>>>>^^
Because if I put space in the Form Design before runtime then for other names It will come like this: John(unnecessary space)Doe
Hope you have understood my problem.
Thanks for your time. :D
Controls are located in a form based on coordinates. Luckily for you these controls have properties that tell you the coordinate for the top, left, right, bottom of a control. So you could dynamically move the right label after setting the text.
Label2.Point = new Point(Label1.Right + 5, Y-coord);
An easier way would be to play about with the labels properties in the designer.
You could also try to anchor label1 to the right and label2 to the left. That way you should have a clean middle line, and as the text grows larger it pushes outwards on does not overlap inwards over each other.
However you need an object to anchor to and luckily the SplitContainer works excellent for this.
Also consider setting the autosize property to off and maxing the widths of the labels. Large enough for the string you expect.
Have you considered making it one label?
As in
theOnlyLabel.Text = $"{dataObject.FirstName} {dataObject.LastName}";
or, if you're using textboxes, something like
theOnlyLabel.Text = $"{txtFirstName.Text} {txtLastName.Text}";
Otherwise, I'm afraid, you'd have to realign these two labels every time your first or last name changes.

Positioning a StatusStrip on the bottom of a window in Windows Forms

I'm trying to keep a status bar flush up against the bottom left corner of a realizable window without overflowing. I've got it to stay in place by having a resize function that updates the location and size of the status strip, but the bottom and the right side of it are always extending past the window. Here's how I'm calculating where it should go.
statusBar.Location = new System.Drawing.Point(0, Form.Size.Height - 22);
statusBar.Size = new System.Drawing.Size(Form.Size.Width, 22);
Where 22 is the constant height I want the statusBar to be. I know there has to be some other variable I'm not taking into account in setting this that's stored in the form, but I'm not sure how to access it, or what it even is.
What am I doing wrong? And is there any other easier way to keep the statusstrip on the bottom of the window regardless of resize events?
Set the Dock property to Bottom
You have to use ClientSize instead of Size.
The following:
textBox1.AppendText(Size.ToString() + "\r\n");
textBox1.AppendText(ClientSize.ToString() + "\r\n");
yields:
{Width=300, Height=300}
{Width=284, Height=262}
Though, of course, it's easiest to just use Boo's answer.

How to show SplashScreen on the second monitor?

How to show SplashScreen on the second monitor? Now it shows only on primary monitor, but application starts on the secondary monitor.
There are no properties to control where the standard WPF splash screen shows up.
If you need to change the default behavior you need to do your own implementation. An example is available here
You can use the 'Screen' class with the WindowStartupLocation property of your splash screen (I assume you are using a Window for this).
Like:
Window someWindow = new SomeWindow();
someWindow.WindowStartupLocation = WindowStartupLocation.Manual;
someWindow.Left = Screen.AllScreens[1].Bounds.Left;
someWindow.Top = Screen.AllScreens[1].Bounds.Top;
If needed you can center it yourself ofcourse. Take the screen height and the window height and calculate the center.
Check out the System.Windows.Forms.Screen class.
You can use Screen.GetWorkingArea() to get the current display, and then set the location of the splash screen based on that. Screen.FromControl() and Screen.AllScreens may also be useful.

Getting a text area in the system tray

i was just wondering what i need to research to be able to have a programme that is in the system tray, when the user clicks the programme icon, just above the system tray a small text area appears allowing the user to type in a search condition. There is plenty of resources for c# and getting your programme in the system tray, but then it just opens as normal, which is not quite what i am looking for.
Thanks
One way to accomplish this is to use a standard WinForms window which contains a single text box and has no border. This window can then be displayed and positioned as normal (likely using many of the existing samples) but will appear as a floating text box.
var form = new MyTextBoxForm();
form.FormBorderStyle = BorderStyle.None;
form.StartPosition = FormStartPosition.Manual;
// position the form
form.ShowDialog();
Handle the NotifyIcon.Click event and show your form in the desired location.
For example:
var screen = Screen.PrimaryScreen;
form.Left = screen.WorkingArea.Right - form.Width;
form.Top = screen.WorkingArea.Bottom - form.Height;
Maybe with this Make your program in the system + add a menu you could try editing the menu, like you'd do a regular menu with toolstrips.... and change the label by a textbox.
Just a random idea.

How to dock a windows form in C#?

I just would like to know if it is possible to dock a windows form on top of the user screen? I have been trying to do this by manually setting the position of my form to the coordinates I want. But using this method, however, allows the user to change the position of the form just by dragging it. I want to make the form docked to the upper portion of the screen since this window form will server as a menu for the project I am making.
Thanks a lot. :)
I would consider using the Control.Dock property along with one of the DockStyle enumeration values.
You might need to play with the Layout too, so that you may layout your form's controls differently depending on the DockStyle selected.
You will need, in my point of view, to consider the Control.Location property so that you get to know which DockStyle value to dock your form with.
EDIT #1
Your Windows Form has a Dock property as it inherits from Control.
Let's consider the following :
Each time your form comes closer to your right-side of the screen, for example, or of the MDI container, you want to dock right, right ? (Little word play here... =P) So, you have to subscribe to the Control.LocationChanged event.
private void myForm_LocationChanged(object sender, EventArgs e) {
if (this.Location.X > 900) then
this.Dock = DockStyle.Right;
else if (this.Location.X < 150) then
this.Dock = DockStyle.Left;
else if (this.Location.Y > 600) then
this.Dock = DockStyle.Bottom;
else if (this.Location.Y < 150) then
this.Dock = DockStyle.Top;
else
this.Dock = DockStyle.None;
}
Indeed, instead of constant values, you should use the current desktop resolution and calculate a ratio from it where you want your docking to occur.
***Disclaimer:****This code is provided as-is and has not been tested. This algorithm is hopefully enough to guide you through the docking process as you need it. Further assistance may be brought upon request.* =)
It seems the Form.DesktopLocation property is the righter tool for the job as for your main window, meaning your MDI container, for instance. As for the other windows, I would go along with something that looks like the code sample provided.
Does this help?
EDIT #2
If you want to prevent Form's overlapping, perhaps the Control.BringToFront() method could do it before or after your call to the Control.Show() method, depending on what works best for you.
So after some tweaks I finally was able to get this code working.
this.DesktopLocation = new Point((Screen.PrimaryScreen.Bounds.Width / 2 - 420), 0);
I placed that line below the InitializeComponent() and it docks my form to the center of the screen with whatever resolution values.
By setting the FormBorderStyle of your form to None, you take the drag handle away from the user so they cannot move it via the mouse.
Then you just need to place it where you want.
If you really want to take away the users options you can also set the ShowInTaskbar property to false

Categories