if in a form I have 10 panels named in order from 1 to 10 and all of them registered with the same Event myPanel_Click
private void myPanel_Click(object sender, EventArgs e)
{
}
can I retrieve the name of the panel I clicked among those 10 panels?
int panelClicked;
private void myPanel_Click(object sender, EventArgs e)
{
//not a single clue
}
If I understand you correctly, you should be able to cast the sender as a panel and then take the name property.
private void myPanel_Click(object sender, EventArgs e)
{
Panel target = sender as Panel;
if(target != null)
MessageBox.Show(target.Name);
}
private void myPanel_Click(object sender, EventArgs e)
{
MessageBox.Show((Panel)sender.Name);
}
You can also us the Tag Property to reference your Panels, by assigning your panel number to the respective Tag.
private void myPanel_Click(object sender, EventArgs e)
{
Panel p = (Panel)sender;
switch ((int)p.Tag )
{
case 1:
// Your Code for Panel 1
break;
case 2:
// Your Code for Panel 2
break;
// Your other Panels here
default:
break;
}
}
Related
I try to switch panels on tabControls on UserControl.
Like this
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible=true;
panel2.Visible=false;
.....
panelN.Visible=false;
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible=false;
panel2.Visible=true;
.....
panelN.Visible=false;
}
private void buttonN_Click(object sender, EventArgs e)
{
panel1.Visible=false;
panel2.Visible=false;
.....
panelN.Visible=true;
}
but when N exceeds 6, switching panels doesn't work well.
Some panels don't visible ,even if the Button event occurs!
So could you tell me how to switch multi panels.
If possible, could you tell me the smart way to switch panels.
The above code seems to be bad readability.
You could make all of the buttons fire the same handler, then do something like this:
private void btn_Clicked(object sender, EventArgs e)
{
Button btn = (Button)sender;
int numButtons = 6;
int index = int.Parse(btn.Name.Replace("button", ""));
for(int i=1; i<=numButtons; i++)
{
Control ctl = this.Controls.Find("panel" + i, true).FirstOrDefault();
if (ctl != null)
{
ctl.Visible = (i == index);
if (i == index)
{
ctl.BringToFront();
}
}
}
}
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
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'm looking for a way to determine which item in a toolStrip that was dragged after a DragDrop event has occured, all I want to do is make a switch case with different cases for each item in the toolstrip but I cant seem to find a way of comparing them.
UPDATE: SHORT CODESAMPLE
private void toolStrip1_DragDrop(object sender, DragEventArgs e)
{
//Here I want something like a DoDragDrop() and send the specific item from the
//toolstrip..
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
//And here some way to determine which of the items was dragged
//(I'm not completely sure if I need a mouseUp event though..)
}
Hopefully a bit easier to get what I'm trying to do.
The events in your example don't look like the correct events to use.
Here is a working example from a ToolStrip that has 2 ToolStripButtons on it:
public Form1() {
InitializeComponent();
toolStripButton1.MouseDown += toolStripButton_MouseDown;
toolStripButton2.MouseDown += toolStripButton_MouseDown;
panel1.DragEnter += panel1_DragEnter;
panel1.DragDrop += panel1_DragDrop;
}
void toolStripButton_MouseDown(object sender, MouseEventArgs e) {
this.DoDragDrop(sender, DragDropEffects.Copy);
}
void panel1_DragEnter(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.Copy;
}
void panel1_DragDrop(object sender, DragEventArgs e) {
ToolStripButton button = e.Data.GetData(typeof(ToolStripButton))
as ToolStripButton;
if (button != null) {
if (button.Equals(toolStripButton1)) {
MessageBox.Show("Dragged and dropped Button 1");
} else if (button.Equals(toolStripButton2)) {
MessageBox.Show("Dragged and dropped Button 2");
}
}
}
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.