I'm building a WinFormApplication which allow the user to drag a document into a richTextBox which is loading the files content to the richTextBox. However the TextChange-event doesn't seem to happend emedietly after the text is changed, why could this be?
private void richTextBox_DragDrop(object sender, DragEventArgs e)
{
....
....
richTextBox.Text = content;
hasBeenEdited = false;
}
private void Form1_TextChanged(object sender, EventArgs e)
{
....
hasBeenEdited = true;
}
In the code above I have one DragDrop-event, the content of a file replaces the current richTextBox text. However the has been edited flag first get set to false, then true. I was hoping the flag would be set to true then false. How can I fix this?
Thanks!
Related
I've a user control with 2 labels and two textboxes. When the label is clicked the textbox's visible prop is set to true. Here's the code I've used:
private void label_Heading_Click(object sender, EventArgs e)
{
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}
After the textbox lose focus, it's visible prop is set to false and the label is updated with the text. Code:
private void textBox_Heading_Leave(object sender, EventArgs e)
{
textBox_Heading.Visible = false;
if(textBox_Heading.Text != "")
label_Heading.Text = textBox_Heading.Text;
label_Heading.Visible = true;
}
The code to create user controls on click:
private void label1_Click(object sender, EventArgs e)
{
TaskCard _taskCard = new TaskCard(++TOTAL_ITEM_COUNT, PanelName);
panel_DeletedItem.Controls.Add(_taskCard);
panel_DeletedItem.Refresh();
}
These code work fine when a single user control of this type is added to a panel. But if I add more than one, the code works only for the first user control, but it wont work for the new ones, although the event is fired for every user control. What am I missing here? Please suggest.
If I add a mbox to this code, the mbox is displayed for any control, but the rest of the code won't work, except for the first one.
private void label_Heading_Click(object sender, EventArgs e)
{
MessageBox.Show("Test"); // this will display, but the rest of the code is not executed or changes are not visible, i.e., the teboxes are not displayed even if I click the labels
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}
I have a Windows Form Application, where the User can input numbers into three different TextBoxes. I want to save these numbers by checking the Checkbox next to it, so when the Application gets closed and re-opened you don't have to put in the numbers again.
I have added the Properties to the User Settings and implemented the Code below, but when I input a number and re-open the Application, nothing is shown and they aren't saved in the user.config file.
Any help is greatly appreciated as I can't find my mistake.
private void MainForm_Load(object sender, EventArgs e)
{
Text = Properties.Settings.Default.title;
chkBox1.Checked = Properties.Settings.Default.checkBox;
chkBox2.Checked = Properties.Settings.Default.checkBox;
chkBox3.Checked = Properties.Settings.Default.checkBox;
txtBox1.Text = Properties.Settings.Default.textBox;
txtBox2.Text = Properties.Settings.Default.textBox;
txtBox3.Text = Properties.Settings.Default.textBox;
this.Location = new System.Drawing.Point(Properties.Settings.Default.PX, Properties.Settings.Default.PY);
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkBox = chkBox1.Checked;
Properties.Settings.Default.checkBox = chkBox2.Checked;
Properties.Settings.Default.checkBox = chkBox3.Checked;
Properties.Settings.Default.textBox = txtBox1.Text;
Properties.Settings.Default.textBox = txtBox2.Text;
Properties.Settings.Default.textBox = txtBox3.Text;
Properties.Settings.Default.PX = this.Location.X;
Properties.Settings.Default.PY = this.Location.Y;
Properties.Settings.Default.Save();
}
private void chkBox1_Checked(object sender, EventArgs e)
{
this.Text = txtBox1.Text;
}
private void chkBox2_Checked(object sender, EventArgs e)
{
this.Text = txtBox2.Text;
}
private void chkBox3_Checked(object sender, EventArgs e)
{
this.Text = txtBox3.Text;
}
Why not use databinding to save changes automatically. You don't need to replicate the code on form_load and form_closing events.
The best explanation I have for control data binds is that they provide two way model update between a control properties and object properties.
More Info https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.databindings?view=netcore-3.1
private void Form1_Load(object sender, EventArgs e)
{
chkBox1.DataBindings.Add("Checked", Properties.Settings.Default, "Checked1",true, DataSourceUpdateMode.OnPropertyChanged);
chkBox2.DataBindings.Add("Checked", Properties.Settings.Default, "Checked2",true, DataSourceUpdateMode.OnPropertyChanged);
chkBox3.DataBindings.Add("Checked", Properties.Settings.Default, "Checked3",true, DataSourceUpdateMode.OnPropertyChanged);
//you can others
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//don't forget to call save on form closing
Properties.Settings.Default.Save();
}
The first part of my answer, regarding to the fact that nothing is saved when you close your application, is based on the assumption that when testing, you leave the third textbox empty
Why is nothing saved
First is why you are seeing nothing when opening your application, leading you to believe nothing was saved when closing it.
You are in the part of your code handling what happens when your application is closing, saving all of the textboxes (and checkboxes states) in the same setting
Which leads to the following
txtBox1 contains a
txtbox2 contains nothing (or an empty string if you prefer)
When saving, what is happening with your code is that in a first step, you are putting "a" into your textbox setting.
Then, you are replacing this vlue with the content of the second textbox, which is empty
(repeat for the third textbox)
The you are saving.... An empty value.
If you wish to fix this in a "naive" way, you would need a setting per textbox and checkbox.
Which would lead to code ressembling this in your Closing event handler
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkBox1 = chkBox1.Checked;
Properties.Settings.Default.checkBox2 = chkBox2.Checked;
Properties.Settings.Default.checkBox3 = chkBox3.Checked;
Properties.Settings.Default.textBox1 = txtBox1.Text;
Properties.Settings.Default.textBox2 = txtBox2.Text;
Properties.Settings.Default.textBox3 = txtBox3.Text;
Properties.Settings.Default.PX = this.Location.X;
Properties.Settings.Default.PY = this.Location.Y;
Properties.Settings.Default.Save();
}
Why do I say "naive", because as you've surely understood, this approach is not sustainable for a huge number of controls, but this is not the scope of the question, I'll let you research a solution on your own for this particular point.
Why are the checkbox doing nothing to determine what is saved
First, with the events available on Winforms (at least with the .NET Framework 4.5 which I used to reproduce what you had) the only events available to be notified of the checkbox state change are :
CheckedChanged
CheckStateChanged
The first is used on a binary Checkbox (checked or not)
The second on a checkbox with an uncertain state added to both of the other states.
I imagine you used the first of the two (because that is the one used by default by Visual Studio when double clicking on it in the designer).
The first issue here is that it notifiesyou that the state changed not only that it went from unchecked to checked, but the other way around too.
That means if you only want an action to be done when checking, you need to add a.... check (an if block) to skip the cases you're not interest into.
Next is the actual saving.
What you are doing in your code is just copying the textbox values in a property in your class, and that will NOT persist after closing the application.
Now there is two approach you could use to save those values into the settings, the first is to do it as soon as you check the boxes.
What you would need to do then is for each event handler to copy the value of the textbox.... directly into the settings
An example for the first textbox and checkbox :
private void chkBox1_Checked(object sender, EventArgs e)
{
if(chkBox1.Checked)
{
Properties.Settings.Default.checkBox1 = chkBox1.Checked;
}
}
I'm not a huge fan though, and would prefer the second solution => to check in the closing event, before copying the value from the textbox into the settings, if the corresponding checkbox is closed.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.chkBox1.Checked)
{
Properties.Settings.Default.textBox = txtBox1.Text;
}
[...]
}
Now that a little better, and should be working as intended.
Please note that this answer is oriented towards correcting the problem whilst using solutions that are the closest possible of your original code.
I have problem with richBox1 text disabling.
I've tryed richTextBox1.readonly = true; and richTextBox1.Enabled = false;
My code:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.ReadOnly = !richTextBox1.ReadOnly;
}
Its disabling after one letter.
EDIT: And if disable I can still copy text but cant write there.
Honestly, disabling expected functionality is not something you should be doing. It is not good UI design.
The event TextChanged is fired every time the text changes (including writing or removing one letter). You can use Form's Load event (by double clicking the form on design time) :
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.ReadOnly = true;
richTextBox1.Enabled = false;
}
I'm creating a clipboard editing program and I encountered an error when I use the Copy button. If the text box from where it copies to the clipboard's contents are null, then I get a "ArgumentNullException was not handled". I know this is because the TextBox it copies the text from is empty. I want to write a method where if the TextBox is empty, then the button is disabled. Here is the code for this button:
// Copies the text in the text box to the clipboard.
private void copyButton_Click(object sender, EventArgs e)
{
Clipboard.SetText(textClipboard.Text);
}
Any and all help is appreciated. If I'm missing some more details please let me know so I can add them.
You have to initially set the button to be disabled.
Then you can use that code to detect the change in the text box:
private void textClipboard_TextChanged(object sender, EventArgs e)
{
copyButton.Enabled = textClipboard.Text.Length > 0;
}
You should check for null:
// Copies the text in the text box to the clipboard.
private void private void textClipboard_LostFocus(object sender, System.EventArgs e)
{
if(!string.IsNullOrEmpty(textClipboard.Text)
{
Clipboard.SetText(textClipboard.Text);
}
else
{
copyButton.Enabled = false; //Set to disabled
}
}
You could initially set the button.enabled to false, and add a KeyUp event to your textbox:
private void textClipboard_KeyUp(object sender, KeyEventArgs e)
{
copyButton.Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
i have a from c# and i want to show text Box after a click in a check box.
but when i scroll down, and check , the text Box is shown in the wrong place !!!
the text Boxes must be in the same level with check Boxes.
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
textBox1.Visible = true;
}
and changing the Location of the text Box don't give good results !
thanks for help.
You are running into an awkward quirk of the Panel control, it only scrolls controls that are visible. When you make it visible in your code, it will have the wrong Location property if you've used the scrollbar. You will need to make the correction yourself. Make it look like this:
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
if (!textBox1.Visible) {
textBox1.Location = new Point(textBox1.Left + panel1.AutoScrollPosition.X,
textBox1.Top + panel1.AutoScrollPosition.Y);
textBox1.Visible = true;
}
}
A better alternative is to use the Enabled property instead, also much less disorienting for the user. Set it to False in the designer and then:
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
textBox1.Enabled = true;
}