I am currently looking at a better approach to remember/persist previous state of the controls on a .NET Windows Form.
For example, there are 5 drop down list menu controls on a windows form. And user previously selected some items in these drop down menu. What I'd like to do here is: when this WinForm is loaded again, user's previous selections shall be recovered and remain the same.
For now, I can kinda think of a solution: store each selected value/index in a text file or registry key or something. And then read them every time the From is loaded.
But the thing is this approach would become inefficient to deal with a large number of controls and maintain their states.
So can anyone give me some thoughts or suggestions? What'd be the best way to do achieve it?
EDIT:
I just had a read about this article on MSDN, and this concerns me because I am doing the add-in project at the moment:
You cannot use application settings in an unmanaged application that
hosts the .NET Framework. Settings will not work in such environments
as Visual Studio add-ins, C++ for Microsoft Office, control hosting in
Internet Explorer, or Microsoft Outlook add-ins and projects.
You might want to look at Settings files
Saving User Settings
private void UserSettingsDemo_Load(object sender, EventArgs e)
{
txtServer.Text = Settings.Default.ServerNameSetting;
txtDatabase.Text = Settings.Default.DBNameSetting;
txtPassword.Text = Settings.Default.PasswordSetting;
txtUserId.Text = Settings.Default.UserIdSetting;
}
private void UserSettingsDemo_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.Default.DBNameSetting = txtDatabase.Text;
Settings.Default.UserIdSetting = txtUserId.Text;
Settings.Default.PasswordSetting = txtPassword.Text;
Settings.Default.ServerNameSetting = txtServer.Text;
Settings.Default.Save();
}
Are you talking about keeping the previous state of a control after restarting the application? In that case, you probably won't have no choice than writing all the changes down in an configuration file for example.
If you are trying to navigate between different winforms and want to persist the changes, you could implement an history of controls. A datastructur like a stack should do the trick, with the following methods:
AddToHistory(Control control)
RetrieveLastOpen()
As far as you know what exactly you want to save it is ok to use smth like
(foreach var child in MainForm.Children.OfType<ComboBox>)
{
// Save properties of child into Dictionary<string, ComboBoxProperties>
}
and for loading you will do smth like this
(foreach var child in MainForm.Children.OfType<ComboBox>)
{
// Load properties of child from dictionary[child.Name]
}
Related
I am designing an application using RibbonReportDesigner, which will let the user to create his own report template. That app will be a part of bigger application.
When user will save template I need to obtain result of Command Property (it is set on the button in designer as "SaveFile"). Why do I need this? I want to check if user really saved that template- if yes then i have to save report name to database. One of the problems is that the event of button click is executed before the dialog opening and i don't know how to check result of saving template.
How can I achieve this? I don't see in designer any appropiate events, which could be usefull for my purposes.
Code, which I use to fire event of saving file:
private void commandBarItem32_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var result = commandBarItem32.Command.Equals(DialogResult.OK);
if (result == true)
{
//create object- report name etc. and save it to database
}
}
The reason I need to save it is that in bigger application there will be combobox with a list of created templates.
You dont need to hook all UI events,
it is much easier to just replace the ReportStorage (list, save, load report layouts)
ReportStorageExtension.RegisterExtensionGlobal(new MyCustomStorage());
where MyCustomStorage implements all CRUD methods to manage the layouts.
For more details you can follow the documentation
My goal
I am working on a project in C# using Visual Studio 2013. The project is one that I intend to contain a lot of pages. These pages are all linked together using buttons. My problem is that I cannot come up with an efficient and elegant solution for this.
My attempts
So far I have came up with two potenial solutions to my problem. First I added extra forms and then on button press I hid the current form and displayed the new form, like so:
Form2 frm = new Form2();
frm.Show();`
Form1.Hide();
While this does work, I have two problems with it.
My project will end up with hundreds of forms
The transition between forms looks sloppy. I am aiming for a browser like transition by where all navigation occurs on one window, without opening and closing others.
The second potential solution I tried incorporated the use of Panels. So I essentially created each page on a different Panel. Then the appropriate panel was shown upon a button press and the rest were hidden. Like this:
private void button1_Click(object sender, EventArgs e)
{
mainMenuPanel.Hide();
submenuPanel1.Show();
submenuPanel2.Hide();
submenuPanel3.Hide();
submenuPanel4.Hide();
}
This is exactly what I was looking for however my issue with it is that managing the vast amount of panels quickly became a nightmare. Editing the controls on a Panel that was hidden behind 9 other Panels and as the number of panels in my project was only going to grow - this does not seem like the ideal solution in its current form.
In my head I thought there maybe an option in Visual Studio 2013 that allows me to 'hide' the Panels I am not using on the form, or drag them off the form temporarily. Is that an option in Visual Studio.
If not do any of you know a more efficient and manageable way of achieving this?
Thanks in advance.
If you are stuck using WinForms, your best bet is probably using UserControls. you can actually extend the UserControl class out to be a "page" ie: UserControlPage. This makes the form much simpler in function, but you will need to do some finicky work with handling events /passing data if the controls need to talk to each other.
if you aren't nailed into using Winforms, WPF supports all of this natively, and has wonderful tools for building all the pages you would need, and storing/populating your data, and propagating events.
If you want to have single form with changing content, and you don't want to mess up with panels in one form, then solution is user controls. You will be able to create them dynamically and add to form controls. Also there is no mess, because your form will be very simple - you can have single 'placeholder' control which will be used to dock user control which is currently displayed (e.g. panel control):
private void ShowContent(Control content)
{
placeHolderPanel.Controls.Clear(); // clear current content
placeHolderPanel.Controls.Add(content); // add new
content.Dock = DockStyle.Fill; // fill placeholder area
}
Usage:
private void button1_Click(object sender, EventArgs e)
{
ShowContent(new FooUserControl());
}
You could subclass the Panel class and create as many of those custom panels as needed, then they would be inserted on your Main Form, and managed as you described.
The advantage is that you would be able to individually edit them as a separate user control.
The drawback is that you lose direct event handling of controls on those panels from the main form. You can still define your own events on those panels and delegate the individual control events.
There's always a trade-off somewhere.
Cheers
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.
The AxAcroPDF swallows all key-related events as soon as it gets focus, including shortcuts, key presses, etc. I added a message filter, and it doesn't get any key-related messages either. It's a COM component, could that be relevant?
Is there any way to catch these before the control starts swallowing them?
Hans is correct, the Acrobat Reader spawns two child AcroRd32 processes which you have no direct access to from within your managed code.
I have experimented with this and you have three viable options:
You can create a global system hook, and then look for and filter out / respond to WM_SETFOCUS messages sent to your child AcroRd32 windows. You can accomplish some of this from within C# by using a wrapper library, such as the one here: http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
You also need to identify the correct processes as there may be more than one instance of your application, or other instances of AcroRd32. This is the most deterministic solution, but because your application will now be filtering messages sent to every single window in existence, I generally don't recommend this approach because then your program could negatively affect system stability.
Find an alternate PDF viewing control. See this answer for a few commercial components: .net PDF Viewer control , or roll your own: http://www.codeproject.com/KB/applications/PDFViewerControl.aspx
Find an acceptable hack. Depending on how robust your application needs to be, code such as the following may be suitable (it was suitable for my case):
DateTime _lastRenav = DateTime.MinValue;
public Form1()
{
InitializeComponent();
listBox1.LostFocus += new EventHandler(listBox1_LostFocus);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axAcroPDF1.src = "sample.pdf"; //this will cause adobe to take away the focus
_lastRenav = DateTime.Now;
}
void listBox1_LostFocus(object sender, EventArgs e)
{
//restores focus if it were the result of a listbox navigation
if ((DateTime.Now - _lastRenav).TotalSeconds < 1)
listBox1.Focus();
}
I might finally have a ridiculously simple answer. So far in testing this is working.
Having suffered from this problem for quite some time and having built a complex system of each custom control recording which of them last had focus and using a timer to flip focus back (when acropdf grabbed it) I revisited this problem and read a great number of answers (looking for recent solutions). The information gleaned helped me with the idea.
The idea is to disable the (acropdf) control whilst it is loading as in the following example (code reduced for clarity)
AxAcroPDF_this.Enabled = False
AxAcroPDF_this.src = m_src
Then on a timer, after say 1 second.
AxAcroPDF_this.Enabled = True
Basically the idea is to tell Windows not to let users use the acropdf control until allowed, so asking Windows to prevent it from getting focus (because users are not allowed in there).
So far this is holding up, I will edit this if anything changes. If it doesn't work completely for you then maybe the idea points into a useful direction.
It is an out-of-process COM component, that's the problem. Completely in violation of Windows SDK requirements as laid out in SetParent(). Once its window gets the focus, the message loop in the acroread.exe process gets all the messages, your message filter cannot see any messages anymore.
Technically it is fixable by using SetWindowsHookEx() to inject a DLL into the process and monitor messages with WH_GETMESSAGE. But you can't write such a DLL in the C# language.
Major suck, I know. There never seems to be any lack of it with that program.
For some reason Tim's answer, disabling the AxAcroPDF control directly, didn't work in my case. The Leave event on the previously-selected Textbox would never fire, either.
What is working is nesting the AxAcroPDF control inside of a disabled GroupBox. Since the users of my application need to only see the PDF, not interact with it, the GroupBox's Enabled property is set to False in the designer.
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.