c# How to link combobox SelectedIndex to ApplicationSettings - c#

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();
}

Related

Checkbox save state using settings not working (winforms C#)

As you can see below, save code.
No errors at all.
Image
But when I change the state of the checkbox and close the form, the setting does not update and remains false.
Yes I do have something that uses the setting, and that works normally.
I am not following what the problem is. How are you “checking” if the setting is saved? In other words, I see nothing in your current code that actually USES the setting. I suggest you grab and apply the Properties CheckBox setting value to the CheckBox ExampleToggle when the form loads. Something like…
private void Form1_Load(object sender, EventArgs e) {
ExampleToggle.Checked = Properties.Settings.Default.CheckBox;
}
Also, in the future, posting pictures of code is frowned upon. You are forcing others who may help you into typing the code themselves. Many will just move on if they have to type the code.

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

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.

Can I stop Visual Studio auto-changing the form designer code?

I'm using visual studio and have some numericUpDown controls that I have set values for (min, max, value and increment). The Code Designer keeps turning my simple decimal values into int[]. I know that it says "do not modify the contents of this method with the code editor" but I find it really useful to adjust properties in here and I thought I was just "making things neat" when I changed:
this.numericUpDown1.Increment = new decimal(new int[] {
2,
0,
0,
0});
to
this.numericUpDown1.Increment = new decimal(2);
but alas, it changes it back if I touch the control on the visual form designer. I thought maybe there was a flag that would leave it as is until I want it to be updated. It is more for readability and navigation but even if it would just leave it on one line I'd be happier.
I've found this (How can I tell Visual Studio to not populate a field in the designer code?) where someone was trying to leave it but I'm not sure if it is applicable in this instance.
Feel free to tell me that I should just get over it and leave it alone!
Just realised (thanks #Jonathan) that I could just create a separate section in the designer ABOVE that place ... this means I can have all my default values in a separate section as long as I don't want to changed the defaults on the form designer. This is the method above InitializeComponent()
private void IntializeOtherComponents()
{
this.numericUpDown1.Value = new decimal(17);
this.numericUpDown1.Maximum = new decimal(7777);
this.numericUpDown1.Minimum = new decimal(5);
}
You could make your own NumericUpDown user control. Just inherit from the common NumeriUpDown and set the dessired properties in the constructor.
public partial class MyUpDownCtrl : NumericUpDown
{
public MyUpDownCtrl()
{
InitializeComponent();
this.Increment = new decimal(2);
}
}
After generating your solution you will have your new custom UpDownControl in your Tool Box ready to use just dragging it like the common numericUpDown.
I hope it help you, because I don't know any way to make Visual Studio stop auto-changing the form designer code. Maybe it can't be done at all.

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.

Unable to Affect Scintilla Control Programmatically at Runtime

I'm attempting to use the ScintillaNET control in an application I am working on. I drag and drop the control into my form and run form. The control appears on the form. This is good. In addition, if I set any of the properties in the control's properties editor (ConfigurationManager.Language, for example), I am able to type in that language and see syntax highlighting occur.
Where I run into problems is when I attempt to change properties programmatically. For example, I attempt to load text from a file into the form (I'm doing this in the form's Load). The text doesn't display. I also can't seem to show the line numbers or do any other number of tasks (including programmatically change the Language).
Any idea what I may be doing wrong? Even something as simple as the code below doesn't seem to work:
private void scintilla1_Load(object sender, EventArgs e)
{
scintilla1.ConfigurationManager.Language = "xml";
}
Simply add scintilla1.ConfigurationManager.Configure();
private void scintilla1_Load(object sender, EventArgs e)
{
scintilla1.ConfigurationManager.Language = "xml";
scintilla1.ConfigurationManager.Configure();
}
After spending some time playing around with the different events, it appears that I cannot affect the Scintilla control until after it is already visible. Hence, the "Load" event does not let me make any programmatic changes to the control until I've set it visible.
It's a little strange, and seems sort of pointless to me to have the Load event at all, but I just wanted to let everybody know what is happening in case someone else ran into the same problem.

Categories

Resources