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.
Related
In a C# WinForms application I need to create a ContextMenuStrip with dropdown and textbox:
private System.Windows.Forms.ContextMenuStrip ct1;
private void button_Click(object sender, EventArgs e)
{
var header = new ToolStripMenuItem("Header");
header.Enabled = false;
var options = new ToolStripMenuItem("Options");
for (int i = 0; i < 5; i++)
{
var checkoption = new ToolStripMenuItem("Check Me " + i + "!");
checkoption.CheckOnClick = true;
options.DropDownItems.Add(checkoption);
}
var txt = new ToolStripTextBox();
txt.Text = "changeme";
options.DropDownItems.Add(txt);
options.DropDown.Closing += DropDown_Closing;
ct1.Items.Clear();
ct1.Items.Add(header);
ct1.Items.Add(options);
ct1.Show(this, button.Left, button.Top);
}
private void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked);
}
Now, e.Cancel will prevent closing the dropdown if the reason is ItemClicked, so I can select more items without having to open the menu again:
Please note that "changeme" is a ToolStripTextBox!
Once I focus it (click on it), I can edit the text inside:
After finish editing the textbox, I still can change the checkbox items, but there is no focus indicator:
How can I get back the focus indicator just as shown on the first picure?
Note: if I move the mouse onto "Header", the dropdown will close, and then moving it back to "Options", will reopen the dropdown and then the focus indicator is good again:
How can I do this without closing and reopening the dropdown?
I have tried Select() for the options item, but it did not help, neither Invalidate() on ct1.
Just have found it:
First needs to add a click handler on the dropdown:
options.DropDown.Click += DropDown_Click;
Then in the click handler it needs to be focused:
private void DropDown_Click(object sender, EventArgs e)
{
var dropdown = (ToolStripDropDown)sender;
dropdown.Focus();
}
class RTB
{
Form1 f = new Form1();
Button bt = new Button();
RichTextBox r = new RichTextBox();
public RichTextBox addPanel(string Task)
{
bt.Text = "X";
bt.Click += new EventHandler(btnButton_Click);
r.Controls.Add(bt);
return r;
}
void btnButton_Click(object sender, EventArgs e)
{
f.rem(r.Name);
}
}
class Form1 : Form
{
public void rem(string name)
{
flowLayoutPanel1.Controls.RemoveByKey(name);
}
}
In the RTB class, I am creating a RichRextBox, that contains a button control, and adding the Richtexbox with the button straight into a FlowLayoutPanel,
the button is subscribed to btnButton_Click so when it is clicked it will run f.rem(r.Name).
now when I click the button inside the RichTextBox to remove it from the FlowLayoutPanel it doesn't work. Any help will be greatly appreciated.
Note: I am able to create and add them to the FlowLayoutPanel, I did not include that code because i think it is irrelevant to this specific problem.
you need to assign name for RichTextBox that you need to remove at create
RichTextBox r = new RichTextBox();
r.Name = "Any_Unique_Name";
or other way without use ram function :
void btnButton_Click(object sender, EventArgs e)
{
//delete parent of clicked `Button` will be target `RichTextBox`
((Button)sender).Parent.Dispose();
}
You have the r variable available to you in the button click handler. You can simplify even more with an anonymous handler:
btn.Click += new EventHandler((s, e) => {
r.Dispose();
});
Now the control will disappear when the button is clicked.
As described in the title, I have a Form with a Datagridview on the front. The datagridview is smaller than my form in the back and I want the Datagridview to disappear whenever I click anywhere else but the Datagridview.
My code looks like this:
this.dataGridView1.Leave += new System.EventHandler(this.focus);
and the Eventhandler is defined like this:
private void focus(object sender, EventArgs e)
{
if(dataGridView1.Focused == false)
{
dataGridView1.Visible = false;
}
}
My problem is that my Datagridview only disappears when a new event in my form is activated but not when I click for example in a textbox on my form.
Can anyone help me?
The Leave event will not raise if you click on Form, or a ToolStripButton, PictureBox or any other non-selectable control.
If you expect a behavior like a dropdown, you can host DataGridView in a ToolStripControlHost and show it using a ToolStripDropDown. This way when you click anywhere outside the `DataGridView, it will disappear. It will act like a dropdown menu. Also the grid can be larger than your form:
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Margin = new Padding(0);
var host = new ToolStripControlHost(this.dataGridView1);
this.dataGridView1.MinimumSize = new Size(200, 100);
host.Padding = new Padding(0);
var dropdown = new ToolStripDropDown();
dropdown.Padding = new Padding(0);
dropdown.Items.Add(host);
dropdown.Show(button1, 0,button1.Height);
}
Important Note: It's an example. It's better to pay attention to disposing of objects in a real world application. For example, use just a single ToolStripdropDown and dispose it when closing the form.
Change the event handler assigning to:
this.dataGridView1.Leave += new System.EventHandler(fokussiert);
Worked for me when focusing on a textbox
you want your dgv also to disapear when you click on your textbox? is that what you mean?
private void dataGridView1_Leave(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}
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");
}
I have a TextBox and a RichTextBox with the same text. Everytime I click inside the RichTextBox, the TextBox should be focused with same caret position.
My first idea was this:
void richTextBox_GotFocus(object sender, RoutedEventArgs e)
{
vat textRange = new TextRange(rtfBox.Document.ContentStart, rtfBox.CaretPosition);
plainTextBox.Focus();
plainTextBox.CaretIndex = textRange.Text.Length;
}
But the problem is that the RichTextBox doesn't know the CaretPosition in the event handler yet.
Are there any workaround for this?
Maybe with subclassing the RichTextBox?
If you use Dispatcher.BeginInvoke to run that code it should be called after WPF has finished determining the carets position, etc.
e.g.
private void RichTextBox_GotFocus(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(new Action(UpdateTextBoxCaretPosition));
}
void UpdateTextBoxCaretPosition()
{
var textRange = new TextRange(rtfBox.Document.ContentStart, rtfBox.CaretPosition);
plainTextBox.Focus();
plainTextBox.CaretIndex = textRange.Text.Length;
}