I am using Scintilla Text Editor in my WPF application, I would like to enable the user to edit only whitespaces: insert, delete, replace etc.
I want to handle the before text insert event, and before text delete event of the scintilla, but there is no e.Handled = true to the TextModifiedEventArgs... how can I prevent the insertion / deletion? The KeyDown isn't enough for me because it does not always represent the text changed....
I have tried the following code:
static void scintilla_BeforeTextModified(object sender, TextModifiedEventArgs e)
{
var scintilla = (Scintilla) sender;
if (scintilla != null)
{
if (!string.IsNullOrWhiteSpace(scintilla.Selection.Text) || !string.IsNullOrWhiteSpace(e.Text))
{
// flag to ignore the change
_ignoreTextChanges = true;
// save text before modified
_text = scintilla.Text;
} }
} private void _scintilla_TextChanged(object sender, EventArgs e)
{
if (!_suspendTextChanges)
{
_suspendTextChanges = true;
if (_ignoreTextChanges)
{
_ignoreTextChanges = false;
Text = _text;
_scintilla.Text = _text;
}
else
{
Text = _scintilla.Text;
}
_suspendTextChanges = false;
}
_ignoreTextChanges = false;
}
But the text that I set does not change in scintilla...
Can anyone help me?
Thanks...
Related
When I click on a checkbox I want the next checkbox information to be displayed on a new line, I know how to do this with "\r\n" however when unchecking the box and rechecking the box, it adds a new line above the text moving the original text down by 1 line. https://imgur.com/a/IHDDG85
I've tried "\r\n" and Environment.NewLine
private void chkHamburger_CheckedChanged(object sender, EventArgs e)
{
if (chkHamburger.Checked == true)
{
txtHamburger.Enabled = true;
txtHamburger.Text = "";
txtHamburger.Focus();
txtOrder.Text += ("Hamburger");
}
else
{
txtHamburger.Enabled = false;
txtHamburger.Text = "0";
}
if (chkHamburger.Checked == false)
{
txtOrder.Text = txtOrder.Text.Replace("Hamburger", "");
}
}
private void chkCheeseBurger_CheckedChanged(object sender, EventArgs e)
{
if (chkCheeseBurger.Checked == true)
{
txtCheeseBurger.Enabled = true;
txtCheeseBurger.Text = "";
txtCheeseBurger.Focus();
txtOrder.Text += ("Cheese Burger");
}
else
{
txtCheeseBurger.Enabled = false;
txtCheeseBurger.Text = "0";
}
if (chkCheeseBurger.Checked == false)
{
txtOrder.Text = txtOrder.Text.Replace("Cheese Burger", "");
}
}
I want the text of a checkbox to be displayed on a new line but when rechecking the box a whitespace should not appear above it.
I suggest you to use a List<string> where you add or remove your orders. Then it is easy to rebuild the txtOrder data with a single line of code using string.Join
List<string> orders = new List<string>();
private void chkHamburger_CheckedChanged(object sender, EventArgs e)
{
txtHamburger.Enabled = chkHamburger.Checked;
if (chkHamburger.Checked)
{
txtHamburger.Text = "";
txtHamburger.Focus();
orders.Add("Hamburger");
}
else
{
txtHamburger.Text = "0";
orders.Remove("Hamburger");
}
UpdateOrders();
}
private void chkCheeseBurger_CheckedChanged(object sender, EventArgs e)
{
txtCheeseBurger.Enabled = chkCheeseBurger.Checked;
if (chkCheeseBurger.Checked)
{
txtCheeseBurger.Text = "";
txtCheeseBurger.Focus();
orders.Add("Cheese Burger");
}
else
{
txtCheeseBurger.Text = "0";
orders.Remove("Cheese Burger");
}
UpdateOrders();
}
private void UpdateOrders()
{
txtOrders.Text = string.Join(Environment.NewLine, orders);
}
The best way to do this is to have a routine that builds the contents of the text independent of what just happened -- this you could use join or a loop to create the text contents.
Make this a function and call it when the check boxes change. The function loops over all your items and adds them to the output with the formatting and totals etc.
I am trying to make my text inside the box disappear when a new value is typed inside the box. I figured out how to make it disappear but my reset to default value no longer works. Which is the problem
I tried multiple += signs and if statements ideas but I am not sure how to make this work together.
private void BtnResetDefault_Click(object sender, EventArgs e)
{
txtElapsedTime.Text = "2";
txtCurrentAmount.Text = "25";
txtInitalAmount.Text = "100";
}
private void TxtInitalAmount_TextChanged(object sender, EventArgs e)
{
if (txtInitalAmount.Text.Contains("100") && txtElapsedTime.Text.Contains("2") && txtCurrentAmount.Text.Contains("25"))
{
txtElapsedTime.Text = "100";
txtCurrentAmount.Text = "2";
txtInitalAmount.Text = "25";
}
else if (txtCurrentAmount.Text != null)
{
txtInitalAmount.Text = "";
txtElapsedTime.Text = "";
txtCurrentAmount.Text = "";
}
}
I want to have when the user types in a value in the text box all 3 text boxes disappear and when the user clicks the reset to default button the default text comes back inside the text boxes. Instead when I hit reset to default it stays the same.
You can try to unsubscribe from event "txtInitalAmount.TextChanged", and then resubscribe to it.
private void BtnResetDefault_Click(object sender, EventArgs e)
{
txtElapsedTime.Text = "2";
txtCurrentAmount.Text = "25";
txtInitalAmount.TextChanged -= txtInitalAmount_TextChanged;
txtInitalAmount.Text = "100";
txtInitalAmount.TextChanged += txtInitalAmount_TextChanged;
}
private void txtInitalAmount_TextChanged(object sender, EventArgs e)
{
txtInitalAmount.TextChanged -= txtInitalAmount_TextChanged;
txtElapsedTime.Text = "";
txtCurrentAmount.Text = "";
txtInitalAmount.Text = "";
}
I have a listview and created a textbox to search itmes in that list!
everything is ok about searching!
but problem is I want my search box to be like this:
at first Searchbox.Text="Search ..." and if user started typing in that searchbox change to that keyword! else(if) searchbox got empty Searchbox.Text change to "Search ..." again!
maybe it's a little complicated! but i tried 1-2 hours on it! and couldn't make it!
I have used Timers,checkboxchange event,booleans,...! but couldn't make it! :(
Please Help!
*my Searchbox name here is textbox1.
Some Codes I tested:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string str = textBox1.Text;
/*
if (!keywordentered_bool)
{
textBox1.Text = "";
}
*/
if (str != "")
{
//Doing Search operations!
search_bool = true;
}
else
{//Doing Search operations!
search_bool = true;
// keywordentered_checkbox.Checked = true;
Searchtextbox_Timer.Interval = 100;
Searchtextbox_Timer.Enabled = true;
Searchtextbox_Timer.Tick += Searchtextbox_Timer_Tick;
//textBox2.Visible = false;
}
}
else
{
if (search_bool)
{
listView1.Items.Clear();
label1.Visible = false;
listView1.Items.AddRange(Originalplaylist_list.ToArray());
if (!search_bool)
{
listView1.Items[MusicLogindex_list[MusicLogindex_list.Count - 1]].ForeColor = Color.Cyan;
}
else if (search_bool)
{//Doing Search operations
search_bool = false;
Searchtextbox_Timer.Interval = 100;
Searchtextbox_Timer.Enabled = true;
Searchtextbox_Timer.Tick += Searchtextbox_Timer_Tick;
//textBox2.Visible = true;
// keywordentered_checkbox.Checked = false;
}
}
void Searchtextbox_Timer_Tick(object sender, EventArgs e)
{
if (!search_bool)
{
textBox2.Visible = true;
textBox2.Location = textBox1.Location;
//textBox1.Text = "Search ...";
//textBox1.ForeColor = Color.Gray;
//textBox1.Font = new Font(textBox1.Font, FontStyle.Italic);
}
else
{
textBox2.Visible = false;
// textBox1.Text = "";
// textBox1.ForeColor = Color.Black;
// textBox1.Font = new Font(textBox1.Font, FontStyle.Regular);
}
Searchtextbox_Timer.Enabled = false;
//throw new NotImplementedException();
}
This is just psuedocode but the concept is there and you need to implement the text change event for searching , you can do additional changes in event handler.
Textbox myTxtbx = new Textbox();
myTxtbx.Text = "Enter text here...";
myTxtbx.OnFocus += OnFocus.EventHandle(RemoveText);
myTxtbx.LoseFocus += LoseFocus.EventHandle(AddText);
public RemoveText(object sender, EventArgs e)
{
myTxtbx.Text = "";
}
public AddText(object sender, EventArgs e)
{
if(string.IsNullorEmpty(myTxtbx.Text))
myTxtbx.Text = "Enter text here...";
}
This is an example shows for a dynamic textbox control, you can add these events to your control and use the same code for make it work.
Or you can use this plugin
Visual Studio gallery plugin
Another plugin you can use for this purpose
Textbox with placeholder
Hope this helps.
thanks to #Frebin Francis my problem solved!
I downloaded the source code from this link TextBox With Placeholder
and added it to my project! then add it to my form and yeah! :)
Can I create a button with both .Image and .Text properties simultaniously, in such way, that text is not visible on form, and is created just for identifying what button should do at the moment?
Using TextAlign and TextImageRelation properties doesn't help. Text is always visible, just a position changes.
private System.Windows.Forms.Button bRenameCourse;
this.bRenameCourse.BackColor = System.Drawing.SystemColors.ButtonFace;
this.bRenameCourse.Image = ((System.Drawing.Image)(resources.GetObject("bRenameCourse.Image")));
this.bRenameCourse.Location = new System.Drawing.Point(966, 6);
this.bRenameCourse.Name = "bRenameCourse";
I want this text "Rename" to be not visible on button
this.bRenameCourse.Text = "Rename";
this.bRenameCourse.Size = new System.Drawing.Size(64, 60);
this.bRenameCourse.TabIndex = 10;
this.bRenameCourse.UseVisualStyleBackColor = false;
this.bRenameCourse.Click += new System.EventHandler(this.bRenameCourse_Click);
Here is why do I want it works :
private void bRenameCourse_Click(object sender, EventArgs e)
{
if (bRenameCourse.Text.Equals("Rename"))
{
//DO SMTHNG
bRenameCourse.Text = "OK";
}
else if (bRenameCourse.Text.Equals("OK"))
{
//DO SMTHNG
bRenameCourse.Text = "Rename";
}
}
I can avoid this using some flags, but I'd like to know if it's possible in general.
Don't use the .Text property of the button to store information.You can use the .Tag property
ie
this.bRenameCourse.Tag = "Rename";
And in the Event
private void bRenameCourse_Click(object sender, EventArgs e)
{
if (bRenameCourse.Tag.Equals("Rename"))
{
//DO SMTHNG
bRenameCourse.Tag = "OK";
}
else if (bRenameCourse.Tag.Equals("OK"))
{
//DO SMTHNG
bRenameCourse.Tag = "Rename";
}
}
Just set the .Text property to ""(blank or empty)
I tried this but it doesn't work. They're still greyed out even when I select stuff.
btnVoirFiche.Enabled = false;
btnEchangerJoueur.Enabled = false;
if (lstJoueurs.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
else
{
}
You'll want to handle the ListBox.SelectedIndexChanged event, and within your handler you're going to check if the specific value is the selected one, and then set you button's enable property accordingly.
Something like this:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
else
{
//whatever you need to test for
}
}
Cheers
EDIT: I'm not too sure what your logic for button's enabled property is, so my answer is pretty generic. If you add details to you question, I'll adapt accordingly.
Hook into SelectedIndexChanged event and put your code inside of it
private void lstJoueurs_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstJoueurs.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
}
As an alternative, and using mrlucmorin's answer, you could use the listbox's SelectedItem which will return null if nothing is selected.