I have a uvSelfLoadingTextBox with multiple instances on a form.
I would like to load the tooltip with the _value property at run time.
I've tried
public ucSelfLoadingTextBox()
{
Windows.Forms.ToolTip myToolTip;
myToolTip.AutomaticDelay = 5000;
myToolTip.AutoPopDelay = 50000;
myToolTip.InitialDelay = 100;
myToolTip.ReshowDelay = 500;
myToolTip.SetToolTip(this, _value);
inside the control but that does not work.
I have tried using the tooltip that is dragged onto the form
ucSelfLoadingLogicTextBox uc = new ucSelfLoadingLogicTextBox();
toolTipA.SetToolTip(uc,uc._value );
and that does not work.
What is the correct way to do this?
You forgot to instantiate myToolTip. You need to set it to new Tooltip().
Also, I don't think it's a good practice to assign the tooltip in the textbox's constructor. You could do this in OnCreateControl() (that you need to override).
Your code could therefore become:
protected override void OnCreateControl()
{
base.OnCreateControl();
var myToolTip = new System.Windows.Forms.ToolTip
{
AutomaticDelay = 5000,
AutoPopDelay = 50000,
InitialDelay = 100,
ReshowDelay = 500
};
myToolTip.SetToolTip(this, this.Text);
}
Many visible controls on windows form have ToolTip property. Just set the Tooltip with your newly created one. You can also add tooltip to your form. Have you tried this?
myToolTip.ShowAlways = true;
And try to set this tip to a button control. This may be a good test for your tooltip.
Related
When programmatically adding controls to a tab control, I have been using the Form_Load event to create and embed things like datagridviews into my UI. I made a class that inherits from DataGridView
class DBDataGridView : DataGridView
{
public DBDataGridView()
{
DoubleBuffered = true;
AllowUserToAddRows = false;
AllowUserToDeleteRows = false;
AllowUserToResizeRows = false;
AllowUserToOrderColumns = false;
AllowUserToResizeColumns = false;
RowHeadersVisible = false;
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
ReadOnly = true;
Dock = DockStyle.Fill;
SelectionMode = DataGridViewSelectionMode.FullRowSelect;
TabStop = false;
}
}
And I call it later in the Form_Load event like so
private void MainDesignerForm_Load(object sender, EventArgs e)
{
DBDataGridView _DGV = new DBDataGridView();
var listOfOverlays = new List<OverlaySelectionList>()
{
new OverlaySelectionList { Description = "Description 1", PartNumber = "123-R1"},
new OverlaySelectionList { Description = "Description 2", PartNumber = "456-R1"}
};
var overlayList = new BindingList<OverlaySelectionList>(listOfOverlays);
_DGV.DataSource = overlayList;
Tab_Overlay.Controls.Add(_DGV);
_DGV.ClearSelection();
}
This gridview is on the THIRD tab of the TabControl, and everything works as expected except the ClearSelection(). No matter where I call it, it does not clear the initial row selection of the DGV. However, if I fire the same code block from a button ON the third tab, the formatting AND the ClearSelection() behave as expected.
What is causing this behavior?
Thanks to 41686d6564 and Jimi for the insight into the specifics on why this was happening.
Reiterating what they said in the comments: Assignment of properties appear to be cached regardless of whether the control they belong to is active or not (Hence why all the sizing and formatting properties were present at run time). However, actions that require a handle, like ClearSelection() require the control to be shown and active for the intended behavior to be observed.
Setting the selected tab to where the DataGridView before calling ClearSelection() was the solution (Or in my case, I had nested tabs, so I had to follow the tab tree to get to the specific tab that the DataGridView was on)
So now, part of the Load_Form logic is to check WHERE the control is located, make that tab active, THEN format and clear selections for each control that is being added. This allowed ClearSelection() to work as intended.
I have a simple form as an example. I've added some check boxes using designer, and set the margin and padding to 0,0,0,0. This is the form code:
public partial class Frm1 : Form
{
public Frm1()
{
InitializeComponent();
AddCheckBox();
AddCheckBox();
AddCheckBox();
}
public void AddCheckBox()
{
CheckBox cb = new CheckBox();
cb.Text = "AddedFromCode";
cb.Padding = new Padding(0,0,0,0);
cb.Margin = new Padding(0,0,0,0);
flowLayoutPanel1.Controls.Add(cb);
}
}
I would expect the check boxes that are added from designer and from code to appear the same, however, they do not.
How can I get the spacing of the AddedFromCode controls to have 0 spacing?
I don't know why it took me so long to figure this out. One of the properties that designer changes when you add a control to a FlowLayoutPanel is AutoSize = true.
So in code...
cb.AutoSize= true;
solves the problem.
so I figured I'm making just a stupid mistake here. In the first of what will be many controls, I need to either show a balloon tooltip when a bool is true or not show them when the bool is false. I know that ShowAlways is not what I need to modify and I've tried various solutions already. Does anyone spot the problem? The bool is set by a checked dropdown item in a Help Menu Strip Item.
It will open with the application with the correct display, but as soon as I check that option to show it, it always shows there after.
public void changeBalloonProperties(bool boolSet)
{
ToolTip helpDeskInfoButtonToolTip = new ToolTip();
if (boolSet)
{
helpDeskInfoButtonToolTip.ToolTipTitle = "HelpDesk Information Button";
helpDeskInfoButtonToolTip.UseFading = true;
helpDeskInfoButtonToolTip.UseAnimation = true;
helpDeskInfoButtonToolTip.IsBalloon = true;
helpDeskInfoButtonToolTip.ShowAlways = true;
helpDeskInfoButtonToolTip.AutoPopDelay = 5000;
helpDeskInfoButtonToolTip.InitialDelay = 1000;
helpDeskInfoButtonToolTip.ReshowDelay = 500;
helpDeskInfoButtonToolTip.SetToolTip(helpDeskButton, "Click to launch HelpDesk user info page in default browser.");
}
else
{
helpDeskInfoButtonToolTip.RemoveAll();
}
}
You are creating a new ToolTip instance each time the changeBalloonProperties is called so the code isn't removing the caption associated with the original ToolTip that was used with the helpDeskButton. Try moving the ToolTip declaration outside of your changeBalloonProperties method so the same ToolTip object is used with RemoveAll().
Also note you can use that same ToolTip object to add captions for multiple controls (as shown in the sample here) and it's probably better to set helpDeskInfoButtonToolTip.Active = false to disable them all at once instead of setting and removing the captions (and other properties) each time you toggle.
I am using a ToolStripDropDown control to implement the dropdown portion of a custom ComboBox-like control. In order to be visually appealing, I am imposing a MaximumSize on the dropdown and manually specifying the width of each ToolStripButton within it - the result is a popup which is the same width as the control that activates it, with a cap on the height of the height of the dropdown portion.
Example (simplified):
ToolStripDropDown dropDown = new ToolStripDropDown();
dropDown.MaximumSize = new Size(200, 100);
dropDown.RenderMode = ToolStripRenderMode.System;
dropDown.AutoSize = true;
for (int i = 0; i < 50; i++) {
ToolStripButton dropDownItem = (ToolStripButton)dropDown.Items.Add("Item " + i);
dropDownItem.AutoSize = false;
dropDownItem.Size = new Size(200, 20);
}
dropDown.Show(owningControl, new Point(0, owningControl.Height - 1));
As you can see, the constraints on the popup's size are applied, however the up/down scroll buttons are not displayed and there seems to be no way to make them appear. There do not appear to be any methods or properties within ToolStripDropDown regarding the scrolling offset or a mechanism to scroll a particular item into view (such as EnsureVisible() on ListViewItem).
How, then, can I get the dropdown to scroll? Any method would be sufficient, be it a scroll bar, scroll buttons or even the mouse-wheel.
(Incidentally, I have tried many times to make similar controls using a Form for the dropdown portion - despite trying dozens of solutions to prevent the popup from stealing focus or gaining focus when its controls are clicked, this seems to be a dead end. I have also ruled out using ToolStripControlHost, whose hosted control can still take focus away from the form that opened it.)
Finally cracked this one. It occurred to me that ContextMenuStrip and ToolStripDropDownMenu are capable of the auto-scrolling behaviour which their base class, ToolStripDropDown, cannot provide. Initially, I avoided these alternative controls because they usually add a wide margin. This can be removed via ShowImageMargin and ShowCheckMargin. Even after doing this, a small (approx 5px) margin remains. This can be removed by overriding the DefaultPadding property:
public class MyDropDown : ToolStripDropDownMenu {
protected override Padding DefaultPadding {
get { return Padding.Empty; }
}
public MyDropDown() {
ShowImageMargin = ShowCheckMargin = false;
RenderMode = ToolStripRenderMode.System;
MaximumSize = new Size(200, 150);
}
}
// adding items and calling Show() remains the same as in the question
This results in a popup window which can contain any type of ToolStrip item, enforces MaximumSize, has no margin and, most importantly, does not steal focus and cannot receive focus.
This is your nemesis:
internal virtual bool RequiresScrollButtons
{
get
{
return false;
}
set
{
}
}
It is internal, you cannot override it. You can revive your approach of using a Form by fixing the focus stealing behavior. Paste this into the form class:
protected override bool ShowWithoutActivation
{
get { return true; }
}
Guys, I have a windows form with a panel control and inside the panel control are several other controls with a System.Windows.Forms.Tooltip attached to them. How can I iterate through each tooltip and set the Active property of the tooltip to false? Tooltips, unlike other controls, are not actually controls. So I had this:
foreach (System.Windows.Forms.Control ctrl in this.pnlControl.Controls)
{
if (ctrl.Name.StartsWith("tt")) // since all my tooltip names start with 'tt'
{
System.Windows.Forms.ToolTip TipControl=(System.Windows.Forms.ToolTip)ctrl;
TipControl.Active=false;
}
}
This does not work though. It gets an error because the ToolTip control is not inherited from System.Windows.Forms.Control. Any ideas?
EDIT:
Okay Guys. I probably didn't go into enough detail to get the answer I needed. My problem is, I'm taking all the controls in my panel and moving them to a different panel. Once they are switched over, the tooltips are still attached to the controls, which is what I want. However I have no way to deactive or reactivate them once I move them since the form and the original panel no longer exist. However, I found a solution which I will post here.
How to add tool tips for two buttons? The correct way is NOT creating two instances of ToolTip in this way:
ToolTip tt1 = new ToolTip(); //or you can create one in the designer
tt1.ToolTipTitle = "test";
tt1.SetToolTip(button1, "caption1");
ToolTip tt2 = new ToolTip();
tt2.ToolTipTitle = "test2";
tt2.SetToolTip(button2, "caption2");
Remember that a ToolTip instance and a control are not one-on-one related. The right way for this example is:
ToolTip tt1 = new ToolTip(); //or you can create one in the designer
tt1.ToolTipTitle = "test";
tt1.SetToolTip(button1, "caption1");
tt1.SetToolTip(button2, "caption2");
To remove the tooltip of button2, use:
tt1.SetToolTip(button2,string.Empty);
For your case,we can use
foreach(Control c in this.Controls)
{
tt.SetToolTip(c,string.Empty);
}
Typically, you have a single ToolTip instance that handles the displaying of tool tips for all of your controls. That single ToolTip instance is just a regular member of your form. Simply set it's Active property to false.
Edit: OK, scrap my previous answer. Yes, ToolTip is a Component, not a Control, so it's not actually in the Panel at all. From your question, it sounds like you have one ToolTip instance and you use it for controls inside this Panel as well as for other controls, right? In that case the solution is simple: create a separate ToolTip instance and use that one for controls in the Panel, then just refer to it directly to deactivate it, eg.
ttPanel.Active = false;
Okay what I did was create a new class that is inherited from Control, like so:
public class TooltipMaster : System.Windows.Forms.Control
{
private System.Windows.Forms.ToolTip m_tooltip1;
private System.Windows.Forms.ToolTip m_tooltip2;
private System.Windows.Forms.ToolTip m_tooltip3;
private System.Windows.Forms.ToolTip m_tooltip4;
public System.Windows.Forms.ToolTip ToolTip1 {
get { return m_tooltip1; }
set { m_tooltip1 = value; }
}
public System.Windows.Forms.ToolTip ToolTip2 {
get { return m_tooltip2; }
set { m_tooltip2 = value; }
}
public System.Windows.Forms.ToolTip ToolTip3 {
get { return m_tooltip3; }
set { m_tooltip3 = value; }
}
public System.Windows.Forms.ToolTip ToolTip4 {
get { return m_tooltip4; }
set { m_tooltip4 = value; }
}
}
Then what I did was create an instance of this class inside my main form's Load event. Then I just assigned each of my 4 tooltips to the 4 tooltips in this class. Finally, I added this control to my panel. After doing all that, I could access the tooltips later by iterating through each control and looking for the TooltipMaster control.
Hope this makes sense!