RichTextBox TextChanged not fired - c#

I have a RichTextBox which is in a page of my TabControl. Take notice that the RichTextBox is programmaticaly made with the following code:
TabPage addedTabPage = new TabPage("Tab Page");
tabControl.TabPages.Add(addedTabPage);
RichTextBox addedRichTextBox = new RichTextBox()
{
Parent = addedTabPage,
Dock = DockStyle.Fill,
/* ... */
};
Moreover, at the beginning of each of my methods I use this code in order to have access to my RichTextBox:
RichTextBox programTextBox =
(RichTextBox)tabControl.TabPages[tabControl.SelectedIndex].Controls[0];
Everything in my program seems to work fine, but I've noticed that the Text_Changed event is never fired. Why does that happen and how can make this event fire (preferably by its own as it does in a simple RichTextBox).

addedRichTextBox.TextChanged += addedRichTextBox_TextChanged;
void addedRichTextBox_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Text changed");
}

Related

User Control Form Click event not clicking on label C#

I have a problem with User form Click that I am trying to make in C# using a usercontrol.
It consists of a picturebox and a label. I want to call the click event but the picturebox and the label don't do anything when I click them. Only the background area of the usercontrol does what I want it to do. Any ideas?
here's my code
for (int i = 0; i < listitems2.Length; i++)
{
listitems2[i] = new declined();
//adding sample data to each dynamic user
listitems2[i].dicon = dicon[i];
listitems2[i].did = did[i];
listitems2[i].dname = dname[i];
//adding data to flow layout panel
flowLayoutPanel2.Controls.Add(listitems2[i]);
// below line will assing this (usercontrolclick) event to every user control created dynamically
listitems2[i].Click += new System.EventHandler(this.UserControl_Click);
}
for click function
void UserControl_Click(object sender, EventArgs e)
{
string ctrlName = ((UserControl)sender).Name;
applicable obj = (applicable)sender;
studid.Text = obj.id;
studname.Text = obj.name;
pictureBox1.Image = obj.icon;
}
You added OnClick handler to your UserControl, so only clicks made on UserControl will be registered. To enable Click event for all the controls on your form, you need to add your click handler to all other controls.
But, according to your code, you're adding click handling on form where your control is located. In this case, you cannot register clicks on control from "outside" (you can by making Label and PictureBox controls public and adding handlers for their OnClick events but that is wrong way to do it)
My suggestion is to make custom event on your form and raise it on Form, Label and PictureBox click, and then make handler for that event on form which holds your UserControl.
Something like this:
//make custom eventHandler on your UserControl
[Browsable(true)]
public event EventHandler UserControlClicked;
//constructor
public UserControl1()
{
InitializeComponent();
//after intialize compoment add same handler for all three controls
this.Click += ControlClicked;
this.pictureBox1.Click += ControlClicked;
this.label1.Click += ControlClicked;
}
//this method will "catch" all clicks
private void ControlClicked(object sender, EventArgs e)
{
//raise event
UserControlClicked?.Invoke(this, e);
}
and on form where your custom control is, add handler for UserControlClicked (maybe with cast, I don't know what listItems2[i] contains):
listitems2[i].UserControlClicked+= new System.EventHandler(this.UserControl_Click);
or maybe like this (with casting)
(listitems2[i] as UserControl1).UserControlClicked+= new System.EventHandler(this.UserControl_Click);
and handle the rest in your UserControl_Click method like before

NumericUpDown is not changing ToolStripStatusLabel when MouseEnter

I used this code to implement on hover tooltip, it works with TextBox, ComboBox, MaskedTextBox but not on NumericUpDown. Does anybody know why it is not working?
public static void addHovertip(ToolStripStatusLabel lb, Control c, string tip)
{
c.MouseEnter += (sender, e) =>
{
lb.Text = tip;
// MessageBox.Show(c.Name);
};
c.MouseLeave += (sender, e) =>
{
lb.Text = "";
};
}
I admit the deleted answer from Hans Passant helped a bit in creating this answer.
First of all your code works fine. If you're working with events that happen rather often (like the MouseEvents) you better add a Debug.WriteLine to your code so you can see in the Debugger Output Window which events, for which controls, in which order take place.
The main problem is that due to the fact that the numeric up/down control is a control that is buildup with two different child controls your MouseLeave event is called as soon as the mouse enters one of the two child controls. What happens is: MouseEnter is called when the mouse hits the single line border of the control and MouseLeave is called the moment the mouse is no longer on that line. In MouseLeave you set the Label to an emtpy string. That gives the impression that your code doesn't work.
By simply adding a loop to go over any child controls solves this issue. This still set the label to an empty string a bit to often but it is also immediately set to the correct text if needed.
Here is the changed code with the Debug statements in place.
public static void addHovertip(ToolStripStatusLabel lb, Control c, string tip)
{
c.MouseEnter += (sender, e) =>
{
Debug.WriteLine(String.Format("enter {0}", c));
lb.Text = tip;
};
c.MouseLeave += (sender, e) =>
{
Debug.WriteLine(String.Format("Leave {0}", c));
lb.Text = "";
};
// iterate over any child controls
foreach(Control child in c.Controls)
{
// and add the hover tip on
// those childs as well
addHovertip(lb, child, tip);
}
}
For completeness here is the Load event of my test form:
private void Form1_Load(object sender, EventArgs e)
{
addHovertip((ToolStripStatusLabel) statusStrip1.Items[0], this.numericUpDown1, "fubar");
}
Here is an animated gif demonstrating what happens when you move the mouse in and out the Numeric Up Down control:

C# How to fix run data show in RichTextBox by instand

I have a problem about showing data in RichTextBox by selected tab, but I don't need to show in the selected tab, I need show by RichTextBox for run.
private void btn_Runserver_Click(object sender, EventArgs e)
{
AddTab();
StartCMD();
}
private void AddTab()
{
TabPage newTab = new TabPage((string)cbConfig.SelectedItem);
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
rtb.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
rtb.BorderStyle = System.Windows.Forms.BorderStyle.None;
rtb.BackColor = System.Drawing.Color.White;
rtb.ReadOnly = true;
newTab.Tag = rtb;
newTab.Name = (string)cbConfig.SelectedItem;
newTab.Controls.Add(rtb);
tabControl.Controls.Add(newTab);
tabControl.SelectTab(newTab);
}
private void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
string strMessage = e.Data;
if (tabControl.InvokeRequired)
{
tabControl.Invoke(new Action(() =>
{
RichTextBox rtb = (RichTextBox)tabControl.SelectedTab.Tag;
rtb.AppendText(strMessage + Environment.NewLine);
rtb.Select(rtb.Text.Length - 1, 0);
rtb.ScrollToCaret();
}));
}
}
Code RunCMD Form
proc.OutputDataReceived += build_ErrorDataReceived;
proc.BeginOutputReadLine();
Now my problem is that if I run the program and data show in RichTextbox by selected tab, but I need to show data in RichTextbox On RichText Tab Safe
Someone said "change RichTextBox rtb = (RichTextBox)tabControl.SelectedTab.Tag;, don't use SelectedTab", but I not know how to change it accordingly.
I believe you are adding RichTextBox in one event and adding the text to it in another event. So there is a post back while adding the text. In this case, you need to re-add the controls which are dynamically created. Refer This Link which explains how to handle dynamic controls in asp.net.

Add character counter to MemoExEdit control

I am trying to make a character counter, 40/200...41/200 and so on. Now for a textbox control I am hooking into the KeyUp Event with something like this...
public static void GetRemainingChars(MyTextBox txt, LabelControl lbl)
{
var maxChars = txt.Properties.MaxLength;
lbl.Text = txt.Text.Length + "/" + maxChars;
}
Unfortunately the MemoExEdit control has a popup window you type the text into and that seems to be hidden. I tried the KeyUp, EditValueChanging, TextChanged, and they all do the same thing. They don't fire till the user closes the popup. I am guessing that it is a composite control that transfers the editvalue when it closes.
Any ideas on how I can get at the popups events? Is there a different way to do this?
Just because I couldn't find this anywhere else I will post my solution for other's benefit.
Subscribe to the Popup Event of the MemoExEdit control, then inside that subscribe to the EditValueChanging Event. That is where you can hook in. See below for MY working version. Tweaks may be needed for yourself. Also, the Popup Event is created in my Designer.cs file.
private void memContactWith_Properties_Popup(object sender, EventArgs e)
{
MemoExPopupForm popupForm = (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow as MemoExPopupForm;
MemoEdit me = popupForm.Controls[2] as MemoEdit;
me.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(me_EditValueChanging);
}
void me_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
var memo = (sender as MemoEdit);
var maxChars = memo.Properties.MaxLength;
lblContactWithCharCount.Text = memo.Text.Length + "/" + maxChars;
}

Dynamically set method for tabPage

I have TabControl. I added it to tabpages. To one of them(tpTags) I dynamically add usercontrol tagsModule. When I'll click at tpTags I wanna to call method on tagsModule BindData
NEW CODE:
TabPage tpTags = new TabPage();
tabControl1.TabPages.Add(tpTags);
...setting properties...
TagsModule tagsModule = newTagsModule(_countryCode, ObjectType.Country);
tpTags.Select() = tpTags.BindData(); //**How do it ??**
tpTags.Controls.Add(tagsModule);
It could be: "how do I set an event that triggers when this tab is selected?"
You have to wire up the delegate to the event like this:
tabControl1.SelectedIndexChanged += new EventHandler(this.tabControl1_SelectedIndexChanged);
Now you can put all your code inside this method
private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(TabControl1.SelectedTab == tpTags)
{
// BindData();
}
}
BindData() runs whenever your tabPage 'tpTags' is selected. If you want only for the first time, set a flag.
If you mean "how do I make this the current tab", then:
tabs.SelectedTab = tpTags;
If you mean "how do I respond when this tab is selected", then look at the tpTags.SelectedIndexChanged event (you don't necessarily need to care about the index when handling this event - you can just check tabs.SelectedTab again).

Categories