Can't write into a RichTextBox control - c#

I can't write in richTextBox or the textBox , whenever I start to write something it freezes my program. Any idea what it is ? I haven't changed anything in code or properties of the textBox.
using System.Text;
using System.Windows.Forms;
using System.IO;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
File.WriteAllText("TextFile1.txt", richTextBox1.Text);
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.LoadFile ("TextFile1.txt");
}
}

It looks like you are loading a file into the text box that you are writing in:
So get rid of this piece of code:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.LoadFile ("TextFile1.txt");
}
Try moving it into the OnLoad method of the form (assuming you want the text box populated when the form opens):
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
richTextBox1.LoadFile ("TextFile1.txt");
}
The RichTextBox also has a SaveFile method. It's not clear from your code if the "rich" text is important to the application.

bool _isLoading = false;
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if(_isLoading) return;
_isLoading = true;
richTextBox1.LoadFile ("TextFile1.txt");
_isLoading = false;
}

Related

C# Chromium browser whatsapp web send message using textbox

I already follow the Chromium user guide, still doesn't work. Using my C# program, I want to send message or image via whatsapp web by cascading the textbox in my program with the message box of whatsapp. This is my code
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Windows.Forms;
namespace ChromiumwithEditorWsWeb
{
public partial class Form1 : Form
{
ChromiumWebBrowser browser;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
browser = new ChromiumWebBrowser("http://web.whatsapp.com");
browser.Dock = DockStyle.Fill;
panel1.Controls.Add(browser);
}
private void buttonBack_Click(object sender, EventArgs e)
{
browser.Back();
}
private void buttonForward_Click(object sender, EventArgs e)
{
browser.Forward();
}
private void buttonReload_Click(object sender, EventArgs e)
{
browser.ShowDevTools();
}
private void buttonGo_Click(object sender, EventArgs e)
{
browser.Load(textBoxUrl.Text);
}
private void buttonPhoneNo_Click(object sender, EventArgs e)
{
browser.EvaluateScriptAsync("document.GetElementsByClass('jN-F5 copyable-text selectable-text').value = textBoxPhone.Text");
browser.EvaluateScriptAsync("document.GetElementsByClass('_2S1VP copyable-text selectable-text').value = textBoxMessage.Text");
}
}
}
Help it! Give suggestions for the best step.
Clipboard.SetText(msg);
Thread.Sleep(TimeSpan.FromSeconds(2));
SendKeys.Send(msg); //SendKeys.SendWait("^{V}");
Thread.Sleep(TimeSpan.FromSeconds(1));
SendKeys.SendWait("{ENTER}");
Not a good solution though, as it requires the windows to be on focus and this uses the clipboard. But it works !

C# Winforms change label based on running process

I have a dotnet 2.0 app in C# that I have developed in Visual Studio 2008, it's pretty simple:
I would like to change the label name currently called "label1" to "Running" or "Stopped" when my process loop.exe is running or stopped.
When I press start, it will run loop.exe and the stop button will obviously stop it.
I've read a lot of topic on C# Winforms but I cannot get this working and I have no idea what do to now. I think that I need to add a backgroundworker but I don't know how to check the process and update the label programmatically.
Here's my clean/current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace APP_NAME
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("taskkill", "/F /IM loop.exe");
Process.Start("taskkill", "/F /IM azcopy.exe");
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Process.Start("notepad.exe", #"C:\ProgramData\APP_NAME\settings\settings.xml");
}
private void button4_Click(object sender, EventArgs e)
{
Process.Start("explorer.exe", #"C:\ProgramData\APP_NAME\logs");
}
}
}
I hope I made myself clear, thank you.
You can update the text shown by a label by accessing it's Text property. So, change your method like:
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
label1.Text = "Running";
}
//hi
//Do this to see the label value changes in real time in the window
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
label1.Text = "Running";
lable1.Refresh();
}

By pressing a button i want to show the user all the data inserted to the boxes

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Apostolh_Texnikou
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void GN_TextChanged(object sender, EventArgs e)
{
}
private void CP_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
I want the user to click in the ready button and a window to pop out.
In the window the user must be able to copy the text.
I found how to make a pop window but its just a window which doesn't allow
copy paste. thank you in advance
You might want to look into a more complex type of dialog box. Perhaps one with a textbox? This will require launching another form. (see how to display textBox control in MessageBox?)

New coder, trying to make Label invisible on program start

First, pardon my new-ness, I just started coding class recently. Now, upon startup, I want parts of my form (c#) to not be shown, however when I put
NameDisplay.Visible = false;
(NameDisplay being the label I wish to hide) into my Form1.cs it gives me the error of that it is a 'field' being used as a 'type'. How do I correct this, and apply to other object types (buttons, textboxes, etc?)
EDIT 1-
Code- as it stands
namespace ATM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label NameDisplay;
NameDisplay.Visible = false;
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Remove Label NameDisplay;, and place NameDisplay.Visible = false; into your FormLoad event.
The loading of a form is an event just like clicking a button, and will execute the code like so.
Also, when I hide labels, I use .Hide(), but I believe that only works on WinForms.
Hope this helps!
You need to drag and drop the Label on the form and object will be created and initialized automatically in InitializeComponent.
In the form constructor (after InitializeComponent function) or Form_Load event, you may set the visibility to false
For example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
NameDisplay.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}

Save user settings in c#

i try to save user settings in c#.
I can read the value and put it in a textbox but i can not change it.
This is my WindowsForm with an textbox and a save button:
namespace tool
{
public partial class Form4 : Form
{
string source = Properties.Settings.Default.Source;
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = source;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Source = source;
Properties.Settings.Default.Save();
Application.Exit();
}
}
}
And here is a picture with my settings:
I hope someone as an idea :-)
Thanks for help
Try this:
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Source = textBox1.Text;
Properties.Settings.Default.Save();
Application.Exit();
}
You need to use what is currently in the text box, not your member variable.
Or you can change this event to be as follows (and then you don't have to modify the above):
private void textBox1_TextChanged(object sender, EventArgs e)
{
source = textBox1.Text;
}
could you possibly have a read lock being applied to the key you're looking at while testing this? Maybe the code's not the problem?

Categories