Tooltip background alternation - c#

The single tooltip which shows different messages for different controls. Now the Problem is the background image is not fit/suitable to all messages. I supposed to call the draw event of the tooltip for custom size, Font etc.,
I able to successfully call the draw and Popup event of the tooltip for particular message but setting the generalized size for different messages(e.ToolTipText) is unknown to me.
public void tooltip_Popup(object sender, PopupEventArgs e)
{
e.ToolTipSize = new Size(100, 100);
}
Kindly let me know anybody have any idea about it.

you can set the size in the Popup event, like this:
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
e.ToolTipSize = new Size(200, 200);
}
result of my test is this, hope it's helpful to you.

I found the answer for my Problem. The below code of POPUP event will change the tooltipsize according to the text size.
public void toolTip_Popup(object sender, PopupEventArgs e)
{
using (Font f = new Font("Arial", 12f))
{
e.ToolTipSize = TextRenderer.MeasureText(
toolTips.GetToolTip(e.AssociatedControl), f);
}

Related

Elegant log window with text wrapping [duplicate]

Ehm, umm, this means some lines should be two-lined in size. My boss think this is more simple solution, than limit displayed text to fit width and don't like horizontal scroll bar >_<
lst.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
lst.MeasureItem += lst_MeasureItem;
lst.DrawItem += lst_DrawItem;
private void lst_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = (int)e.Graphics.MeasureString(lst.Items[e.Index].ToString(), lst.Font, lst.Width).Height;
}
private void lst_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(lst.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
}
private void lst_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = (int)e.Graphics.MeasureString(lst.Items[e.Index].ToString(), lst.Font, lst.Width).Height;
}
private void lst_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(lst.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
}
To get the right display member to show up when data binding, replace
lst.Items[e.Index].ToString()
with a casted version of the property. So if your binding source is class object Car it would look like
((Car)lst.Items[e.Index]).YourDisplayProperty
Then the above functions can appropriately measure the string and draw it. :)
Helpful link
Check out this answer. It overrides the template of the listbox with a textblock which wraps the text. Hope it's useful. To solve your problem I think you shold add : ScrollViewer.HorizontalScrollBarVisibility="Disabled" . Found it here
To make binding correct, be sure to add check "lst.Items.Count > 0" to lst_MeasureItem function. Here is my example:
if (lst.Items.Count > 0)
{
e.ItemHeight = (int)e.Graphics.MeasureString(lst.Items[e.Index].ToString(), lst.Font, lst.Width).Height;
}
Everything else seems to work nicely after that.

Datagridview shall disappear when clicking on the Form in the background

As described in the title, I have a Form with a Datagridview on the front. The datagridview is smaller than my form in the back and I want the Datagridview to disappear whenever I click anywhere else but the Datagridview.
My code looks like this:
this.dataGridView1.Leave += new System.EventHandler(this.focus);
and the Eventhandler is defined like this:
private void focus(object sender, EventArgs e)
{
if(dataGridView1.Focused == false)
{
dataGridView1.Visible = false;
}
}
My problem is that my Datagridview only disappears when a new event in my form is activated but not when I click for example in a textbox on my form.
Can anyone help me?
The Leave event will not raise if you click on Form, or a ToolStripButton, PictureBox or any other non-selectable control.
If you expect a behavior like a dropdown, you can host DataGridView in a ToolStripControlHost and show it using a ToolStripDropDown. This way when you click anywhere outside the `DataGridView, it will disappear. It will act like a dropdown menu. Also the grid can be larger than your form:
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Margin = new Padding(0);
var host = new ToolStripControlHost(this.dataGridView1);
this.dataGridView1.MinimumSize = new Size(200, 100);
host.Padding = new Padding(0);
var dropdown = new ToolStripDropDown();
dropdown.Padding = new Padding(0);
dropdown.Items.Add(host);
dropdown.Show(button1, 0,button1.Height);
}
Important Note: It's an example. It's better to pay attention to disposing of objects in a real world application. For example, use just a single ToolStripdropDown and dispose it when closing the form.
Change the event handler assigning to:
this.dataGridView1.Leave += new System.EventHandler(fokussiert);
Worked for me when focusing on a textbox
you want your dgv also to disapear when you click on your textbox? is that what you mean?
private void dataGridView1_Leave(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}

How can I write a live MetroTile custom control?

Using MetroFramework 1.3.5 and .NET Transitions, in Windows Forms, I have written the code for a live MetroTile. Here is the Timer Tick method used to update the tile:
private void updateTiles_timer_Tick(object sender, EventArgs e)
{
metroPanel1.BackColor = Color.Transparent;
Bitmap bm = new Bitmap(metroPanel1.Width, metroPanel1.Height);
metroTile_startStop.DrawToBitmap(bm, new Rectangle(metroTile_startStop.Location.X, metroTile_startStop.Location.Y, metroTile_startStop.Width, metroTile_startStop.Height));
metroPanel1.BackgroundImage = bm;
if (animationFlag)
{
metroTile_startStop.Text = Properties.Resources.startStopTile_alternateText;
animationFlag = false;
}
else
{
metroTile_startStop.Text = "Start";
animationFlag = true;
}
Transition.run(metroTile_startStop, "Top", metroTile_startStop.Height - 16, 4, new TransitionType_Linear(500));
}
private void metroPanel1_Click(object sender, EventArgs e)
{
metroTile_startStop.PerformClick();
}
The tile is placed inside a MetroPanel with the Size(tile.Width + 10, tile.Height + 8). metroPanel1_Click() is necessary if, during a transition, the user clicks the background bitmap.
I don't have sufficient experience and I need help in writing a custom control so I can easily reuse it in future projects. The control should have a property which sets the timer interval and the possibility to set the tile texts. If I have a working code, then I can extend it to set the tile image, customize animations etc.
Thank you!

Make a TabPage flash on event

I'm in the stages of making a program which will require a tabpage to flash when an event happens
I've googled around, and i came across this: Blink tab header on receiving event. This is similar, but uses WPF, and i'm using WinForms, and i'm not even sure that does what i want :L
I've also found this: C#: Flash Window in Taskbar via Win32 FlashWindowEx. This is what i want, but obvious for the whole form, and in the taskbar, not 'in form'
Anyone got any ideas?
I'm not saying this is the best way or even a great way to accomplish this, but it does work. I've used code similar to this when I needed to something similar.
The tabControl1 has two tabs and I blink tab 1 (the second tab).
For my example that I threw together, I set tabControl1's DrawMode property to "OwnerDrawFixed" and then a couple of buttons which start/stop the timer. The interval was something like 750ms but you could choose whatever, of course. On a timer1_Tick event, I swap out the current color and tell the tabControl1 to refresh itself. That'll make the DrawItem event get raised and then I either draw the rectangle the current color if it is tab page 1 or the backcolor if not. Then I draw the tabpage's text.
It works. Could use some tweaking for sure. Give it a whirl!
public partial class Form1 : Form
{
Color currentColor = Color.Green;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (currentColor == Color.Yellow)
currentColor = Color.Green;
else
currentColor = Color.Yellow;
tabControl1.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
if (timer1.Enabled && e.Index == 1)
{
e.Graphics.FillRectangle(new SolidBrush(currentColor), e.Bounds);
}
else
{
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), e.Bounds);
}
Rectangle paddedBounds = e.Bounds;
paddedBounds.Inflate(-2, -2);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, this.Font, SystemBrushes.HighlightText, paddedBounds);
}
}

DrawToBitmap on Panel is blank

So I've written a class that has a stores some test results info and then a control that displays that info to the user. I want to put a print function on this class to draw the control at a full page size and print it. However it always comes out blank. The code see the panel as a control because it could be some other type of value. I figure there must be something simple I'm missing.
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Size oldSize = printData.Size;
printData.Size = new System.Drawing.Size(e.MarginBounds.Width, e.MarginBounds.Height);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Size.Width, printData.Size.Height);
InvertZOrderOfControls(printData.Controls);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, printData.Size.Width, printData.Size.Height));
InvertZOrderOfControls(printData.Controls);
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
bitmap.Save(#"C:\Users\jdudley\Documents\File.bmp");
printData.Size = oldSize;
}
Following this advice of this thread inverted the Z-Order of the controls but it didn't change anything.
The save call was added for debugging. It looks like it's actually rendering the background color of the panel without any of the controls.
Edit: This is in the context of printing but I have not issue with printing what so ever. My error is in creating the bitmap. The save line I added proves this because it creates a blank bitmap file.
Change your entire event to this
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Width, printData.Height);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(new Point(0, 0), printData.Size));
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
}
Edit
This is my whole Project. I created a panel named printData and I added two buttons, I attached an event to button1.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
PrintDocument printDocument = new PrintDocument();
public Form1()
{
InitializeComponent();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
}
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Width, printData.Height);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(new Point(0, 0), printData.Size));
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
}
private void button1_Click(object sender, EventArgs e)
{
pd.Print();
}
}
}
You have to try this and see if it works, or else I wont be able to sleep tonight!!

Categories