How to SAVE edited Treeview node text in C# Visual Studio 2010 - c#

I am trying to edit treeview node names in a treeview (they become editable on a button click ) and then I want them to remain saved (if I exit and enter the aplication again, the new, edited names should be displayed), BUT they always revert to the original name(text) on program reentering.
private void button1_Click(object sender, EventArgs e)
{
treeView1.LabelEdit = true;
}
Questions is how to make the new treeview node names be saved after editing, so when I enter the aplication again they don't reset to the old ones.

You can use the event on application shutdown to manually iterate over the TreeView and save the data to a file. Then when you applications starts again read them and populate the TreeView.

You cannot edit your project by modifying the developped program runtime.

Related

Update PDF in windows form without losing focus

I'm developing a PDF creator using Windows Forms application in Visual Studio Community 2017. I'm using MigraDoc to generate PDFs and they are directly created by a series of data inserted by the user in textboxes.
The page for inserting and displaying the data is the same, because I want a real time update of the PDF document preview. To display the PDF I'm using an axAcroPDF element obtained in the COM section after the installation of Adobe Reader.
In order to update the PDF document I have written a class that simply introduces the new elements (I use a Leave event trigger on textboxes to call the update) and load the new PDF file inside the axAcroPDF element:
axAcroPDF1.LoadFile(filename);
The problem is the fact that each time I insert new data in one of the textboxes and the leave event is triggered it is like the entire form is reloaded and I lose focus on the textbox I was writing on (data inserted remains but the focus gets totally lost and the writing cursor is not preserved). This happens to the other textboxes if I click on another one or if I use tab to move to the next one. Notice that the PDF is correctly updated. Is there any method to avoid this problem and update the PDF section without losing focus on textboxes? Is it also possible doing that on a changetext event listener for those textboxes?
Here is an extract of the code use. The function updateText is called when a changetext event is detected on the textbox:
private void updateText(object sender, EventArgs e)
{
updateDocument();
axAcroPDF1.LoadFile(filename);
TextBox s = (TextBox)sender;
s.Focus();
}

How to Show Other Forms Using DevExpress tileItem in tileControl?

I am new in C# and DevExpress. I'm trying to show another form by clicking a tile in the tileControl group but it doesn't show up. I just right-clicked at the tileControl, clicked view code and manually declared this since this doesn't automatically shows up if you double click a tile.
private void addTile_Click(object sender, EventArgs e)
{
var xForm2 = new XtraForm2();
xForm2.Show();
}
I just right-clicked at the tileControl, clicked view code and manually declared this since this doesn't automatically shows up if you double click a tile.
If I understand this correctly, you did not build your project using the C# code listed above, but only edited the source.
That does not work because what you edit in this way is not being loaded into the app.
Of course, I could be misunderstanding what you wrote.

ReportViewer: Two rldc files with same DataSource

I have a report working fine, but now I need develop the same report in other language (english btw).Same layout, same fields.
So I created another .rdlc file and other winform (Maybe it's possible use the same winform, I saw something like that on my research)
My problem is:
After I set all fields and layout, when I select .rdlc file on "choose report" in ReportViewer and do the same way I was doing. Something isn't right because I not getting all BindingSource and all TableAdapter automatically.
On .rdlc file I add all DataSet from DataSource that I was needing.
I Try add manually the TableAdapter from Toolbox because it's showing all components from the other report, but doesn't work.
What is the best way to do something like that? two rdlc file with same data, same DataSource.
I'm thinking create another DataSource file (.xsd).
(sorry my english)
Same data but different languages? Try this:
use a single RDLC file with a boolean parameter like blnEnglishLanguage
for every TextBox used as label set an expression like this: =IIf(Parameters!blnEnghlishLanguage.Value, "Item", "Articolo")
BONUS: give a look at this link to localize your form:
see accepted answer and the answer provided by noelicus.
what i have don is.
you need to set a parent window to IsMdiContainer = true.
and then you can open the windows forms with the report in the same parent.
Remove the form border of the windows form report windows
with the code for the buttons to open it in the mdi Container
awDushiHomesClients OpenawViewClients;
private void ViewClientsMenuB_Click(object sender, EventArgs e)
{
if (OpenawViewClients == null)
{
OpenawViewClients = new awDushiHomesClients();
OpenawViewClients.MdiParent = this;
OpenawViewClients.FormClosed += OpenawViewClients_FormClosed;
OpenawViewClients.Show();
}
else
OpenawViewClients.Activate();
}
void OpenawViewClients_FormClosed(object sender, FormClosedEventArgs e)
{
OpenawViewClients = null;
///throw new NotImplementedException();
}
for the second button use the same code but rename all awDushiHomesClients to lets say awDushiHomesClientsEng."But then your file name"
don't know what kind of information you are showing but if you just need to rename the column text copy and past the first report and rename it.

Tree View / File View Control for C#

I have been looking for a C# tree control for displaying a file system that has the following capabilities:
Select a starting directory. I don't always want to start at a "default" top directory level.
The ability to grab an event when the user double clicks on a file in the tree. I want to handle opening the file within my application.
I have been looking at this C# File Browser. Unfortunately, I have not been able to figure out how to do meet my second need. (If anybody can clear that up for me, I would like that even better.) Thanks for any help.
Hi I've looked at the C# File Browser and Find a way to handle your 2nd requirement. You could try adding ItemActivate event on the fileView control (under Browser User Control of the FileBrowser project) and get the selected item(s) when handling it. ItemActivate event is triggered on every double click of an item. Here is the sample code:
private void fileView_ItemActivate(object sender, EventArgs e)
{
//Loop thru all selected items
foreach (ListViewItem item in ((BrowserListView)sender).SelectedItems)
{
//Do your stuuf here. MessageBox is only used for demo
MessageBox.Show(item.Text);
}
}
Edit by Original Question Writer: To see all of the source, look at the code posted by cipriansteclaru in the comments section of the FileBrowser. You have to actually edit the FileBrowser source to gain this functionality (which is what this answer was demonstrating).

Retaining the changes made to the backcolor property

I am developing a windows application.
I have 3 forms:
I want to change the backcolor of all the 3 forms to the color selected by the user.
I have used the following code I am able to change the backcolor but When I exit the application and restart it I am not able to get the color that user has set. I am getting the default colour only.
Is it possible to retain the colour selected by the user and use it as backcolor when the user restarts the application.
CODE
In Form1
ColorDialog c1 = new ColorDialog();
public static System.Drawing.Color bkc;
private void button1_Click(object sender, EventArgs e)
{
DialogResult res = c1.ShowDialog();
if (res == DialogResult.OK)
{
bkc = c1.Color;
this.BackColor = bkc;
MessageBox.Show(Convert.ToString(bkc));
}
}
private void button2_Click(object sender, EventArgs e)
{
Form2 obj1 = new Form2();
obj1.BackColor = bkc;
obj1.Show();
}
In Form 2
CODE
private void button2_Click(object sender, EventArgs e)
{
Form3 obj1 = new Form3();
obj1.Show();
}
private void Form2_Load(object sender, EventArgs e)
{
this.BackColor = Form1.bkc;
}
In Form3
CODE
private void button2_Click(object sender, EventArgs e)
{
Form1 obj1 = new Form1();
obj1.Show();
}
private void Form3_Load(object sender, EventArgs e)
{
//Form1 obj2 = new Form1();
this.BackColor = Form1.bkc;
}
In the color dialog box I am selecting a color and pressing Ok button the color is also changed but when I restart the application I dont get the colour which I set using the Color Dialog.I want to retain this setting so that the user can get the desired color without resetting it each time the application is executed.
The above code does not generate any error.
can anybody help me out in performing this task?
Thanks in advance!
You will need to save the value somewhere such as the Application.exe.config:
// Open App.Config of executable
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration
(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add("BackgroundColour",
bkc + " ");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
Here is a C# full code example: Using System.Configuration.ConfigurationManager Example
The suggestion of using the application configuration file is close, but there are two things wrong with it.
First, all users of the application share the same application configuration file. If you have multiple users (on a network, say, or different users on the same machine), storing a user's preference in the application configuration file will change that setting for all users. A second thing wrong with it is that under a default installation on Vista it won't work anyway: by default, Vista doesn't give the user write access to anything under the Program Files directory, so saving changes to the application configuration file will throw an exception.
The right answer is to use user settings. These get stored in the application's user settings file, which lives in a (deeply nested, and OS-version-dependent) subdirectory of the user's home directory. The ConfigurationManager loads these settings at runtime, and lets you update and save them in your code. There's an entire infrastructure built into Visual Studio to make this (relatively) easy, which is good, because doing it properly involves writing a spooky amount of code against the ConfigurationManager class. Here's how it works:
If you look under the Properties of your VS project, you'll see an item called Settings.settings. When you double-click on this, it will show you a grid that lets you add settings to your project. You give the setting name, choose its the data type and default value, and, crucially, the scope. The setting can be application scope, in which case its value will be common to all users of the application and be stored in the application configuration file. Or it can be user scope, in which case each user can have his own value for the setting, and the setting will live in the user settings file.
When you add a setting to this grid, VS generates code to make the setting available to your code. Basically, it creates a class that exposes these settings to your code as properties of a singleton object. (You can see this code if you want to get an idea of what this is saving you from having to do yourself; it gets stored in the 'Settings.Designer.cs' file created under 'Settings.settings' in the project view.) It also, conveniently, regenerates this class every time you change the information in the Settings grid. Once you create a setting in the settings grid, you can reference it in your code thusly:
ctl.BackColor = Properties.Settings.Default.BackColor;
User settings can be modified by your code:
Properties.Settings.Default.BackColor = newBackColor;
And you can save them to the user settings file like this:
Properties.Settings.Default.Save();
Having these settings being exposed as properties of a class is useful for a lot of reasons. One of the most important is that since they're properties (and not, say, dictionary entries accessed by a key, which is how most code that people write against the ConfigurationManager class works), there's compile-time checking of the names you're using in code. You're not ever going to get a NullReferenceException at runtime if you misspell the name of a setting; you'll get an error when you compile it instead.
There are a few subtleties to using user settings. One of the less obvious ones is: what happens when you produce a new release of the software? The user settings are stored in a directory that's keyed to the version number of the program; if you release a new version, the user settings file for it won't exist. How do you keep the user from losing all of his settings when he upgrades your program?
This is also built in to that Settings class; all you need to do is this:
if (Properties.Settings.Default.UpgradeSettings)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpgradeSettings = false;
}
This will copy the user's settings from the previous release into the settings file for the new release.
Why dont you create an event that all three forms listen to and get them to change the background colour when listening to the "change colour" event? And you could store the colour in a static variable so that when the form gets loaded, the background colour could be set to that stored in the variable.
In order for the screen to remember the colour settings, why not store the colour selected in a user preferences file? Try the "IsolatedStorage" functionality to save a preferences file.
You are doing it wrong way.
How will the application remember the user choice of backcolor?
The app runs in memory & shows chosen backcolor till its terminated.
Read on this & take it forward.
EDIT: Also, it is not right thing to use Form1.BackColor in Form2.
Open Form1, change backcolor, close Form1 & open Form2 to see what happens (you might see that Form1 opens again).

Categories