It's possible to bind a property to an existing application setting using the designer, that way I don't have to write something like
textBox.Text = Settings.Default.Name;
when initializing my form.
Now it looks like the binding stops there, ie if I change the value of my setting, it won't update the value in my textbox and vice versa.
Is it normal or am I missing something?
I'll explain how I do it on windows forms with Visual Studio 2010. It is likely similar in newer versions.
Click on the component to bind (textbox, checkbox, ...)
Make component's properties panel visible
Expand the node ApplicationSettings
Click the ... button to the right of (PropertyBinding)
Choose which setting you want the control bound to.
There is a more detailed tutorial at Exploring Secrets of Persistent Application Settings
Well, it depends.
First, I'm not sure if you're talking WPF or Windows Forms, so I'll assume neither.
Second, you are not "binding" anything. You are taking the value of Name and setting the property Text equal to this value. You are setting a property. This does not come with any magic side-effects which inextricably links the Name property to the Text property.
Third, you can change settings, but until you save them, they are not written back to your app.config. In a Windows Forms app, you'd have to do something like this:
// event handler for the Form.Closed event.
// this.FormClosed += FormClosed;
void FormClosed(object sender, FormClosedEventArgs e)
{
Settings.Default.Name = textBox.Text;
Settings.Default.Save();
}
In WPF, you'd use the normal binding semantics (which means you avoid the hassle of setting all the property values when closing), but you still have to trap for the form closing so you
can Save() the settings.
Binding:
<TextBox
xmlns:lol="clr-namespace:MyApplication.Settings"
Text="{Binding Name, Source={x:Static lol:Default}}" />
The Save() call happens much as with the Forms example, but you don't have to do anything other than call Save().
Related
I am currently using windows forms application in visual studio. I want to change button and textbox properties programmatically and not use the the properties tab. How do i do this? Is there a way to access the code of the UI of that button/Textbox after it is changed in the properties tab?
Of course you can change that programmatically. If you have for example a button called btnStart, then you have in your form access to all properties:
btnStart.Text = "start";
Have also a look at: Change properties of programmatically created buttons
EDIT:
If you change it programmatically after the InitializeComponent(); it will override changed properties set manually in the properties tab.
Yes. And in fact it's always done in code -- Properties window (i.e. VS winforms designer) just writes some code for you. You can see that code when you delve into the InitializaComponent() method call in the form's constructor (right click InitializeComporent and select "Go to definition").
Anytime after this InitializeComponent() call, you can add code to change what you want:
public Form1()
{
InitializeComponent();
button1.Text = "Go!";
}
Actually on a WinForms project (VS 2010, C#, .NET 4.5), I build a matrix (10x10) in a form. Each element is represented by a control that can be "checked" or "unchecked". Each element's state is completely independent of another.
The ideal solution would be to use a CheckBox. Unfortunately, the client wants a different appearance - for example, the one of the RadioButton. I could use RadioButton, place every one of them in a dedicated group, add an event listener to uncheck on-click if the element is already checked... Pretty much for a question of appearance!
Couldn't I override some rendering methods of CheckBox class? (Anyway, that's the only place in the whole application where I use this control)
Note: Controls are instanciated on runtime from a class MyClass:CheckBox.
Use a RadioButton if you need to make it look like one. You'll need to set their AutoCheck property to False to make them act like check boxes and give them a common event handler:
private void radioButtons_Click(object sender, EventArgs e) {
var button = (RadioButton)sender;
button.Checked = !button.Checked;
}
Never hesitate to point out that this is very poor UI design.
I have a C# Dialog based app. I want to save the preferences/settings the user choose, so that I could reload them in the next run.
I am new to C#, may be this is something quite basic but I do not know.
Do I have to explicitly write them to a file like ini or something ? or is there a built in way to do that.
The kind of config data is like checkboxes selelected, numericUpDOwn, checkedListbox - checked items etc
Select the control in the designer. Scroll all the way up in the Properties window and expand (ApplicationSettings). Click the indicated button to open a dialog. Select the property whose value should be persisted (like Checked for a check box) and click New in the dropdown.
Be a bit careful, not all properties are suitable to be persisted like this. An example is the form's Size. You don't want to store the size when the form is minimized or maximized, that size won't restore well. You need to do this by adding the setting in the Settings designer an only write it when the control is in the right state. In the case of Size, that's when the Resize event runs and the WindowState is Normal.
After you create the application settings as the other answers suggest, make sure you don't forget to call Properties.Settings.Default.Save(), for example:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Properties.Settings.Default.Save()
}
To Create a New Setting at Design Time
In Solution Explorer, expand the Properties node of your project.
In Solution Explorer, double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.
In the Settings designer, set the Name, Type, Scope, and Value for your setting. Each row represents a single settings
For more info you can refer here
You should use application settings. These will persist their values after you close your application, and you will be able to read from them when the program starts back up again.
Im developing an application in C# under WPF.I want to change the status of the check box and also i need to change the text block's value of already opened window from the currently working windows operation and to update that opened window with these changes(Refresh the already opened window with some updates).
In order to control UI elements from code behind you need to assign a name to each UI element you would like to control.
As for check box decleared as
<CheckBox Name="chkA"> Checkbox A </CheckBox>
you can change its' checked state from code-behind via
chkA.IsChecked = true;
As for the diffenet window update - your Windows in WPF are just classes, part of which usually lives in *.xaml file, and another in the corresponding *.cs file.
If you declare a public method that refreshed the windows content's as you want in your second windows' class, and, when you will be creating your second window, you somehow save the reference to its' instance available in a first class (or some other logic in your application), you will be able to simply call that method from windows' 1 code to refresh the second widows appearance, as declared in a method called.
Basically, from Windows1 you call:
MySecondWindow secW = new MySecondWindow();
secW.Show();
....
secW.RefreshWithMyChages();
RefreshWithMyChages() is just a public method in your second windows' class codebehind.
All of this hold true if:
both of your windows are in the same project
you are not willing to use MVVM or other UI-patterns.
I have a very annoying problem I'm trying to solve for couple of weeks. I have a WinForms C# project where I developed my custom control (ListView + ToolStrip with ToolStripButtons). This control is used in different forms inside solution - but in other projects. For different forms I need to make certain buttons visible or hidden, so I have added to my control corresponding properties like
public Boolean DeleteButtonVisible
{
get
{
return tsbDelete.Visible;
}
set
{
tsbDelete.Visible = value;
}
}
Some buttons are visible by default, some are hidden. In designer when editing a form with my control I'm able to change those properties, buttons on control become visible or hidden as they should. But every time I'm changing anything in my control source file in all forms those properties are reset to default values regardless of what I have set in designer and I have to restore those values manually. Well, I'm using a source control so this is not that hard, but performing "Undo" on a couple dozen of files every time I change a bit in another file is a damn disaster.
I have tried to use [DesignerSerializationVisibility] attribute to fix this issue. If I used it with value "Hidden" it didn't do any good at all - values were just not saved. "Content" made buttons randomly disappear even if by default they were visible. "Visible" lead to no effect, as this is default value...
I don't want to set every button visibility for every form in my code - this is just not the way it should be done.
Does anyone know something about this?
Yes, the Control.Visible property is special. The getter does not return the last assigned value, it only returns true when the control is actually visible. That can have side-effects, you've found one. In this case probably induced when the control switches out of design mode. To do this correctly, you must store the assigned state in a backing variable. Like this:
private bool tsbDeleteVisible;
public bool DeleteButtonVisible {
get { return tsbDeleteVisible; }
set { tsbDelete.Visible = tsbDeleteVisible = value; }
}
Be sure to initialize the default value of the backing variable to the default value of tsbDelete.Visible. Use the constructor to be sure.