I'm tring to implement a button which have a dropdown menu when checked and this menu is gone when unchecked. My problem is I cannot uncheck the checkbox when it or its menu lost focus.
The checkbox's appearance mode is button.
My code:
private void cbSettings_CheckedChanged(object sender, EventArgs e)
{
if (cbSettings.Checked) {cmsSettings.Show(cbSettings, 0, cbSettings.Height);}
else {cmsSettings.Hide();}
}
I've tried to uncheck the checkBox on contextMenuStrip's VisibleChanged / Closed event but this caused menu not to hide (or hide and show immediately).
The example below does not, of course, include the code you would need for swapping BackGroundImage of the CheckBox to indicate CheckState. The events to "wire-up" should be obvious. Hope this is helpful.
// tested in VS 2010 Pro, .NET 4.0 FrameWork Client Profile
// uses:
// CheckBox named 'checkBox1
// ContextMenuStrip named 'contextMenuStrip1
// TextBox named 'cMenuSelectionInfo for run-time checking of results
// used to position the ContextMenuStrip
private Point cPoint;
// context click ? dubious assumption that 'right' = context click
private bool cmOpenedRight;
// the clicked ToolStripMenuItem
private ToolStripMenuItem tsMIClicked;
private void checkBox1_MouseDown(object sender, MouseEventArgs e)
{
cmOpenedRight = e.Button == MouseButtons.Right;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// positioning the CheckBox like this
// is something in a 'real-world' example
// you'd want to do in the Form.Load event !
// unless, of course, you'd made the CheckBox movable
if(checkBox1.Checked)
{
contextMenuStrip1.Show();
cPoint = PointToScreen(new Point(checkBox1.Left, checkBox1.Top + checkBox1.Height));
contextMenuStrip1.Location = cPoint;
}
else
{
contextMenuStrip1.Hide();
}
}
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
// assume you do not have to check for null here ?
tsMIClicked = e.ClickedItem as ToolStripMenuItem;
tbCbMenuSelectionInfo.Text = tsMIClicked + " : " + ! (tsMIClicked.Checked);
}
private void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = checkBox1.Checked;
}
private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
if (cmOpenedRight)
{
tbCbMenuSelectionInfo.Text += " : closed because : " + e.CloseReason.ToString();
}
}
I think your approach of unchecking the check box on the context menu's closed event is a good one, what you need is a bit of "event cancelling logic"(c), like this:
private void OnContextClosing(object sender, EventArgs e)
{
_cancel = true;
cbSettings.Checked = false;
_cancel = false;
}
private void cbSettings_CheckedChanged(object sender, EventArgs e)
{
if(_cancel)
return;
if (cbSettings.Checked) {cmsSettings.Show(cbSettings, 0, cbSettings.Height);}
else {cmsSettings.Hide();}
}
This will keep your CheckChanged event from re-checking your checkbox.
Related
So I have a matrix of panels (maybe will change for Picture Boxes in the future), and what i want is that every time i press one of the panels after pressing the button on the toolbox it will change it's background to a certain picture.
Right now what i have is:
private void EtapaInicial_Click(object sender, EventArgs e)
{
EtapaInicialWasClicked = true;
}
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
panel1.BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
What I would like to change was the panel1 to make it work for every panel (otherwise it will only change panel1 independently of the panel i've clicked), is that possible?
Try the following
private void EtapaInicial_Click(object sender, EventArgs e)
=> EtapaInicialWasClicked = true;
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
(sender as Panel).BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Yes it is. You have to loop through each panel
and assign the same event handler but you have to make some changes in the event handler itself
foreach(var p in allPanels)
{
p.Click += panel_Click;
}
Then change your event handler like this
private void panel_Click(object sender, EventArgs e)
{
var p = (Panel)sender;
if (EtapaInicialWasClicked)
{
p .BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Remember the sender argument contains reference to the actual control that initiated the event but you have to cast it first in order to use it.
However if you want to store more data for the event you've just handled you can use the panel.Tag property. This can be used to store EtapaInicialWasClicked for example
I am new to C# and I am planing to design my own keypad but I don't know how/where to start. as shown in photo, I have 4 textBoxes the keypad buttons.
The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?).
So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing.
Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in.
Thank you professionals.
Firstly, have a textbox that represents the one that is selected (outside of subroutines but inside the class):
TextBox SelectedTextBox = null;
And then make the "Click" event of each TextBox look like this:
private void textBoxNUM_Click(object sender, EventArgs e)
{
SelectedTextBox = sender as TextBox;
}
And then make the "Click" event of each Button look like this:
private void buttonNUM_Click(object sender, EventArgs e)
{
if (SelectedTextBox != null)
{
SelectedTextBox.Text = buttonNUM.Text;//Or set it to the actual value, whatever.
}
}
Or if that one doesn't work, this should.
private void buttonNUM_Click(object sender, EventArgs e)
{
if (SelectedTextBox != null)
{
(SelectedTextBox as TextBox).Text = buttonNUM.Text;//Or set it to the actual value, whatever.
}
}
To check if a textbox is focused you can do
if(textbox1.Focused)
{
//Print the value of the button to textbox 1
}
else if (textbox2.Focused)
{
//Print the value to textbox 2
}
UPDATE:
Since the textbox will lose focus when you click the button, you should have a temporary textbox (ie lastTextboxThatWasFocused) which is saved to everytime a textbox gains focus. Write an OnFocused Method and do something like
public void Textbox1OnFocused(/*Sender Event Args*/)
{
lastTextboxThatWasFocused=textbox1;
}
Then on button click you can do
if(lastTextboxThatWasFocused.Equals(textbox1))
{
//ETC.
}
You can give something along these lines a try. Create a generic click handler for the buttons and then assign the value to a textbox the text from the button, which happens to be the value. You can check which box was the last one focused in the TextBoxes' Click event. Create a global variable to store which one and use it in the below method.
private TextBox SelectedTextBox { get; set; }
private void NumericButton_Click(object sender, EventArgs e)
{
var clickedBox = sender as Button;
if (clickedBox != null)
{
this.SelectedTextBox.Text += clickedBox.Text;
}
}
private void TextBox_Click(object sender, EventArgs e)
{
var thisBox = sender as TextBox;
if (thisBox == null)
{
return;
}
this.SelectedTextBox = thisBox;
}
Try this code:
TextBox LastTxtBox;
private void textBox_Enter(object sender, EventArgs e)
{
LastTxtBox = sender as TextBox;
}
private void button_Click(object sender, EventArgs e)
{
LastTxtBox.Text = this.ActiveControl.Text;
}
Add textBox_Enter function to all textboxes enter event.
Add button_Click to all buttons click event.
Button Enter Event
Control _activeControl;
private void NumberPadButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (_activeControl is TextBox || _activeControl is RichTextBox)
{
_activeControl.Text += btn.Text;
if (!_activeControl.Focused) _activeControl.Focus();
}
}
TextBox or RihTextBox Enter Event
private void TextBoxEnter_Click(object sender, EventArgs e)
{
_activeControl = (Control)sender;
}
Is this possible to display button on Windows Form only when focus is on specific textbox?
Tried that with this approach:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
button3.Visible = false;
}
No luck, because button click does not work then, because button is hidden immediately after textbox lost focus, preventing it from firing button3_Click(/*...*/) { /*...*/ } event.
Now I'm doing it like that:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
//button3.Visible = false;
DoAfter(() => button3.Visible = false);
}
private async void DoAfter(Action action, int seconds = 1)
{
await Task.Delay(seconds*1000);
action();
}
Form now waits for a second and only then hides button3.
Is there any better approach?
I think you want to display the button only when focus is on specific textbox or the focus is on the button.
To do this you can check the Focused property of button3 in the Leave event of textBox2 and only hide the button if the button doesn't have focus. Note that the button will get focus before the Leave event of textBox2 fires.
You will then need to hide the button in the scenario where button3 loses focus and the focus moves to somewhere other than textBox2. You can use exactly the same technique here by handling the Leave event of button3 and only hiding button3 if textBox2 does not have focus.
The following code should fit your requirements:
private void textBox2_Leave(object sender, EventArgs e)
{
if (!button3.Focused)
{
button3.Visible = false;
}
}
private void button3_Leave(object sender, EventArgs e)
{
if (!textBox2.Focused)
{
button3.Visible = false;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
Why not work with the GotFocus and LostFocus event of the TextBox?
private void textBox2_GotFocus(object sender, EventArgs e)
{
button3.Visible = true;
}
Then hide the button on the click event.
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
button3.Visible = false;
}
How about you add a Panel and place the button and text boxes in that panel and when user MouseHovers that Panel then display the button...
This way user would be able to click on the button...
This is the event you are looking for, I think...
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
UPDATE:
var textboxFocussed = false;
private void textBox2_Enter(object sender, EventArgs e)
{
textboxFocussed = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textboxFocussed = false;
}
UPDATE 2
private void Panel_GotFocus(object sender, EventArgs e)
{
button3.Visible = textboxFocussed;
}
private void Panel_LostFocus(object sender, EventArgs e)
{
button3.Visible = false;
}
Here are the details of the Panel Events
you can add Enter event handler for all controls on form at Load. Just make sure to skip the controls on which you want to show the button.
List<string> strControlException = new List<string>();
public Form1()
{
InitializeComponent();
strControlException.Add("btnMain");
strControlException.Add("txtMain");
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count;i++ )
{
if (!strControlException.Contains(Controls[i].Name))
{
Controls[i].Enter += new EventHandler(hideButton);
}
}
}
private void txtMain_Enter(object sender, EventArgs e)
{
btnMain.Visible = true;
}
private void hideButton(object sender, EventArgs e)
{
btnMain.Visible = false;
}
btnMain (Button you want to Manipulate) and txtMain (Which controls the vibility of the button) are the controls in contention here
Add more controls on the form to test.
Explanation for the above code :
First initialize a list with the names of controls that should show the Button
On Form Load add an Event handler to all controls (except the one in our list)
In the handler function hide the button. (You might want to perform more logic here based on the control that called this function)
Button is hidden by default and only on textbox Enter event we show the button.
I have done this before and I have a way of doing it, but I want to make sure it is the best way. I have a ListView in Details view. I also have a button. I only want that button to be enabled if there is an item selected (multiselect is disabled). Items will be added and removed to this listview but the button should be enabled anytime there is a selected item and disabled otherwise.
My event handler:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
button1.Enabled = true;
else
button1.Enabled = false;
}
That is what I have, just wondering if that will always work or are there freak incidents where it fails? Like if I delete or add things or anything else?
It'd be better if you show what you have - but in short, you start with the button disabled, and in the list view selectedindexChanged event enable the button if the list view has a selectedItems.Count of 1. Disable it if no item is selected. Here's a link which may help:
ListView selectedindexchanged
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 9; i++)
{
listView1.Items.Add("kashif");
}
button1.Enabled = false;
}
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
button1.Enabled = listView1.SelectedItems.Count > 0;
}
private void button2_Click(object sender, EventArgs e)
{
foreach (ListViewItem v in listView1.SelectedItems)
{
v.Remove();
}
}
}
Before Button2 Click
After Button2 click
I want to display a ballon tip when an error occures instead of showing MessageBox.
[NOTE] i did not want it to be shown on mouse Hover.
I tried both but they actually show the tip on mouse hover
toolTip1.SetToolTip();
toolTip1.Show();
You can use the ToolTip Popup event to check if there is a Tooltip present and cancel it if there isn't. You can then set the tooltip during your validation then show it. In this example I set a timer to reset the tooltip text after a 2 second timeout.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
toolTip1.IsBalloon = true;
toolTip1.Popup += new PopupEventHandler(toolTip1_Popup);
toolTip1.SetToolTip(textBox1, "");
}
void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (toolTip1.GetToolTip(e.AssociatedControl) == "")
e.Cancel = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
toolTip1.RemoveAll();
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
int temp;
if (!int.TryParse(textBox1.Text, out temp))
showTip("Validation Error", (Control)sender);
}
private void showTip(string message, Control destination)
{
toolTip1.Show(message, destination);
timer1.Start();
}
}
Much to my surprise it appears that toolTip1.IsOpen = true will show a tooltip and allow it to stay open. Note that you will need to provide code to close it because it wouldn't go away on its own on my machine no matter what I did.