How to change TabControl's width inside UserControl? - c#

I have created a windows form with two panels with width 25:75 ratio. That is 25 ratio is the sidemenu and the other panel is Main menu/screen of app.So i used usercontrol and dragged & dropped it on the Main screen of form when user clicks the respective button on the sidemenu then the usercontrol is seen.
Also the side menu has a partial hide feature the is a three lined button on top right of sidemenu panel when user clicks that button the menu partially hides (basically decreasing width of sidemenu and increasing the with of main menu).
The problem is usercontrol has a tabcontrol in it when user clicks hide button on sidemenu the mainmenu width increases but it leaves the width of tabcontrol as it is.
I want to also change the width of tabcontrol when user clicks hide button on sidemenu, i am not getting access to tabcontrols width which is in usercontrol in panel in form...
What I have tried:
I have tried to access the width of tab control as follows:
//inside hide button in panel1 of form1
private void hidemenu_click()
{
usercontrol1.tabcontrol1.width= mainmenu.width;
}
but there isn't any suggestion showing by intellisense.... simply showing error
error:
are you missing a using reference or an assembly reference?

Try removing usercontrol1 as when I drag&dropped a tabcontrol, I could acces the Width property of it.

This worked for me...!!!
Open the user control in the designer.
Select the tab control.
In the property grid, change the "Modifiers" to public.
Or, open the user control's code file, and add a public property to get and set the width of the tab control.
public int TabControlWidth
{
get { return tabcontrol1.Width; }
set { tabcontrol1.Width = value; }
}

Related

How can I resize a TreeView during runtime in a Control

I am trying to resize my TreeView window during runtime and I cant do it.
in my program I can press on a button and the TreeView opens as a popup window.
The TreeView is inside a Control:
private Control parent;
mytree= new TreeView();
parent.Controls.Add(mytree);
I have already tried to search for any resize properties but no luck, I can't find a way that the tree will be resizable during runtime.
The only way I can see it is to delete the control and to put it in a Form and then I can make it look like the same but I still want to know if there is another way to solve this, please if someone knows !
thank you
Assign anchor property to tree view so it resizes with form (or maybe control he is child of)
Here is example of anchoring button:
// Add a button to a form and set some of its common properties.
private void AddMyButton()
{
// Create a button and add it to the form.
Button button1 = new Button();
// Anchor the button to the bottom right corner of the form
button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
// Assign a background image.
button1.BackgroundImage = imageList1.Images[0];
// Specify the layout style of the background image. Tile is the default.
button1.BackgroundImageLayout = ImageLayout.Center;
// Make the button the same size as the image.
button1.Size = button1.BackgroundImage.Size;
// Set the button's TabIndex and TabStop properties.
button1.TabIndex = 1;
button1.TabStop = true;
// Add a delegate to handle the Click event.
button1.Click += new System.EventHandler(this.button1_Click);
// Add the button to the form.
this.Controls.Add(button1);
}
Important part is button1.Anchor where in example button is anchored to bottom right of window, so it will just follow window as you resize, but you can anchor to all sides of window and it will resize with it.
Try different things and you will figure it out.
SOURCE: MSDN

On windows forms, set tab focus to a button

I have created an Outlook add in which at some point displays a windows form with four buttons present on it. I am trying to default the focus to the first button, however the visual "selected" border will not appear around the button whenever I default this button as the focused one on start.
Any ideas how I could achieve this?
You can use either of these options to set the focus on a control in Load event of the form:
this.ActiveControl = this.button1;
this.button1.Select();
this.Show(); this.button1.Focus();.
You can use the Control.Focus method in the Load event of the form to set the focus on a control only after the Visible property of the form is set to true.
After selection the button, the border of the button will be drawn in a way that shows it's the active control, but the focus cues will not be drawn.
As a quick and dirty fix, you can send a Tab, and a Shift + Tab to your form:
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("+{TAB}");
If you are interested to change the standard behavior of Button to see focus cues when you select button in code or using mouse, you can create your own button inheriting Button and override its ShowFocusCues to return Focused value. You can read more about it here:
public class MyCustomButton : Button
{
protected override bool ShowFocusCues
{
get { return this.Focused; }
}
}

Remove button border on tab c# winforms

I have a button on my form that has flat style applied and uses a background image, I have removed all borders from the button, but when I tab onto the button from another control a black border appears around the button.
This can be seen in the image below. On the left is the button with black border on the right is a different button but shows how the cancel button should look.
I do not get this border, if I set the BoderSize to 0 in the FlatAppearance section.
Further investigation shows that this border appears only when the button is the default button. You can create your own button, which does never show this border like this
public class NoNotifyButton: System.Windows.Forms.Button
{
public override void NotifyDefault(bool value)
{
}
}
Note: NotifyDefault remains intentionally empty.
You have to make a new button class using IButtonControl and change NotifyDefault to false:
base.NotifyDefault(false);
You don't have to create a derived class.
You can set the ForeColor to be the same as parent control BackColor.
As follows :
btn1.ForeColor = btn1.Parent.BackColor;
You can do it setting the button property "ForeColor" to transparent
I managed to get around this by setting the button TabStop property to False and then using this code on the button click event
private void sendBackTab()
{
System.Windows.Forms.SendKeys.SendWait("+{TAB}");
}

How to detect MouseDown on any non-TextBox control?

TextBoxes and NumericUpDowns have the odd property of not allowing you to deselect them once they are selected. When my user selects a NumericUpDown and clicks else-where on the form, the NumericUpDown should be deselected.
Unfortunately, this is not the case. Currently I am just handling the MouseDown event of all other controls on the form (like the panels and actual form itself) and just calling the Focus method of a random label to remove the focus from the NumericUpDown. However, this cannot be applied to menu items or scrollbars.
There must be a better way to do this. The user may want to scroll the panel instead of the NumericUpDown and intuitively click the Panel and then use the scroll-wheel, but currently that would scroll the NumericUpDown instead, since it still has focus.
Thanks for reading.
Edit: Problem still unsolved.
Normally Panel Control is a Non-Focusable control. Therefore clicking on Panel will NOT remove focus from TextBox or NumericUpDown Countrol.
The workaround can be, place a button on panel and move it away from view for example setting its x = -100 and y = -100. Do NOT set visible = false.
Now whenever user clicks on Panel (Panel_Click event) set focus (Button.Focus()) to that button. In this way panel will be scrollable through scroll-wheel.
Enclose the numeric box within a panel of some sort and then do
panel1.MouseHover += new EventHandler(panel1_MouseHover);
private void panel1_MouseHover(object sender, EventArgs e)
{
if (numericUpDown1.Focused)
{
panel1.Focus();
}
}
I tested it and it works.!

WinForms context menu - not open in certain parts / detect underlying control

I have a .NET 2.0 Windows Forms application. On this app there is a Form control with a Menu bar and a status bar. Also there's a ListView on this form.
If I add a context menu to this form, the context menu will open when the user right clicks any part of the form, including the menu bar and the status bar.
How can I prevent the context menu from opening when the click happened on the menu bar / status bar? I want it to open only when clicking the "gray area" of the form.
If the click happened above a control on this form (for example, on the ListView), how can I identify this? I'd like to know if the user right clicked above the gray area or above the ListView, so I can enable/disable some menu items based on this.
After you've placed your Statusbar at the bottom and MenuStrip at the top,
Set ContextMenuStrip on your form to None
Place a standard Panel in the middle (between MenuStrip and StatusStrip) with the Dock property set to Fill.
Set the ContextMenuStrip property on your Panel (instead of on the form).
And place the ListView and all other controls that should go into the form in the Panel
Eg
~~~~~~~~~~~~~
menustrip
~~~~~~~~~~~~~
Panel. Dock=Fill. ContextMenuStrip=yourContextMenu.
~~~~~~~~~~~~~
StatusStrip
~~~~~~~~~~~~~
I found the answer:
Point clientPos = this.PointToClient(Form.MousePosition);
Control control = this.GetChildAtPoint(clientPos);
This should give the underlying control that was clicked on the Form, or null if the click was on the gray area. So we just need to test for the type of the control on the Opening event of the context menu. If it's MenuStrip, ToolStrip or StatusStrip, do e.Cancel = true;.

Categories

Resources