Skip to content

Commit 870bbb1

Browse files
committed
#44: add '{' and '}' buttons and refactor.
1 parent bf697dd commit 870bbb1

File tree

2 files changed

+117
-7
lines changed

2 files changed

+117
-7
lines changed

CreateGUIDVSPlugin/OptionsControl.Designer.cs

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CreateGUIDVSPlugin/OptionsControl.cs

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace CreateGUIDVSPlugin
1414
public partial class OptionsControl : UserControl
1515
{
1616
private OptionsPageSetting settingOptionsPage;
17-
17+
private int insertPos;
18+
1819
public OptionsControl()
1920
{
2021
InitializeComponent();
@@ -24,6 +25,9 @@ public OptionsControl()
2425
this.comboBoxVariable.Items.Add(variableName.Description);
2526
}
2627
this.comboBoxVariable.SelectedIndex = 0;
28+
29+
this.insertPos = 0;
30+
this.textBoxTemplate.LostFocus += new EventHandler(textBoxTemplate_LostFocus);
2731
}
2832

2933
/// <summary>
@@ -57,28 +61,108 @@ public void SaveSetting(Configuration configuration)
5761
/// <param name="configuration"></param>
5862
public void LoadSetting(Configuration configuration)
5963
{
60-
this.textBoxTemplate.Text = configuration.FormatString;
64+
SetText(configuration.FormatString);
6165
}
6266

67+
/// <summary>
68+
/// handler clicking 'Insert' button
69+
/// </summary>
70+
/// <param name="sender"></param>
71+
/// <param name="e"></param>
6372
private void buttonInsert_Click(object sender, EventArgs e)
6473
{
6574
var guidIndex = decimal.ToInt32(this.numericUpDownGUID.Value);
6675
var keyname = Template.Variables[this.comboBoxVariable.SelectedIndex];
6776
var variable = (guidIndex > 0 )? keyname.GetVariable(guidIndex) : keyname.GetVariable();
68-
var textBox = this.textBoxTemplate;
69-
textBox.Text = textBox.Text.Insert(textBox.SelectionStart, variable);
77+
InsertText(variable);
7078
}
7179

80+
/// <summary>
81+
/// handler clicking 'EOL' button
82+
/// </summary>
83+
/// <param name="sender"></param>
84+
/// <param name="e"></param>
7285
private void buttonInsertLineEnding_Click(object sender, EventArgs e)
7386
{
74-
var textBox = this.textBoxTemplate;
75-
textBox.Text = textBox.Text.Insert(textBox.SelectionStart, Environment.NewLine);
87+
InsertText(Environment.NewLine);
88+
}
89+
90+
/// <summary>
91+
/// handler clicking '{' button
92+
/// </summary>
93+
/// <param name="sender"></param>
94+
/// <param name="e"></param>
95+
private void buttonLeftBrace_Click(object sender, EventArgs e)
96+
{
97+
InsertText("{{");
7698
}
7799

100+
/// <summary>
101+
/// handler clicking '}' button
102+
/// </summary>
103+
/// <param name="sender"></param>
104+
/// <param name="e"></param>
105+
private void buttonRightBrace_Click(object sender, EventArgs e)
106+
{
107+
InsertText("}}");
108+
}
109+
110+
/// <summary>
111+
/// hander clicking 'Default' button
112+
/// </summary>
113+
/// <param name="sender"></param>
114+
/// <param name="e"></param>
78115
private void buttonSetDefault_Click(object sender, EventArgs e)
116+
{
117+
SetText(Template.DefaultFormatString);
118+
}
119+
120+
/// <summary>
121+
/// handler that the textbox loses focus
122+
/// </summary>
123+
/// <param name="sender"></param>
124+
/// <param name="e"></param>
125+
private void textBoxTemplate_LostFocus(object sender, EventArgs e)
126+
{
127+
TextBox textBox = (TextBox)sender;
128+
this.insertPos = textBox.SelectionStart;
129+
}
130+
131+
/// <summary>
132+
/// insert a text to the textbox
133+
/// </summary>
134+
/// <param name="text"></param>
135+
private void InsertText(string text)
136+
{
137+
var textBox = this.textBoxTemplate;
138+
textBox.Text = textBox.Text.Insert(this.insertPos, text);
139+
this.insertPos += text.Length;
140+
141+
ScrollToCarect();
142+
}
143+
144+
/// <summary>
145+
/// overwrite textbox content
146+
/// </summary>
147+
/// <param name="text"></param>
148+
private void SetText(string text)
149+
{
150+
var textBox = this.textBoxTemplate;
151+
textBox.Text = text;
152+
this.insertPos = text.Length;
153+
154+
ScrollToCarect();
155+
}
156+
157+
/// <summary>
158+
/// function to scroll the textbox to the caret position
159+
/// </summary>
160+
private void ScrollToCarect()
79161
{
80162
var textBox = this.textBoxTemplate;
81-
textBox.Text = Template.DefaultFormatString;
163+
textBox.SelectionStart = this.insertPos;
164+
textBox.Focus();
165+
textBox.ScrollToCaret();
82166
}
83167
}
84168
}

0 commit comments

Comments
 (0)