Double-clicking a button doesn't open its code-behind - c#

I'm making a windows form using c# and added 3 buttons. When I double click on any object, it takes me to the code, the specific function that gets called when that object is activated, depending on its type. However, when I double click on the 2nd or 3rd buttons, it takes me to button 1's code instead of opening the function called by double clicking on the button I pressed.
Help?

That happens when all 3 buttons are assigned to the same event. Select the 2 wrong buttons and delete their event in the properties window. Now doubleclicking one of them will create a new code template for it.. You probably had the three all selected when you created the 1st event..

Remove the button click events via the properties window for each button and re-assign them apart.

Related

Must click a button twice to fire an event

I have a data grid that is created dynamically to display information retrieved from a database. The data grid consists of multiple bound columns and two button columns for each row, a view and a delete button.
Clicking either button calls the Page_Load() method with Page.IsPostBack as false.
if (!Page.IsPostBack)
{
this.bindForm(); // Populates drop down lists that the user can select
setAccess(false); // Sets access for the page
// Stuff commented out to avoid confusion
}
else
{
bindDynamicGrids(); // Create the data grid(s) and populate them
}
So when I first click the button the above code gets called with IsPostBack false, and the dynamic grids get bound. But, the button click event does not fire. When I click the button a second time, Page_Load is called with IsPostBack false, and after executing bindDynamicGrids() the button click event is fired. I cannot understand the difference between the first and second click.
I have read a few threads;
ASP.NET C#, need to press a button twice to make something happen
http://forums.asp.net/t/1783694.aspx?ASP+NET+Button+needs+to+be+clicked+twice
To attempt to understand the issue, but I must be missing something. From what I am gathering from the second link, a session variable may be getting set in the click event, which is also being set in Page_Load, and this is all an issue with ordering. If that is the case I am not seeing where it is happening.
When it is not a post back the bindForm() method is called, which populates all drop down lists. The edit click event populates those drop downs with the values from the row, but the click event is always a post back and the form has already been bound.
I have also considered having a script automatically double click one of the buttons anytime the user single clicks, but I have not been able to find an "OnClick" property for the column button. Any help that could be provided would be phenomenal.
The thing is, that the button click event happens after the Page_Load event meaning that the filtering does not get applied on the first postback. It has been updated on the second postback and you see the filtering.
You can try to move the code of your page_load event to OnPreRender so the reload happens after the button click event.
for more information look here: Button needs to be clicked twice
and here

Add Default Event Handler For All Selected Controls

I have a fair number of buttons on one form and I want to add separate click (default) event handler for each. If I double click one control it adds the empty click event handler to the code behind.
Is there anyway for me to select, say 20, buttons and add a separate default handler for each?
I have tried Shift double click, Alt double click Ctrl double click but they all do the same, add one event handler and assign it to all the controls.
Edit
I get that I could have the same event handler for all the buttons, work out which one was click etc etc. This is not what I am asking. I just wanted a quick way to add the empty click handler for all selected buttons.
Sorry. Misunderstood the question, so removing the old answer as it's not really applicable.
My advice would be open the Form cs file in a text editor, find the region where the control/event bindings occur and copy, paste a particular Button line updating the event and id after.
Pretty laborious still.

how to pass values in textbox by clicking button to a datagridview in another form in C#

I have two forms right now, form 1 has a datagridview inside and form 2 has a textbox and a button inside. I want to pass value is entered in textbox to a cell in datagridview in form 2 when I click button. I think code should be written in button_click function in form 1. The basic idea is I don't know how to call datagridview in form 2.
I'm not sure if it's going to work, but try to define a new property inside your first form class and inside your button click event get the value from the textbox and save it inside a variable.
So you can get the value of the variable from the previous define property. When you go to the second form you can update the grid and add the new data to.
If you wanna this passing for multiple values, you can save the all changes on the text box in a list. When you go to the other form you can update your info.
But I don't recommend to make the grid public. Keep it private! I hope it's helpful!

Would it be possible to remove (or hide) a button through the code?

I have been googling this question, but so far nothing found.
Is there some function, that enables me to remove or hide an button (in wpf)?
For example:
A form with 2 buttons on it:
On initialize, only button 1 will be visible
Click button1, this will make button2 visible
Click button2, this will make button1 get removed (or hidden).
Is such a thing possible in c# in WPF?
Look at setting it Collapsed on one click and uncollapsed on the other
For Button 1's ClickEvent
button2.Visibility = System.Windows.Visibility.Collapsed;
It is possible and very easy to do, just use the buttons click event handlers to toggle the other buttons visibility property.
And one more important thing you have to give a name to your buttons so you can use them from code behind.
MSDN Visibility Property

C# Button Click Events firing after Command

I am having trouble trying to get a click event to fire after the command gets executed. I am trying to follow the MVVM style of development.
Here is the scenario:
In my application I have a grid of 2 by N buttons, where N is a dynamically loaded set of buttons (sometimes it'll be 2 x 2, sometimes its 5 x 9, I don't know). Initially, all the columns of buttons will be hidden except for the first column. Upon clicking the first column, a Command gets executed, and a fade in animation gets played and shows certain buttons from the second column, depending on which button was clicked in the first column. For example, clicking the top button in the first column will reveal the first four buttons in the second column, whereas the bottom button in the first column will reveal the first three buttons in the second column.
Here's the problem:
For each button, I set a CommandParameter to each of the buttons in the first column. When the command gets executed, data gets processed, and a property is set in the VM that says which button was pressed (I just use the CommandParameter again). While the data is being processed, I want to display the next set of buttons to the user. I have a click event that takes the CommandParameter from the VM and plays an animation to show the proper buttons.
However, the problem is that WPF likes to do the Click event first before the Command gets fired. This is a problem since the property in the VM that says which button was clicked doesn't get set!
So that brings me to a couple questions...
1) Is there a way to execute the command before firing the event?
2) Am I doing it right? :P
Instead of using a click event handler, you could just setup DataTriggers on your buttons.
They could respond to the property in your ViewModel, hiding buttons you don't want displayed, and showing the button you do.
The nice thing about this is that your View can be done completely in XAML, and the ViewModel is just setting state (ie: I'm at this state), and the View will automatically stay updated since the binding will cause the new DataTrigger to fire.

Categories

Resources