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
Related
I have a combobox that dictates the linklabel name, I would like to select the linklabel depending on the name of it. Here is what I have done so far. First part of the if statement works but the second does not.
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if((string)linkLabel1.Text == "Advanced Software Engineering")
{
Form4 g = new Form4();
g.Show();
}
else if((string)linkLabel1.Text == "Web Research")
{
Form5 g1 = new Form5();
g1.Show();
}
}
What exactly is the problem? Your approach would work fine.
However I would create my own FormBase that has a property called "LinkLabel"
Each WinForm would then set the LinkLable its designed to be used by.
Then in your LinkClicked event do something like this
foreach(FormBase _base in _Forms)
{
if(_base.LinkLabel == linkLabel1.Text)
{
_base.Show();
break;
}
}
EDIT: This wouldn't work if the event is not called by clicking on linkLable1
I am having a message box a simple one inside an if condition and the thing is i want the message box to automatically close when the user points to the OK button in the message box, what i am not able to figure out is how to access the message box OK button
private void button3_Click(object sender, RoutedEventArgs e)
{
Clipboard.Clear();
//string queryvalue;
//queryvalue = SelectedQuery.Value;
//SelectedQuery.Value = queryvalue;
if (QueryChooser.SelectedItem == null)
{
button3.Background = Brushes.PaleVioletRed;
MessageBox.Show("Select a value");
}
else
{
Clipboard.SetText(SelectedQuery.Value);
}
}
In cases like this I've found that it is easier to just create a simple Window resembling a MessageBox and pop it using ShowDialog(), this way you'll have a more flexible "MessageBox".
if (MessageBox.Show("Select a value") == DialogResult.Ok) {
// do something
}
UPDATE
As Saeb already mentioned you should create your own simple MessageBox dialog.
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());
}
}
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();
}
}
Is it somehow possible to disable one (or more) tabs of tab control? At some point I need to make user stay on the active tab and prevent him from leaving... I know I can disable the whole TabControl component, but that disables also all components on active tab...
I also tried to use the Selecting method of TabControl:
private void TabControl_Selecting(object sender, TabControlCancelEventArgs e) {
e.Cancel = PreventTabSwitch;
}
This works, prevents user from switching (if PreventTabSwitch==true), but since all tabs look active and just don't react it's confusing...
There is no Enabled property for individual tab pages, so I don't know what else to do...
Thanks a lot for in advance for all tips.
IIRC, this is the only way to prevent a user from switching tabs.
I presume you are preventing them from leaving as validation on the form has failed? Using the ErrorProvider component would provide some sort of visual cue that they need to do something before switching tabs.
I've had a similar need once (I wanted the active tab to have different background color and some other stuff) and ended up creating new Controls that inherited from TabControl & TabPage where I used OwnerDraw to alter the look.
What you are doing is the right way to go according to MSDN but it does suggest that another option is to hide/show the pages as needed.
TabControl - Disable/Enable tab page at
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/985b41c3-a1de-4744-8875-63262d4c2718/
MSDN Search for "tabcontrol disabled tabpage" at http://social.msdn.microsoft.com/Search/en-US?query=tabcontrol+disabled+tabpage&ac=8
The user cannot click on tabs to navigate, but they can use the two buttons ( Next , Back ). The user cannot continue to the next if the //conditions are no met
private int currentTab = 0;
private void frmOneTimeEntry_Load(object sender, EventArgs e)
{
tabMenu.Selecting += new TabControlCancelEventHandler(tabMenu_Selecting);
}
private void tabMenu_Selecting(object sender, TabControlCancelEventArgs e)
{
tabMenu.SelectTab(currentTab);
}
private void btnNextStep_Click(object sender, EventArgs e)
{
switch(tabMenu.SelectedIndex)
{
case 0:
//if conditions met GoTo
case 2:
//if conditions met GoTo
case n:
//if conditions met GoTo
{
CanLeaveTab:
currentTab++;
tabMenu.SelectTab(tabMenu.SelectedIndex + 1);
if (tabMenu.SelectedIndex == 3)
btnNextStep.Enabled = false;
if (btnBackStep.Enabled == false)
btnBackStep.Enabled = true;
CannotLeaveTab:
;
}
private void btnBackStep_Click(object sender, EventArgs e)
{
currentTab--;
tabMenu.SelectTab(tabMenu.SelectedIndex - 1);
if (tabMenu.SelectedIndex == 0)
btnBackStep.Enabled = false;
if (btnNextStep.Enabled == false)
btnNextStep.Enabled = true;
}
If you want to cancel the change of a tab, you can use the Deselecting event. There you can cancel the change by setting property Cancel of the provided TabControlCancelEventArgs to true.