I'm designing a WPF Application in C# and I'm creating a grid with its controls at runtime. All of my controls work fine (Image, Textblocks, Labels), but my Button does not. It just sits there. When I move my mouse over it, it does nothing. When I click it, (w/ a click event handler) it does absolutely nothing! I've tried using different event handlers such as (Click, MouseDown, MouseUp). I have no clue what the problem is. Here's my code for the button:
Button AccessBtn = new System.Windows.Controls.Button();
AccessBtn.Content = "Access:";
AccessBtn.Margin = new Thickness(397, 571, 472, 41);
AccessBtn.Name = "AccessButton";
AccessBtn.Focusable = true;
AccessBtn.Click += (s, EventArgs) =>{
MessageBox.Show("Nothing here yet!");
};
grid.Children.Add(AccessBtn);
When you add children to the grid. They are added to row 0 and column 0.
So they will all be stacked on to each other. The button is no longer visible for the mouse event. You should put the button on top of the items or use row and columns to arrange the items. (Alternatively you might want to check a Canvas to position your elements.)
Related
I have encountered a very odd issue with the code I am working on. I have a Table Layout Panel that contains two smaller table layout panels. One contains 6 radio buttons to handle SMEMA modes while another contains buttons to handle conveyor actions (load, rewind).
Nested within the click event handler for the conveyor buttons, I have a function that disables the buttons while motion is active (segment below). I have found that when the line calls to disable the rewind button, the CheckedChanged event for the radio buttons suddenly fires. NOTE: This is only when one of the 6 buttons are selected.
bool isIdle = state.IsIdle;
//process tab
_buttonStartupRoutine.Enabled = isIdle;
_ckbuttonPLCRun.Enabled = true;
_buttonCameraMode.Enabled = isIdle;
_checkBoxDryRun.Enabled = isIdle;
_updownJogScale.Enabled = isIdle;
_buttonConveyorLoad.Enabled = isIdle;
_buttonConveyorUnload.Enabled = isIdle;
_buttonConveyorRewind.Enabled = isIdle;
_updownConveyorWidth.Enabled = isIdle;
I have confirmed that this line is causing the CheckedChanged event to fire by moving it higher in the call stack so it is called immediately when the Rewind button is clicked. However, I can't find any link between the two besides sharing the same general table layout panel, despite being in two separate nested panels.
Has anyone ever experienced something like this?
EDIT: With more troubleshooting I have confirmed that something is trickling down into the radio buttons. I added a foreach statement above the line that says _buttonConveyorRewind.Enabled = isIdle; that disables each radio button in the table layout panel. This also re-enabled them because it comes back through the attached code once the movement of the conveyor is complete.
foreach (RadioButton button in tableLayoutPanel22.Controls)
{
button.Enabled = isIdle;
}
I also had to call the Focus event on the main panel to prevent the panel from jumping down below the panel of radio buttons. This seemed to prevent the radio buttons from changing and the SMEMA mode of the machine itself was remaining consistent as well. It would seem that a Focus event somewhere was trickling down and causing the radio buttons to change.
It turns out that an event was trickling down into either the panel or radio buttons. My solution was to disable each radio button using a foreach statement before disabling the conveyor buttons. This seemed to prevent the event from changing the buttons since they were disabled when it hit. I also had to manually call Focus() on the main panel to get the focus back up to the top of the scroll area. This would confirm that a focus event of some sort was causing the radio buttons to think they were changing. I have included the code change below.
foreach (RadioButton button in tableLayoutPanel22.Controls)
{
button.Enabled = isIdle;
}
_Panel_Main.Focus();
I have 2 images for 2 players on my windows form. I have added a MouseClick event for both of them. Now when I mouse click, it activates both the events for both the players. I wanted to know only one player where I am clicking.
How do I do that?
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this._player1_MouseClick);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this._player2_MouseClick);
Assume you have two PictureBox controls with images. Thus you have posted code from designer, I also assume that you are subscribing to events via designer.
Select one of PictureBox controls
In Events tab find Click property, type-in Player_Click as handler name and hit Enter. You have subscribed to Click event of first control.
Select second PictureBox
In Events tab find Click property, click on drop-down and select Player_Click as well. Hit Enter. You have used same event handler to subscribe to Click event of second control.
Now go to code view (you can double click any of those controls for that) and find Player_Click method
In order to find which control was clicked you need to cast sender argument to PictureBox type:
private void Player_Click(object sender, EventArgs e)
{
var pictureBox = (PictureBox)sender;
// use control which was clicked. e.g. get it's name
var name = pictureBox.Name;
}
I have a TabControl to which the user can add tab pages.
I am trying to attach some events to it such as: MouseEnter, MouseLeave, MouseClick, But it seems the these events are not firing at all, they only fire when I attach them to the TabControl itself, but this is not what I need.
What is the problem with attaching events to a tab control tab page ?
Here is my latest attempt to attach these event from my code:
private void customerTabCtrl_ControlAdded(object sender, ControlEventArgs e)
{
TCTabPage tctab = (TCTabPage)e.Control; // Option A
TCTabPage tctab = (TCTabPage)customerTabCtrl.Controls[customerTabCtrl.Controls.Count - 1]; //Option B
tctab.MouseEnter += new EventHandler(tctab_MouseEnter);
tctab.MouseLeave += new EventHandler(tctab_MouseLeave);
}
I fill so silly...
I found out the "problem", I thought that the MouseEnter, MouseLeave, MouseClick events should fire even when the cursor is on the tab header, but it appears that these events fire only when the cursor is at the tab body...
Sory for the trouble, I am using winforms only 6 months now...
You don't need an event for this since by default, the end user cannot add TabPages to the TabControl without you providing the code for it.
So wherever you are adding the TabPage, that's when you should wiring up those events:
TCTabPage tctab = new TCTabPage();
tbtab.Text = "New Tab";
tctab.MouseEnter += tctab_MouseEnter;
tctab.MouseLeave += tctab_MouseLeave;
customerTabCtrl.TabPages.Add(tctab);
so i have a flow panel and a button that adds listviews to it at run time. i have my doubleclick events set up - is it possible to set up some kind of click (or click and drag) event to rearrange the controls in the flow panel?
i know we can change the sort strategy (top down, left right) and wrap, but i was hoping for organization a user could simply drag the control from one spot and relocated somewhere else.
private void addNewWOButton_Click(object sender, EventArgs e)
{
ListView newListView = new ListView();
newListView.AllowDrop = true;
newListView.DragDrop += listView_DragDrop;
newListView.DragEnter += listView_DragEnter;
newListView.DoubleClick += listView_DoubleClick;
flowPanel.Controls.Add(newListView);
}
This will move a control to the top of the panel and move the remaining controls down.
FlowLayoutPanel1.Controls.SetChildIndex(myControl, 0);
For drag and drop re-ordering, you'll have to hook up drag and drop events for each control. On the drop event get the control being dragged and the index position of the target control. Then change the index with SetChildIndex. If your still working on this problem, I can dig up some code to show you.
I have a problem where scrolling in both a toolStripComboBox and regular ComboBox is really slow.
This happens both using the arrowkeys and the mouse scrollwheel. However, if I use the scrollbar it behaves as expected.
Here's the toolstrip combobox:
//
// toolStripComboBoxDeild
//
this.toolStripComboBoxDeild.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.toolStripComboBoxDeild.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.toolStripComboBoxDeild.DropDownWidth = 121;
this.toolStripComboBoxDeild.Items.AddRange(new object[] {
"Allir"});
this.toolStripComboBoxDeild.Margin = new System.Windows.Forms.Padding(1, 0, 8, 0);
this.toolStripComboBoxDeild.MaxDropDownItems = 24;
this.toolStripComboBoxDeild.Name = "toolStripComboBoxDeild";
this.toolStripComboBoxDeild.Size = new System.Drawing.Size(200, 52);
this.toolStripComboBoxDeild.SelectedIndexChanged += new System.EventHandler(this.toolStripComboBoxDeild_SelectedIndexChanged);
I'm adding the rest of the data in the combobox with an SqlDataReader (not using a dataset because I'm comfortable using the sqlreader).
and the regular combobox:
//
// comboBox1
//
this.comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.comboBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(77, 17);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(221, 21);
this.comboBox1.TabIndex = 1;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
Has anyone ever run into this problem? If so, what did you do to solve it?
EDIT
Changing the event handler to SelectionChangeCommitted solved the problem regarding the arrow keys, but not the mouse part.
The mouse scrolling behaviour is only aberrant when the mouse is over the dropdown list. When I click the combobox down arrow without moving the mouse and apply the scroll wheel, the list scrolls as expected.
EDIT 2
Figured out the problem with the mouse scrolling, turns out that it's the "Lenovo Mouse Suite" software and/or driver. Uninstalled it and now everythings just fine.
Thanks to Jeff Yates for showing me the SelectionChangeCommitted event.
When you use the keyboard, the selected index changes. When using the scroll wheel, the item under the mouse changes which will also lead to the SelectedIndexChanged event.
Therefore, if your event handler is intensive when the index changes, it will slow down the scrolling as it will run each time the selected index changes (i.e. each time your scroll with the mouse or keyboard). You should use SelectionChangeCommitted to handle when the selection changes instead as this will only fire once the combo is closed.
Update
So, you use the mouse wheel when the combo is NOT dropped down? If that is the case, then it is still the selection change handling as each roll of the wheel will change the committed selection. Scrolling when the combo is dropped down does not do this.
I recommend you add some kind of selection filter using a timer. You start (and restart) the timer each time the selection is committed. Only when the timer fires do you actually handle the selection change. This way, you scroll with the mouse wheel without incurring a selection penalty each time. Make sure to stop the timer when it fires, of course.