Skip to content

Commit 07f0a37

Browse files
authored
Added better save behavior (#6)
1 parent b756220 commit 07f0a37

File tree

3 files changed

+231
-141
lines changed

3 files changed

+231
-141
lines changed

FastColoredTextBox/FastColoredTextBox.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,13 @@ protected virtual void OnCharSizeChanged()
20282028
[Description("Occurs when custom wordwrap is needed.")]
20292029
public event EventHandler<WordWrapNeededEventArgs> WordWrapNeeded;
20302030

2031+
/// <summary>
2032+
/// Occurs when a file is saved
2033+
/// </summary>
2034+
[Browsable(true)]
2035+
[Description("Occurs when a file is saved.")]
2036+
public event EventHandler<FileSavedEventArgs> FileSaved;
2037+
20312038

20322039
/// <summary>
20332040
/// Returns list of styles of given place
@@ -2648,6 +2655,58 @@ public virtual void Paste()
26482655
InsertText(text);
26492656
}
26502657

2658+
/// <summary>
2659+
/// Save given text to file path.
2660+
/// </summary>
2661+
/// <param name="text"></param>
2662+
/// <returns></returns>
2663+
public bool Save(string text)
2664+
{
2665+
if (Tag == null)
2666+
{
2667+
return SaveAs(text);
2668+
}
2669+
2670+
File.WriteAllText((string)Tag, text);
2671+
2672+
/* Raise file saved event for parent to handle */
2673+
FileSaved(this, new FileSavedEventArgs(true));
2674+
return true;
2675+
}
2676+
2677+
public bool SaveAs(string text)
2678+
{
2679+
SaveFileDialog dialog = new SaveFileDialog();
2680+
2681+
dialog.Filter = "Normal text file (*.txt)|*.txt|"
2682+
+ "C# source file (*.cs)" + "|*.cs|"
2683+
+ "Hyper Text Markup Language File (*.html)" + "|*.html|"
2684+
+ "Javascript source file (*.js)" + "|*.js|"
2685+
+ "JSON file (*.json)" + "|*.json|"
2686+
+ "Lua source file (*.lua)" + "|*.lua|"
2687+
+ "PHP file (*.php)" + "|*.php|"
2688+
+ "Structured Query Language file (*.sql)" + "|*.sql|"
2689+
+ "Visual Basic file (*.vb)" + "|*.vb|"
2690+
+ "VBScript file (*.vbs)" + "|*.vbs|"
2691+
+ "JSON file (*.json)" + "|*.json|"
2692+
+ "Windows Batch file (*.bat)" + "|*.bat|"
2693+
+ "Assembly Program file (*.asm)" + "|*.asm|"
2694+
+ "All files (*.*)" + "|*.*";
2695+
2696+
if (dialog.ShowDialog() != DialogResult.OK)
2697+
{
2698+
FileSaved(this, new FileSavedEventArgs(false));
2699+
return false;
2700+
}
2701+
Tag = dialog.FileName;
2702+
2703+
File.WriteAllText((string)Tag, text);
2704+
2705+
/* Raise file saved event for parent to handle */
2706+
FileSaved(this, new FileSavedEventArgs(true));
2707+
return true;
2708+
}
2709+
26512710
/// <summary>
26522711
/// Select all chars of text
26532712
/// </summary>
@@ -3693,6 +3752,7 @@ private void DoAction(FCTBAction action)
36933752
break;
36943753

36953754
case FCTBAction.Save:
3755+
Save(Text);
36963756
break;
36973757

36983758
case FCTBAction.SelectAll:
Lines changed: 142 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,149 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup>
4-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6-
<ProductVersion>9.0.21022</ProductVersion>
7-
<SchemaVersion>2.0</SchemaVersion>
8-
<ProjectGuid>{6DD14A85-CCFC-4774-BD26-0F5772512319}</ProjectGuid>
9-
<OutputType>Library</OutputType>
10-
<AppDesignerFolder>Properties</AppDesignerFolder>
11-
<RootNamespace>FastColoredTextBoxNS</RootNamespace>
12-
<AssemblyName>FastColoredTextBox</AssemblyName>
13-
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
14-
<FileAlignment>512</FileAlignment>
15-
<TargetFrameworkProfile />
16-
</PropertyGroup>
17-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18-
<DebugSymbols>true</DebugSymbols>
19-
<DebugType>full</DebugType>
20-
<Optimize>false</Optimize>
21-
<OutputPath>bin\Debug\</OutputPath>
22-
<DefineConstants>TRACE;DEBUG</DefineConstants>
23-
<ErrorReport>prompt</ErrorReport>
24-
<WarningLevel>4</WarningLevel>
25-
<DocumentationFile>bin\Debug\FastColoredTextBox.XML</DocumentationFile>
26-
</PropertyGroup>
27-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28-
<DebugType>pdbonly</DebugType>
29-
<Optimize>true</Optimize>
30-
<OutputPath>bin\Release\</OutputPath>
31-
<DefineConstants>TRACE</DefineConstants>
32-
<ErrorReport>prompt</ErrorReport>
33-
<WarningLevel>4</WarningLevel>
34-
</PropertyGroup>
35-
<PropertyGroup>
36-
<SignAssembly>true</SignAssembly>
37-
</PropertyGroup>
38-
<PropertyGroup>
39-
<AssemblyOriginatorKeyFile>FCTB_key.snk</AssemblyOriginatorKeyFile>
40-
</PropertyGroup>
41-
<ItemGroup>
42-
<Reference Include="System" />
43-
<Reference Include="System.Data" />
44-
<Reference Include="System.Design" />
45-
<Reference Include="System.Drawing" />
46-
<Reference Include="System.Windows.Forms" />
47-
<Reference Include="System.XML" />
48-
</ItemGroup>
49-
<ItemGroup>
50-
<Compile Include="AutocompleteItem.cs" />
51-
<Compile Include="AutocompleteMenu.cs">
52-
<SubType>Component</SubType>
53-
</Compile>
54-
<Compile Include="Bookmarks.cs" />
55-
<Compile Include="Char.cs" />
56-
<Compile Include="DocumentMap.cs">
57-
<SubType>Component</SubType>
58-
</Compile>
59-
<Compile Include="EncodingDetector.cs" />
60-
<Compile Include="ExportToHTML.cs" />
61-
<Compile Include="ExportToRTF.cs" />
62-
<Compile Include="GoToForm.cs">
63-
<SubType>Form</SubType>
64-
</Compile>
65-
<Compile Include="GoToForm.Designer.cs">
66-
<DependentUpon>GoToForm.cs</DependentUpon>
67-
</Compile>
68-
<Compile Include="Hints.cs" />
69-
<Compile Include="HotkeysEditorForm.cs">
70-
<SubType>Form</SubType>
71-
</Compile>
72-
<Compile Include="HotkeysEditorForm.Designer.cs">
73-
<DependentUpon>HotkeysEditorForm.cs</DependentUpon>
74-
</Compile>
75-
<Compile Include="LineNumberFormatting.cs" />
76-
<Compile Include="LinesAccessor.cs" />
77-
<Compile Include="MacrosManager.cs" />
78-
<Compile Include="PlatformType.cs" />
79-
<Compile Include="Hotkeys.cs" />
80-
<Compile Include="Ruler.cs">
81-
<SubType>UserControl</SubType>
82-
</Compile>
83-
<Compile Include="Ruler.Designer.cs">
84-
<DependentUpon>Ruler.cs</DependentUpon>
85-
</Compile>
86-
<Compile Include="SyntaxDescriptor.cs" />
87-
<Compile Include="SyntaxHighlighter.cs" />
88-
<Compile Include="ReplaceForm.cs">
89-
<SubType>Form</SubType>
90-
</Compile>
91-
<Compile Include="ReplaceForm.Designer.cs">
92-
<DependentUpon>ReplaceForm.cs</DependentUpon>
93-
</Compile>
94-
<Compile Include="FastColoredTextBox.cs">
95-
<SubType>UserControl</SubType>
96-
</Compile>
97-
<Compile Include="CommandManager.cs" />
98-
<Compile Include="Commands.cs" />
99-
<Compile Include="FindForm.cs">
100-
<SubType>Form</SubType>
101-
</Compile>
102-
<Compile Include="FindForm.Designer.cs">
103-
<DependentUpon>FindForm.cs</DependentUpon>
104-
</Compile>
105-
<Compile Include="LimitedStack.cs" />
106-
<Compile Include="Line.cs" />
107-
<Compile Include="Place.cs" />
108-
<Compile Include="Properties\AssemblyInfo.cs" />
109-
<Compile Include="Range.cs" />
110-
<Compile Include="Style.cs" />
111-
<Compile Include="TextSource.cs" />
112-
<Compile Include="TypeDescriptor.cs" />
113-
<Compile Include="FileTextSource.cs" />
114-
<Compile Include="UnfocusablePanel.cs">
115-
<SubType>UserControl</SubType>
116-
</Compile>
117-
<Compile Include="VisualMarker.cs" />
118-
</ItemGroup>
119-
<ItemGroup>
120-
<EmbeddedResource Include="FastColoredTextBox.resx">
121-
<DependentUpon>FastColoredTextBox.cs</DependentUpon>
122-
</EmbeddedResource>
123-
<EmbeddedResource Include="FindForm.resx">
124-
<DependentUpon>FindForm.cs</DependentUpon>
125-
</EmbeddedResource>
126-
<EmbeddedResource Include="GoToForm.resx">
127-
<DependentUpon>GoToForm.cs</DependentUpon>
128-
</EmbeddedResource>
129-
<EmbeddedResource Include="HotkeysEditorForm.resx">
130-
<DependentUpon>HotkeysEditorForm.cs</DependentUpon>
131-
</EmbeddedResource>
132-
<EmbeddedResource Include="ReplaceForm.resx">
133-
<DependentUpon>ReplaceForm.cs</DependentUpon>
134-
<SubType>Designer</SubType>
135-
</EmbeddedResource>
136-
</ItemGroup>
137-
<ItemGroup>
138-
<None Include="FCTB_key.snk" />
139-
</ItemGroup>
140-
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>9.0.21022</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{6DD14A85-CCFC-4774-BD26-0F5772512319}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>FastColoredTextBoxNS</RootNamespace>
12+
<AssemblyName>FastColoredTextBox</AssemblyName>
13+
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
<TargetFrameworkProfile />
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>TRACE;DEBUG</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
<DocumentationFile>bin\Debug\FastColoredTextBox.XML</DocumentationFile>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<PropertyGroup>
36+
<SignAssembly>true</SignAssembly>
37+
</PropertyGroup>
38+
<PropertyGroup>
39+
<AssemblyOriginatorKeyFile>FCTB_key.snk</AssemblyOriginatorKeyFile>
40+
</PropertyGroup>
41+
<ItemGroup>
42+
<Reference Include="System" />
43+
<Reference Include="System.Data" />
44+
<Reference Include="System.Design" />
45+
<Reference Include="System.Drawing" />
46+
<Reference Include="System.Windows.Forms" />
47+
<Reference Include="System.XML" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<Compile Include="AutocompleteItem.cs" />
51+
<Compile Include="AutocompleteMenu.cs">
52+
<SubType>Component</SubType>
53+
</Compile>
54+
<Compile Include="Bookmarks.cs" />
55+
<Compile Include="Char.cs" />
56+
<Compile Include="DocumentMap.cs">
57+
<SubType>Component</SubType>
58+
</Compile>
59+
<Compile Include="EncodingDetector.cs" />
60+
<Compile Include="ExportToHTML.cs" />
61+
<Compile Include="ExportToRTF.cs" />
62+
<Compile Include="FileSavedEventArgs.cs" />
63+
<Compile Include="GoToForm.cs">
64+
<SubType>Form</SubType>
65+
</Compile>
66+
<Compile Include="GoToForm.Designer.cs">
67+
<DependentUpon>GoToForm.cs</DependentUpon>
68+
</Compile>
69+
<Compile Include="Hints.cs" />
70+
<Compile Include="HotkeysEditorForm.cs">
71+
<SubType>Form</SubType>
72+
</Compile>
73+
<Compile Include="HotkeysEditorForm.Designer.cs">
74+
<DependentUpon>HotkeysEditorForm.cs</DependentUpon>
75+
</Compile>
76+
<Compile Include="LineNumberFormatting.cs" />
77+
<Compile Include="LinesAccessor.cs" />
78+
<Compile Include="MacrosManager.cs" />
79+
<Compile Include="PlatformType.cs" />
80+
<Compile Include="Hotkeys.cs" />
81+
<Compile Include="Ruler.cs">
82+
<SubType>UserControl</SubType>
83+
</Compile>
84+
<Compile Include="Ruler.Designer.cs">
85+
<DependentUpon>Ruler.cs</DependentUpon>
86+
</Compile>
87+
<Compile Include="SyntaxDescriptor.cs" />
88+
<Compile Include="SyntaxHighlighter.cs" />
89+
<Compile Include="ReplaceForm.cs">
90+
<SubType>Form</SubType>
91+
</Compile>
92+
<Compile Include="ReplaceForm.Designer.cs">
93+
<DependentUpon>ReplaceForm.cs</DependentUpon>
94+
</Compile>
95+
<Compile Include="FastColoredTextBox.cs">
96+
<SubType>UserControl</SubType>
97+
</Compile>
98+
<Compile Include="CommandManager.cs" />
99+
<Compile Include="Commands.cs" />
100+
<Compile Include="FindForm.cs">
101+
<SubType>Form</SubType>
102+
</Compile>
103+
<Compile Include="FindForm.Designer.cs">
104+
<DependentUpon>FindForm.cs</DependentUpon>
105+
</Compile>
106+
<Compile Include="LimitedStack.cs" />
107+
<Compile Include="Line.cs" />
108+
<Compile Include="Place.cs" />
109+
<Compile Include="Properties\AssemblyInfo.cs" />
110+
<Compile Include="Range.cs" />
111+
<Compile Include="Style.cs" />
112+
<Compile Include="TextSource.cs" />
113+
<Compile Include="TypeDescriptor.cs" />
114+
<Compile Include="FileTextSource.cs" />
115+
<Compile Include="UnfocusablePanel.cs">
116+
<SubType>UserControl</SubType>
117+
</Compile>
118+
<Compile Include="VisualMarker.cs" />
119+
</ItemGroup>
120+
<ItemGroup>
121+
<EmbeddedResource Include="FastColoredTextBox.resx">
122+
<DependentUpon>FastColoredTextBox.cs</DependentUpon>
123+
</EmbeddedResource>
124+
<EmbeddedResource Include="FindForm.resx">
125+
<DependentUpon>FindForm.cs</DependentUpon>
126+
</EmbeddedResource>
127+
<EmbeddedResource Include="GoToForm.resx">
128+
<DependentUpon>GoToForm.cs</DependentUpon>
129+
</EmbeddedResource>
130+
<EmbeddedResource Include="HotkeysEditorForm.resx">
131+
<DependentUpon>HotkeysEditorForm.cs</DependentUpon>
132+
</EmbeddedResource>
133+
<EmbeddedResource Include="ReplaceForm.resx">
134+
<DependentUpon>ReplaceForm.cs</DependentUpon>
135+
<SubType>Designer</SubType>
136+
</EmbeddedResource>
137+
</ItemGroup>
138+
<ItemGroup>
139+
<None Include="FCTB_key.snk" />
140+
</ItemGroup>
141+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
141142
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
142143
Other similar extension points exist, see Microsoft.Common.targets.
143144
<Target Name="BeforeBuild">
144145
</Target>
145146
<Target Name="AfterBuild">
146147
</Target>
147-
-->
148+
-->
148149
</Project>

0 commit comments

Comments
 (0)