How do I apply the Settings.settings values at design time? - c#

I have a panel in my Form like this:
and a Panel_BackColor in project's Settings.setting file:
I can change panel back color in the Form constructor:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
panel1.BackColor = UI_Settings.Default.Panel_BackColor;
}
}
All things work at runtime:
But nothing change at Design Time. How can I apply these settings at Design mode too?

I got your question, I try to handle with it when I use MetroFramework. Some changes just shown in runtime because it use another drawing technic with xml or .netframework when you use runtime code. So, I think you can't see changes in design time.

Next time try to explain a little more in details or maybe add some code or images to make us understand better.
In C# you have the property:
//Namespace: System.ComponentModel
//Gets a value that indicates whether the Component is currently in design mode.
//true if the Component is in design mode; otherwise, false.
protected bool DesignMode { get; }

I asked "not edited version" of this question on MSDN forum and got an answer within an hour.
Questions like Convert int to string? is a good questions but mine is not!
I think stackoverflow should keep a watch on it's editors and policy.
The answer:
Select your control in Form Designer (for example a button), go to Properties, (ApplicationSettings),(PropertyBinding), then bind BackColor or other property to Button_BackColor or other settings. Afterward by changing settings in Settings.settings file, all binded controls would be affected.

Related

c# How to link combobox SelectedIndex to ApplicationSettings

In Visual Studio designer (I use 2012) there is no way to link ApplicationSettings mechanism to SelectedIndex property (only Text).
What workaround can be used instead?
Sorry if my question was against StackOverflow rules. I searched for my question and found no exact answers.
I don't know only how to custom code ApplicationSettings. I always used this feature in design mode - linked Text properties of my textboxes and then used Properties.Settings.Default.Save() to save this and on Form_Load I used Properties.Settings.Default.SomeName to load saved values.
Everything besides this was done by VisualStudio and I don't know what exactly to change its behaviour for my needs.
I was sure that this question would be useful for starting programers
Write some code, it is as easy as using designer, besides, designer generates too much code which you don't understand.
Edit Settings in Visual Studio, add a property named "SelectedIndex", set its Type as int, and Scope as User, Value as 0 (meaning the 1st item is selected). And you can access this property in your code:
public Form1()
{
InitializeComponent();
comboxBox1.SelectedIndex = Properties.Settings.Default.SelectedIndex;
this.Closing += Form1_Closing;
}
void Form1_Closing(object sender, CancelEventArgs e)
{
Properties.Settings.Default.SelectedIndex = comboxBox1.SelectedIndex;
Properties.Settings.Default.Save();
}

Windows Store Universal App - Dependency Properties

actually i am developing a universal app for Windows and Windows Phone.
Within that app i am using dependency properties to assign data to properties of custom user controls in XAML. The problem is that the compiler says he is not able to find the property were i would like to bind data to.
The button definition looks like the following.
public sealed partial class RulerButton : UserControl
{
public static readonly DependencyProperty BackendProperty =
DependencyProperty.Register("Backend", typeof(GlobalData),
typeof(RulerButton), new PropertyMetadata(null));
public GlobalData Backend
{
get { return (GlobalData)GetValue(BackendProperty); }
set { SetValue(BackendProperty, value); }
}
public RulerButton()
{
this.InitializeComponent();
}
}
I use this button within my main page like this.
<btn:RulerButton Backend="{Binding ElementName=root, Path=BackendSource}"/>
Then the compiler says Unknown Member "Backend" in Element "RulerButton".
If have used the same approach earlier in other projects (Normal WPF-Windows applications) and it worked well. So i think the problem is related to universal app projects.
All other questions i have found could not solve my problem.
I would be appreciated if someone could give me a hint.
greets
Edit
Well i actually have solved the problem. But i think it is very curious. Beside the RulerButton i have many other controls where i added the same dependency properties. On some of them i did not add the normal property Backend yet. Cause i first wanted to make the RulerButton work. After completing these controls so that each of them has a BackendProperty and Backend attribute, all errors were gone.
anyway thanks for your help guys
Well i actually have solved the problem. But i think it is very curious. Beside the RulerButton i have many other controls where i added the same dependency properties. On some of them i did not add the normal property Backend yet. Cause i first wanted to make the RulerButton work. After completing these controls so that each of them has a BackendProperty and Backend attribute, all errors were gone.

Best controls to use on a settings form?

I'm just designing a settings form for my app, nothing too fancy, It's a small app. I'm just wondering what controls are good for this? at the moment I'm using labels and textboxes and listboxes, but It's just looking crap and awkward, Is there some orthodox way to do this? Is is more horrible attempt at UI design.
You can produce some really intuitive, aesthetic interfaces with the most primative controls and a dash of creativity or inspiration. Here's a settings UI I created recently:
General Tips
A nice choice of font goes a long way. My personal favorite at the moment is Segoe UI.
Position controls using the designer rulers so that they are all relative of one another. Be consistent with your positioning too; don't position a caption above a TextBox for one field and besides the TextBox for another.
Try and find a nice size for your controls, small controls are hard to use and large controls look very amateur.
I think that setting the Form's Maximize property to false and changing the FormBorderStyle property to FixedSingle works well with small, fixed forms.
It's a good idea to get inspired. Take a look at some other projects of a similar caliber to yours and see how they position their UI.
For something quick-and-dirty, create a class that is a Model of your application's settings, like so:
public class ApplicationSettings {
public String YourName { get; set; }
public Color BackgroundColor { get; set; }
}
Then use that with a PropertyGrid control:
ApplicationSettings settings = new ApplicationSettings();
LoadSettingsFromDB( settings );
propertyGrid.SelectedObject = settings;
After the user hits OK, just persist. The property values of your settings instance will be updated.
You can also use the PropertyGrid with the VS IDE-generated AppSettings feature.

Drag and drop the user control won't work in .net windows project

I created a user control and it shows up on the tool box as form components. Then when I try to drag and drop the user control on to a form , I get this visual studio error.
" The specified named connection is either not found in the configuration ,not intended to be used with the entity client provider or not valid."
Why am I getting this error?
But some other user controls I can drag and drop which are under the same project. I don't know what I missed in creating this user control.
Beware that code in the UserControl class runs at design time. The constructor, the OnLoad method and Load event. But also methods like OnPaint(). If this code does anything that depends on the environment being setup properly, that code is liable to throw an exception and cause the designer to change its mind about adding the control to the form. That certainly seems to be the case when you get a "not found in the configuration" error, there is no configuration file yet.
Use the DesignMode properly to skip such code. Like this:
protected override void OnLoad(EventArgs e) {
if (!this.DesignMode) {
// Do stuff...
}
base.OnLoad(e);
}
As Hans says, you might need to use the DesignMode property in the Constructor or OnLoad. Also, make sure any public properties that use the connection have this attribute:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string Foo
{
get;
set;
}
That way, the designer won't attempt to set them when you add the control to a form. This is always a good habit to get into anyway for properties that you won't be setting at design time.
this error show if you put the code of loading data from database into the constructor of userControl.
"loading data or initialize entity framework"
so the solution is to move the code of loading data from constructor to a method. you can call it "loadData".
and call this method "loadData" in the constructor of the parent form

How to distinguish between User Control load on form and load when runtime

I created a user control using C# for windows form application. This user control has some properties. In runtime, if the user does not enter values for this properties I want to show a message box and exit the application.
The problem is when I write the checking code in the Load event of User Control. When I drag & drop it on the form the message box will appear.
private void UserControl1_Load(Object sender, EventArgs e)
{
if (_getFirstPageArgument==null || _getFirstPageArgument.Length==0)
{
throw new Exception("Some Message");
}
}
How do I distinguish between load on the form and load on run time?
I fear there is a larger problem here. But to solve your immediate problem (if I understand correctly...) There is a form attribute called DesignMode. When you are in the visual studio design mode, this will be true. At runtime, this will be false.
For beginners, #Nimas case can be a good study point to understand that Visual Studio actually runs and executes parts of our code even when we are in design time, which is why the constructor is invoked. Even "DesignMode" property is not 100% reliable. You can find an interesting note here related to that http://weblogs.asp.net/fmarguerie/archive/2005/03/23/395658.aspx
If you only want to know when the type itself has been loaded into the runtime (not a specific instance), you can put code into the static constructor for that class.
If I'm misinterpreting your question, please clarify using a timeline when you want specific events to happen.

Categories

Resources