i'm new to C# i've been messing around to discover this language so far i've wrote many programs in my quest but now i'm stuck with one thing, i can't explain by words but codes can say what i want so here we go i know it's silly program but it's for education purpose only :D
Private void change()
{
anycontrol.BackColor = Color.Gold; // when this function called the control's BackColor will Change to gold
}
// example
private void TextBox1_Focused(object sender, EventArgs e)
{
Change(); // this suppose to change the color of the controls which is now textbox1 i want it to work on other controls such as buttons progressbars etc
}
now after i explained my problem i may ask you if you can to help it will be appreciated.
You can create a method that takes a Control and a Color as a parameter, and anything that inherits from Control (i.e. TextBox, DropDownList, Label etc.) will work with this:
void SetControlBackgroundColour(Control control, Color colour)
{
if (control != null)
{
control.BackColor = colour;
}
}
In your example, you could use it like this:
private void TextBox1_Focused(object sender, EventArgs e)
{
SetControlBackgroundColour(sender as Control, Color.Gold);
}
In response to the comments, you could then use this method in a recursive method that will set the background colour for each control on the form:
void SetControlBackgroundColourRecursive(Control parentControl, Color colour)
{
if (parentControl != null)
{
foreach (Control childControl in parentControl.Controls)
{
SetControlBackgroundColour(childControl, colour);
SetControlBackgroundColourRecursive(childControl);
}
}
}
And then call this function on your Form object (this) in your Form1_Load method (assuming the form is called Form1):
protected void Form1_Load(object sender, EventArgs e)
{
SetControlBackgroundColourRecursive(this, Color.Gold);
}
Related
I have a main form with 1 panel,
the panel has 10 user controls and
every user control has on picture box.
Main form:
private void Form1_Load(object sender, EventArgs e)
{
Picturebox1.Image=....
for(int i=0;i<10;i++)
{
uscontrol a=new uscontrol()
{
usimage=Image.Fromfile....
};
panel1.Controls.Add(a);
}
}
User control:
public Image usimage
{
get { return imagebox.Image; }
set { imagebox.Image = value; }
}
How can I do that when I click on one of the user controls, it passes image of that user control to main form and shows it in Picturebox1,
thank you.
Firstly, you will need to add MouseClickEvent to every uscontrol. To achieve that, edit your Form1_Load to this.
Picturebox1.Image=....
for(int i=0;i<10;i++)
{
uscontrol a=new uscontrol()
{
usimage=Image.Fromfile....
};
a.MouseClick += new MouseEventHandler(USControl_Clicked); //Note this added line
panel1.Controls.Add(a);
}
And then you will need to add USControl_Clicked to your From1 code.
private void USControl_Clicked(object sender, MouseEventArgs e)
{
//Here we cast sender to your uscontrol class and access usimage
Picturebox1.Image = (sender as uscontrol).usimage;
}
Similar questions have been already asked (e.g., here), however I've not found an answer for my specific case. I'm building a custom control based on a DevExpress control, which in turns is based on standard TextBox and I've a flickering problem that seems due to the base TextBox component, which tries to update selection.
Without explaining all the details of my custom control, to reproduce the problem you just need to place a TextBox inside a Form and then use this code:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
textBox1.MouseMove += TextBox1_MouseMove;
}
private void TextBox1_MouseMove(object sender, MouseEventArgs e) {
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
}
}
If you launch it, you click on the TextBox and then you move the cursor toward right you will notice the flickering problem (see video here). For my custom control I would need to avoid this flickering. I'm bound to use a TextBox (so no RichTextBox). Any idea?
The solution has been provided in the meantime by Reza Aghaei by overriding WndProc and intercepting WM_SETFOCUS messages. See here
Depending on what u want to do there are several solutions:
If you want to prevent the selection it would be:
private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
(sender as TextBox).SelectionLength = 0;
}
Or for selecting all:
private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
(sender as TextBox).SelectAll();
}
And besides that u also could specify the conditions for selecting, for example:
private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
if (MouseButtons == MouseButtons.Left) (sender as TextBox).SelectAll();
else (sender as TextBox).SelectionLength = 0;
}
But as long as you want to select the text you always will get some flickering, because a normal Textbox has not the possibility to use things like BeginEdit and EndEdit and so it will change the text first and then select it.
Looking at the video it looks like your textbox is calling WM_ERASEBKGND unnecessarily. In order to remedy this problem you can subclass the textbox class and intercept these messages. Below is sample code which should do the trick (untested) Disclaimer: I have used this technique for other WinForm controls that had the type of flicker shown in your video but not TextBox. If it does work for you, please let me know. Good luck!
// textbox no flicker
public partial class TexttBoxNF : TextBox
{
public TexttBoxNF()
{
}
public TexttBoxNF(IContainer container)
{
container.Add(this);
InitializeComponent();
//Activate double buffering
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
//Enable the OnNotifyMessage event so we get a chance to filter out
// Windows messages before they get to the form's WndProc
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
//http://stackoverflow.com/questions/442817/c-sharp-flickering-listview-on-update
protected override void OnNotifyMessage(Message m)
{
//Filter out the WM_ERASEBKGND message
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}
I am looking to simulate a custom tooltip the like of you see in websites using c# .NET 4.5 windows forms.This tooltip will basically show status of some Tasks like how many tasks are pending,tasks in process, completed etc.To do this i am using a borderless win form.This winform will have some texts, images etc.I want it to reveal itself on button's mouseHover event and disappear on MouseLeave event.My problem is that on Mousehover event numerous instances of that tooltip form is getting generated and on MouseLeave they are not getting closed.My code is
private void B_MouseHover(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Close();
}
My code is not working, hence please tell me how to do this the correct way.Thanks
You're generating a new instance of the form class every time you get a hover event, and every time you get a leave event. If you want to continue to use this approach I would recommend you use a variable on your main form object to store the reference to your tooltip form. Secondly, you need to not generate a new instance whenever the event handler is called, but only when necessary. I would create your instance the first time your Hover event is called for a particular control, and then dispose of it when your Leave handler is called -- this is under the assumption that the tooltip dialog's constructor loads up different information for each control being hovered over. Like so:
frmSecQStatToolTipDlg f_tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
f_tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(f_tooltip != null)
{
f_tooltip.Close();
f_tooltip = null;
}
}
You should keep a global field for this form, and should not dispose or close it. Just hide it on some events and show again.
Sample Code:
frmSecQStatToolTipDlg tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg != null)
{
tooltip.Hide();
}
}
With this logic you'll not have to create tooltip instance again and again and it will not take time to popup if you frequently do this activity.
Declare your tooltip once as readonly and use it without asking anytime if it is null or not.
If you need to Dispose it, implement the IDisposable pattern:
https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
private readonly frmSecQStatToolTipDlg _tooltip = new frmSecQStatToolTipDlg() ;
private void B_MouseHover(object sender, EventArgs e)
{
_tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
_tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
_tooltip.Hide();
}
now i have the current code o MainUC.cs:
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
The thing i want to do is clean the code from main page/form. I have 2 taskbar and 2 toolbar buttons that are calling the same form (this one above), so i don't want to write the code 4 times. I tried to make new class.cs with properties and do it with return value, but it didn't work. Can someone help me with it, or, is there possiblity to call the same code on current page/form. Something like
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
call tsbNoviRacun();
}
"( this isn't working, i know :p)
EDiT: Oh damn me, thanks guys!
In c# there is no "call" keyword for invoking functions. You just type the name and all required arguments in round brackets.
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
tsbNoviRacun_Click(sender, e);
}
This should do it:
public void tsbNoviRacun()
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
}
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
tsbNoviRacun();
}
You can call that method from all the event handlers you want it to run on. Obviously this function is depended on Controls and DockStyle so you must put it within scope of this.
I have a form with controls and associated tooltips. I'm implementing a help-button which should show all the tooltips at once.
I would like to implement it somehow like this:
private void btnHelp_Click(object sender, EventArgs e)
{
System.Windows.Forms.Control.All.Show.Their.Tooltips();
}
Can't find a simple way to do it :-)
I was thinking of using ToolTip.Show() but it requires not only the control, but also the tooltip text - but I don't want to write it again (since necessary tooltips are already assigned in controls' properties).
UPD.
I started implementing it with this function:
public void ShowControlsTooltip(System.Windows.Forms.Control c)
{
ttsToolTips.Show(ttsToolTips.GetToolTip(c), c, c.Location.X, c.Location.Y);
}
But I can't make it show multiple tooltips at the same time.
UPD2.
Now I have this kind of code, but all tooltips still blink and disappear.
public void ShowControlsTooltip(System.Windows.Forms.Control c)
{
ToolTip t = new ToolTip();
//t = ttsToolTips;
t.Show(ttsToolTips.GetToolTip(c), c, c.Location.X, c.Location.Y, 1000);
}
private void btnHelp_Click(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
try
{
ShowControlsTooltip(c);
}
catch
{
}
}
}
The thing with ttsToolTips is that I have all the tooltips associations there already.
UPD3. it looks like works. But coordinates are not exact.
The big question is now - how to remove all these tooltips at once?
foreach(Control c in Form.Controls)
{
string s = Tooltip.GetTooltip(c);
c.ShowTooltip(s,this);
}