What is the logical difference between PushButtonState.Default and PushButtonState.Normal? - c#

I am using ButtonRenderer.DrawButton for a column with buttons with images in a DataGridView, and I am interested in when each PushButtonState value should be used.
Here is the official documentation. It says:
Default - The button has the default appearance.
Disabled - The button is disabled.
Hot - The button is hot.
Normal - The button has the normal appearance.
Pressed - The button is pressed.
The ones I do not understand well are Default and Normal. What is the difference between these two roles? In the screenshot below these 2 roles are combined with the focused bool parameter passed to the ButtonRenderer.DrawButton method.

About the default state
According to user experience guidelines for Windows-based desktop applications for button control:
The default command button is invoked when users press the Enter key.
It is assigned by the developer, but any command button becomes the
default when users tab to it.
In windows forms, to set a button as the default button of a form, you can set it as AcceptButton of the form. For more information see How to: Designate a Windows Forms Button as the Accept Button Using the Designer
About other states
If you take a look at ButtonStandardAdapter Which is responsible to draw a standard button, you will see:
private PushButtonState DetermineState(bool up) {
PushButtonState state = PushButtonState.Normal;
if (!up) {
state = PushButtonState.Pressed;
}
else if (Control.MouseIsOver) {
state = PushButtonState.Hot;
}
else if (!Control.Enabled) {
state = PushButtonState.Disabled;
}
else if (Control.Focused || Control.IsDefault) {
state = PushButtonState.Default;
}
return state;
}
And IsDefault returns true for a button which is set a AcceptButton of a Form.

Related

How to edit the contents of a window from another window in WPF

In my program I have two windows, the first one being my main window with a text box and the second one having an entry field with a button to update the text box in the first window. I'm a beginner in terms of using WPF and coding in C# in general, but is there a way to pass a pointer or reference of my main window to the second window so the second window can edit the text box of my first window? Is that even the right way to think about solving this issue?
WPF assumes you are binding your forms to a ViewModel object. This object can be bound to more than one form to give you different views and capabilities, so in this case you'd bind the same ViewModel to both forms, and what is changed in your edit form will appear automatically in your main form.
Your question is a bit vague and there are many approaches to accomplishing this. MVVM as Steve Todd mentions, is one.
However, it sounds like you simply want to open the window as a dialog. In your second window's code behind, be sure your textbox has a name in XAML and then access it create and easily accessible property that gets and sets your textbox value.
public MyTextContent
{
get => this.MyTextBox.Text;
set => this.MyTextBox.Text = value;
}
You can control the return value based on conditions (such as OK or Cancel buttons) if you like by using click events. The window contains a DialogResult property. The default is false, so you will need to set this somewhere.
this.DialogResult = true; // OK
Then in your main window's code behind, create a new instance of the window, assign it's property and show it. This will need to be done during a click event of a button or some similar trigger
var myDialog = new MyDialogWindow()
{
MyTextContent = "Textbox Starting Value";
}
bool? result = myDialog.ShowDialog(); // Returns when the dialog window is closed.
if(result != null && result)
{
this.LocalTextBox.Text = myDialog.MyTextContent; // Copy the text to the main textbox.
}
Typically you do this in data context of your main window. You use IoC to pass an instance of popup notification service in the constructor and create a private reference. You call that service method that displays the popup notification where user can enter async (and await) for its response or use reactive extensions to subscribe to submit action of that button. A thing to look out for is that you can update ui only in dispatcher thread and do not forget to dispose the subscription after you have finished using the window.

Display Different Windows Within a Panel in a Form (Windows Forms Application)

I am working through an Algorithm book (C#) and am consolidating all of the examples and algorithms into a windows form app. The idea is to have a side menu with buttons to choose which "program" you want to run and to the right of the menu with the buttons the Panel will display the form or user control created for that specific algorithm. For example one of the menu options will be "Area of Parallelogram" when clicked the User Control will show to the right of the selection menu (within a Panel) with the input boxes for height and width, a button to calculate it and an output Textbox for the results. I am kind of stuck on which approach I should use, I tried using multiple Panels layered on top of each other and hide them initially then do a Show() on-click but this proved extremely problematic. I read that using User Controls would allow for better control but am unsure how to proceed. I guess my question is how do I have the User Control display inside the Panel within my Form which displays the programs? I would upload a pic but I do not have the rep.
Rather than instantiate the controls for each application, hide them all, and just show the ones from the selected "application", consider this: design your main selector panel as a placeholder. When the user selects an application, assign the application panel instance in place of the selector panel instance and allow the application to execute to completion. After it exits, restore the placeholder instance.
Bind all the button click event to button1_Click.
Place your panels on top of each other and change the visible properties to false.
Then, use switch-case as below.
Button btn = sender as Button;
switch (btn.Text)
{
case "Triangle":
pnlCircle.Visible = false;
pnlSquare.Visible = false;
pnlTriangle.Visible = true;
break;
case "Square":
pnlCircle.Visible = false;
pnlSquare.Visible = true;
pnlTriangle.Visible = false;
break;
case "Circle":
pnlCircle.Visible = true;
pnlSquare.Visible = false;
pnlTriangle.Visible = false;
break;
}

NullReferenceException Error When Trying to Change the Property IsEnabled of an AppBar Item

On one of my .xaml pages, I have an appbar with a few icons on it.
One of the icons pins the page to Start, so when it is pinned I want to change the IsEnabled property of that icon to false.
However I get this weird error; as described in the title when this procedure is called.
Here's the code:
if (Tile == null) { }
else { appBarPin.IsEnabled = false; }
any ideas?
The behavior with the Application Bar is different to the rest of UI elements. From App bar for Windows Phone:
The app bar doesn’t support some common features of controls, such as
data-binding. As a result, you can’t change the icon button and menu
item text by using Name properties that you set in XAML.
If you want to change a property of the appbar item, do it the following way:
ApplicationBarIconButton button = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
button.IsEnabbled = false;
Replace the 0 with the index of the button. I.e. if the button is the second button of the appbar, the index will be 1.
See more in How to change app bar icon buttons and menu items dynamically for Windows Phone
A null reference exception means that you can't say ".IsEnabled" if the thing before the dot is null.
It appears that appBarPin is null.

hide some button in asp.net 4.0

I want to ask how can I hide some button for different users? For example, an admin can access all buttons, but a regular member cannot access some buttons (e.g. add new user button).
Set the Visible property to true or false depending on your condition (check user),to hide or show.
if(someConditionExpressionHere)
{
btnSave.Visible=true;
}
else
{
btnSave.Visible=false;
}
Assuming btnSave is the Id of the button you want to hide/ show. You should udpdate the someConditionExpressionHere with a check to see whether the user is admin or normal user.

Push Button (On/Off) in C# Windows Forms

On the Windows Form application I have a Lamp image (a black and white one, and a bright one. For OFF and ON respectively).
Using the Button how can I achieve the scenario such that same button will turn the property of the image (pictureBox in my case) to show the Lamp as ON and pressing the same button again will turn the Lamp off.
I am accessing the 'Visible' property of picture box.
Put two images on top of each other and get the button to switch which one of them is enabled.
In the form designer you make one of them visible and the other non-visible. The code in the button handler can then be something like:
lightImage.Visible = !lightImage.Visible;
darkImage.Visible = != lightImage.Visible;
That will swap which one is visible and eliminate the need to keep state elsewhere.
A bit late to the party, but you can use a checkbox and set the appearance to button.
I think that would do what is expected by the original post.
I'm not sure about the way to put 2 images over each other, but if you want to reach the same effect:
place the 2 image files in your project resources
in the click event of the button, toggle the button image depending on a setting:
this would be in the click event:
Properties.Settings.Default.IsOptimizedForTracer !=Properties.Settings.Default.IsOptimizedForTracer;
if (!Properties.Settings.Default.IsOptimizedForTracer)
{
btnOptimizeForTracer.Image = Properties.Resources.TracerOFF;
return;
}
btnOptimizeForTracer.Image = Properties.Resources.TracerON;

Categories

Resources