I am having a treeview with some nodes. I am also having a panel. I have taken some usercontrol forms and i will load those usercontrols when corresponding node is selected from the child hood. Now what i need is have some validations like if i left the text box empty i will have some tooltips displayed to the user. Suppose if i click on first node i will have a user control loaded. With out giving any values if i hit ok i will have some tool tips as follows
Now if i select the second node from the tree still the tooltips getting displayed i would like to hide those
Any Help please
my code for rasing error tooltips is as shown below
public class TestClass
{
public void RequiredText(TextBox txtTemp, ToolTip newtoolTip)
{
if (txtTemp.Text != string.Empty)
{
txtTemp.BackColor = System.Drawing.Color.White;
newtoolTip.Hide(txtTemp);
}
else
{
txtTemp.BackColor = System.Drawing.Color.Tomato;
newtoolTip.Show("Required", txtTemp);
}
}
}
But this was done in the use control form.
I haven't yet mastered the art of reverse-engineering code from a screenshot. I'm guessing that you don't dispose the previous user control when you select a new one. Allowing the tool tip to stay visible. Use code like this:
private UserControl currentView;
public void SelectView(UserControl view) {
if (currentView == view) return;
if (currentView != null) currentView.Dispose();
if (view != null) this.Controls.Add(view);
currentView = view;
}
And call SelectView() from the TreeView's AfterSelect event handler.
Have you tried the Hide method?
http://dotnetperls.com/tooltip
Got the answer just written Usrcntrl_Leave event for every user control as
private void usrcntrlPPD_Leave(object sender, EventArgs e)
{
this.Dispose();
}
This solved my problem :)
private void timer1(object sender, EventArgs e)
{
count++;
if (count == 2)
{
toolTMensaje.SetToolTip(textBox1,"");
toolTMensaje.Hide(textBox1);
count = 0;
timer1.Stop();
}
}
Related
I need show droppeddown combobox after start program.
I need in dropdown style only, not simple style.
This is simple fragment of my program:
private void Form1_Shown(object sender, EventArgs e)
{
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
}
But I found the watch sign as cursor till I click on Form in any place.
I guessed that my Form have not fully active state and wait for something.
When I click Form (or combobox or any control) by LBM, it activated fully and all works fine.
Of course the combobox is dropup then, so I need click combobox twice.
Đ•ell me please what is correct initialization of such style combobox without "Cursor = Cursors.Default;"
You can simply wait until cursor is the default:
while (Cursor.Current != Cursors.Default)
{
Application.DoEvents();
}
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
Application.DoEvents simply process messages from the window queue, so you can process message until you get that cursor is the default. In that moment, you can drop down your control without problem.
If you prefer, create a extension method for the Form:
public static class FormExtends
{
public static void WaitToDefaultCursor(this Form form)
{
while (Cursor.Current != Cursors.Default)
{
Application.DoEvents();
}
}
}
And use it:
this.WaitToDefaultCursor();
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
NOTE: I use Cursor.Default but not to change the cursor. The form is processing messages and it's difficult to select a good moment to drop down the control.
I'm using WinForm to do my little CRM.
I got MainWindow form and on this form 3 panels ( 1 Top/ 1Left/ the other part is filled up by 7 Control User )
All the Control User are on top of each other, and when i click on some button( attached to the left panel) the called CU is brought to the front.
public void BtnContact_Click(object sender, EventArgs e)
{
contactControl1.Visible = true;
contactControl1.BringToFront();
panelBar.Height = BtnContact.Height;
panelBar.Top = BtnContact.Top;
employe1.Visible = false;
comptaControl1.Visible = false;
histoControl1.Visible = false;
alerteControl1.Visible = false;
voyageursControl1.Visible = false;
parametresControl1.Visible = false;
}
I don't want all the CU to load at the start of the App, but i want them to be launch when i click on the button on the left. And let say if i opened one and now opening a new one it close the one who was opened.
If i have no choice to open everything ( which i doubt ) how can i choose the one i want to open first or second etc ??
Thank you
I don't want all the CU to load at the start of the App
Use properties tab and make visible=false or use formload event to prevent all cu loading at the start.
private void Form1_Load(object sender, System.EventArgs e)
{
//use code to make anything visible or invisible
}
you can put all controls that should be shown or hidden on the panel.
This is called the other part is filled up by 7 Control User in the question, so just use Panel to store all those controls.
Then you can introduce new field on your form to store current control index that is shown and to write button click that will check the index and activate next control
int currentIndex = -1;
public void BtnContact_Click(object sender, EventArgs e)
{
currentIndex++;
if (currentIndex >= panelFor7Control.Controls.Count)
currentIndex = 0;
foreach (Control c in panelFor7Control.Controls)
c.Visible = false;
Control control2show = panelFor7Control.Controls[currentIndex] ;
control2show.Visible = true;
}
Button BtnContact has to be pressed to activate next control on the panel.
PS I suppose that you are trying to load data for the visible control only. You can use Control.VisibleChanged to check is data loaded and to load if control is visible and data was not loaded.
First you need to make instance of your UC, something like this:
public partial class YourUC : UserControl
{
public static YourUC _instance;
public static YourUC Instance
{
get
{
if (_instance == null)
_instance = new YourUC();
return _instance;
}
}
}
Then you need to call and show UC when button is clicked
private void btn_Click(object sender, EventArgs e)
{
if (!pnlControls.Controls.Contains(YourUC.Instance))
{
pnlControls.Controls.Add(YourUC.Instance);
YourUC.Instance.Dock = DockStyle.Fill;
YourUC.Instance.BringToFront();
}
else
{
YourUC.Instance.BringToFront();
}
}
You it sounds like you want to have a "pages" navigation, I'm not use to work with WinForm, but perhaps this post could help you
What is the most efficient way to create a Win form with multiple pages?
It seems that you have to manage what Control you want to display manually.
Note that passing Controls from Visible to Hidden doesn't unload them
I am using MSChart-Extensions and I would like the option to choose the Zoom, Pan and Select tools from a button, as well as from the ContextMenuStrip. I feel like the easiest way is to simulate a click from the ContextMenuStrip.Items collection
Here is what I've got. In my form I have this
private void zoomButton_Click(object sender, EventArgs e)
{
this.chart.ChangeTool("Zoom");
}
And in MSChartExtensions.cs I have this
public static void ChangeTool(this Chart sender, string option)
{
Chart chart = sender;
foreach(ToolStripItem item in chart.ContextMenuStrip.Items)
{
if (item.Text == option)
{
item.PerformClick();
break;
}
}
}
This successfully chooses the tool from the collection. However, I am getting a System.ArgumentNullException in the SetChartControlState method. I have stepped through the code and I see that when the application enters ChartContext_ItemClicked, the sender's source control is null. I've dug through MSDN, and found this
A Control that represents the control that is displaying the shortcut menu. If no control has displayed the shortcut menu, the property returns a null reference (Nothing in Visual Basic).
So I assume that because no right-click menu (ContextMenuStrip) is shown, the source control is null. Is there a way around this? How can I get this working? Thanks for the help
I figured it out. Change the ChangeTools() method to this
// In MSChartExtensions.cs
public static void ChangeTool(this Chart sender, string option)
{
if (option == "Zoom")
SetChartControlState(sender, MSChartExtensionToolState.Zoom);
else if (option == "Select")
SetChartControlState(sender, MSChartExtensionToolState.Select);
else if (option == "Pan")
SetChartControlState(sender, MSChartExtensionToolState.Pan);
else if (option == "Zoom Out")
{
Chart ptrChart = sender;
WindowMessagesNativeMethods.SuspendDrawing(ptrChart);
ptrChart.ChartAreas[0].AxisX.ScaleView.ZoomReset();
ptrChart.ChartAreas[0].AxisY.ScaleView.ZoomReset();
ptrChart.ChartAreas[0].AxisY2.ScaleView.ZoomReset();
WindowMessagesNativeMethods.ResumeDrawing(ptrChart);
}
}
And then call this method like how I did in the question
// In the form
private void zoomButton_Click(object sender, EventArgs e)
{
this.chart.ChangeTool("Zoom"); // As an example
}
If anyone has a better way of doing this, feel free to let me know
I've got these functions:
private void setupFocusControls(Control parent)
{
foreach (Control control in parent.Controls)
{
control.GotFocus += HandleFocus;
}
}
private void HandleFocus(object sender, EventArgs e)
{
Control control = (Control)sender;
thisFormName = this.Name;
thisControlName = control.Name.ToString();
if (bHelpSystemActive)
{
bHelpSystemActive = false;
if ((ModifierKeys & Keys.Control) == Keys.Control)
{
HelpSystem hs = new HelpSystem(thisFormName, thisControlName);
hs.ShowDialog();
}
else
{
showTooltipForControl(control, thisFormName);
}
return;
}
}
And I call this in the Form_Load function:
private void Labeller_Load(object sender, EventArgs e)
{
setupFocusControls(this);
fillListBox();
}
What this does is show a custom help system I've written. If no control key is clicked, then I'll display the info in a tool tip. If the control key is pressed, then I show an editor. Simple really.
Now, this code works perfectly on another form, which uses panels as containers for my form controls. The problem is, I now want to add this functionality to a separate form. I've added all the code, but none of the controls on the form are having the HandleFocus event added to them. The only difference between this form and the working one is that it uses a splitContainer as it's container.
My question is, why is the setupFocusControls function not looping through the splitContainer as it does the panels on my working form? And, how would I go about fixing it? I'd obviously rather not have several functions to perform this (what I thought) simple task...
Cheers.
Assuming that the problem is that you are not assigning the event to every single control on the form (only top-level controls), the fix should be to change your setupFocusControls(Control) method:
private void setupFocusControls(Control parent)
{
foreach (Control control in parent.Controls)
{
control.GotFocus += HandleFocus;
// add the following line to recurse throughout the control tree
setupFocusControls(control);
}
}
This will add the HandleFocus event handler to every single control, by recursing through the children of every control. I hope this works for you!
As a bonus, if you want to add the event handler to all controls, including the parent control, you could write the setupFocusControls method as follows:
private void setupFocusControls(Control parent)
{
parent.GotFocus += HandleFocus;
foreach (Control child in parent.Children)
setupFocusControls(child);
}
I have a list box with some items. Is there anyway I can attach a double click event to each item?
Item 1
Item 2
Item 3
If i was to double click Item 2, a Messagebox saying "Item 2" would pop up
How would i do this?
void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = this.listBox1.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
{
MessageBox.Show(index.ToString());
}
}
This should work...check
WinForms
Add an event handler for the Control.DoubleClick event for your ListBox, and in that event handler open up a MessageBox displaying the selected item.
E.g.:
private void ListBox1_DoubleClick(object sender, EventArgs e)
{
if (ListBox1.SelectedItem != null)
{
MessageBox.Show(ListBox1.SelectedItem.ToString());
}
}
Where ListBox1 is the name of your ListBox.
Note that you would assign the event handler like this:
ListBox1.DoubleClick += new EventHandler(ListBox1_DoubleClick);
WPF
Pretty much the same as above, but you'd use the MouseDoubleClick event instead:
ListBox1.MouseDoubleClick += new RoutedEventHandler(ListBox1_MouseDoubleClick);
And the event handler:
private void ListBox1_MouseDoubleClick(object sender, RoutedEventArgs e)
{
if (ListBox1.SelectedItem != null)
{
MessageBox.Show(ListBox1.SelectedItem.ToString());
}
}
Edit: Sisya's answer checks to see if the double-click occurred over an item, which would need to be incorporated into this code to fix the issue mentioned in the comments (MessageBox shown if ListBox is double-clicked while an item is selected, but not clicked over an item).
Hope this helps!
I know this question is quite old, but I was looking for a solution to this problem too. The accepted solution is for WinForms not WPF which I think many who come here are looking for.
For anyone looking for a WPF solution, here is a great approach (via Oskar's answer here):
private void myListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DependencyObject obj = (DependencyObject)e.OriginalSource;
while (obj != null && obj != myListBox)
{
if (obj.GetType() == typeof(ListBoxItem))
{
// Do something
break;
}
obj = VisualTreeHelper.GetParent(obj);
}
}
Basically, you walk up the VisualTree until you've either found a parent item that is a ListBoxItem, or you ascend up to the actual ListBox (and therefore did not click a ListBoxItem).
For Winforms
private void listBox1_DoubleClick(object sender, MouseEventArgs e)
{
int index = this.listBox1.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
{
MessageBox.Show(listBox1.SelectedItem.ToString());
}
}
and
public Form()
{
InitializeComponent();
listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_DoubleClick);
}
that should also, prevent for the event firing if you select an item then click on a blank area.
It depends whether you ListBox object of the System.Windows.Forms.ListBox class, which does have the ListBox.IndexFromPoint() method. But if the ListBox object is from the System.Windows.Control.Listbox class, the answer from #dark-knight (marked as correct answer) does not work.
Im running Win 10 (1903) and current versions of the .NET framework (4.8). This issue should not be version dependant though, only whether your Application is using WPF or Windows Form for the UI.
See also: WPF vs Windows Form
This is very old post but if anyone ran into similar problem and need quick answer:
To capture if a ListBox item is clicked use MouseDown event.
To capture if an item is clicked rather than empty space in list box check if listBox1.IndexFromPoint(new Point(e.X,e.Y))>=0
To capture doubleclick event check if e.Clicks == 2
The post is old but there is a simple solution for those who need it
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > -1)
{
MessageBox.Show(listBox1.Items[listBox1.SelectedIndex].ToString());
}
}