C# make Event to RichTextBox in TabControl - c#

I make RichTextBox in TabControl:
private void newModuleToolStripMenuItem_Click(object sender, EventArgs e) {
TabPage tab = new TabPage();
RichTextBox richText = new RichTextBox();
string promptValue = ShowDialog("Input File Name", "File name");
tab.Text = promptValue;
tabControl1.Controls.Add(tab);
tabControl1.SelectTab(tabControl1.TabCount - 1);
richText.Parent = tabControl1.SelectedTab;
richText.Dock = DockStyle.Fill;
}
and I would to make event TextChange to this RichTextBox.

You can just add the following to newModuleToolStripMenuItem_Click code:
richText.TextChanged += RichText_TextChanged;
Then define the event handler:
private void RichText_TextChanged(object sender, EventArgs e)
{
// add your handling code here ...
}
Or you can make your event handler in lambda expression:
richText.TextChanged += (sender, e) =>
{
// add your handling code here ...
};

Related

Programmatically add Click EventHandler to Button

I'm creating Buttons programmatically with a method and am wanting to attach a Click event handler. However, that data currently comes from a string parameter which can't be used with += RoutedEventHandler.
public Button CreateButton(string Display, string Name, string ClickEventHandler)
{
Button Btn = new Button
{
Content = Display,
Name = "Btn_" + Name
};
Btn.Click += new RoutedEventHandler(ClickEventHandler);
return Btn;
}
void Btn_save_Click(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
// later
Button MyButton = CreateButton("Save", "save", "Btn_save_Click");
Error is RoutedEventHandler expects a Method and not a String. Is there a different approach to programmatically binding events that allows this sort of behaviour?
Thanks
From what I understand you wish to pass the method that should be executed when Click event is triggered. You could do something along the lines of:
Button button = CreateButton("Save", "save", (s, e) => SomeOnClickEvent(s, e));
Button button2 = CreateButton("Create", "create", (s, e) => SomeOtherOnClickEvent(s, e));
public Button CreateButton(string display, string name, Action<object, EventArgs> click)
{
Button b = new Button()
{
Content = display,
Name = $"Btn_{name}"
};
b.Click += new EventHandler(click);
return b;
}
void SomeOnClickEvent(object sender, EventArgs e)
{
}
void SomeOtherOnClickEvent(object sender, EventArgs e)
{
}
I am not entirely sure what you are trying to accomplish with this.
Here is an example of how to create an event at run time.
public void CreateButton()
{
Button Btn = new Button();
Btn.Click += new EventHandler(btn_Clicked);
}
private void btn_Clicked(object sender, EventArgs e)
{
// Your Logic here
}

Get text from dynamicly created RichTextBox in onChange event

How can I get the text from a dynamicly created RichTextBox and a dynamicly created rtb_TextChanged Event?
e.g:
private void button1_Click(object sender, EventArgs e)
{
RichTextBox rtb = new RichTextBox();
rtb.Name = "rtb" + i;
rtb.Dock = DockStyle.Fill;
rtb.TextChanged += rtb_TextChanged;
Controls.Add(rtb);
}
void rtb_TextChanged(object sender, EventArgs e)
{
//string s = rtb.Text; //How can I get the rtb.Text?
}
You need to use the sender argument of your event handler:
void rtb_TextChanged(object sender, EventArgs e)
{
RichTextBox rtb = (RichTextBox)sender;
string s = rtb.Text;
//... etc
}
You just need to use the event parameter : sender
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
RichTextBox rtb = (RichTextBox)sender;
var str = rtb .Text;
}
First rtb is not the name that you called the textbox. Since the textbox sent the message you could cast the sender to a textbox and look at its text property.

C# - Create dynamic buttons and events

I have a form with a tabControl and inside of each tab is a flowLayoutPanel where I can drag and drop files and a button is created for each dropped file. Afterwards when I click on a button, the file that i dropped should open. I have managed to do this for one file only.. My problem is how can I tell which button was clicked and to open the file/app stored in the path for each button.. How can I differentiate in the button_click event the clicked button and the path of the app to open?
Code for this part so far:
Process myProcess = new Process();
string path_app;
public Form1()
{
InitializeComponent();
this.DragEnter += new DragEventHandler(Form1_DragEnter);
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
e.Effect = DragDropEffects.All;
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string s in fileList)
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
}
}
private void button_Click(object sender, System.EventArgs e)
{
myProcess.StartInfo.FileName =path_app;
myProcess.Start();
}
Also my tabControl has the possibility to add new tabs but how can I get the selected tab and the inside flowLayoutPanel to know where to create the button?
And by the way, is there a problem of how I open the files? I understood that i have to take into consideration the working directory..
Thank you for your help!
You can utilize Tag property of the Button:
void Form1_DragDrop(object sender, DragEventArgs e)
{
foreach (String s e.Data.GetData(DataFormats.FileDrop))
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
// Add to Tag any data you want to pin to the button
button.Tag = path_app;
}
}
private void button_Click(object sender, System.EventArgs e)
{
// Obtain via Tag
String path_app = ((sender as Button).Tag as String);
myProcess.StartInfo.FileName = path_app;
myProcess.Start();
}
You could use button.Tag = "theFancyPath" and in the EventHandler cast the object sender as Button to access the Tag property.
If you need more then you could inherit from Button:
public class ButtonWithPathProperty : Button
{
public FileInfo PathToOpen { get; private set; }
public ButtonWithPathProperty(FileInfo path)
{
PathToOpen = path;
this.Click += new EventHandler(this.button_Click);
}
private void button_Click(object sender, System.EventArgs e)
{
var yourPath = this.PathToOpen;
}
}
This is not tested btw :)

Accessing Dynamically created control (textbox) in a dynamically created eventhandler

I am trying to access my dynamically created TextBox in C#, inside an event handler of a Button.
void MainFormLoad(object sender, EventArgs e)
{
this.Width=600;
this.Height=400;
this.FormBorderStyle= FormBorderStyle.FixedDialog;
TextBox t=new TextBox();
this.Controls.Add(t);
t.Location = new Point(60,40);
Label Mylable=new Label();
this.Controls.Add(Mylable);
Mylable.Location=new Point(15,43);
Mylable.Text="string : ";
t.Width=200;
t.Name="MyText";
t.Refresh();
Button Myb=new Button();
Myb.Location=new Point(270,40);
this.Controls.Add(Myb);
Myb.Text="Reverse it!";
Myb.Name="Mybo";
Myb.Click += new EventHandler(this.Myb_Clicked);
this.Refresh();
}
void Myb_Clicked(object sender, EventArgs e) {
// HOW SHOULD I GAIN ACCESS to MyText.Text HERE
MessageBox.Show();
}
Give a name to your dynamic TextBox:
TextBox t=new TextBox();
t.Name = "MyTextBox";
this.Controls.Add(t);
And then:
void Myb_Clicked(object sender, EventArgs e) {
string text = this.Controls["MyTextBox"].Text;
}
Wrong answer: object sender is the TextBox. You can cast sender to textbox and use it.
A decent way would be to make your textbox a class level member. And then you have access to it. If not, link TextBox.Text to a string property and use that.
You could keep a reference to your TextBox in your class
publc class MyForm: Form
{
TextBox myBox = null; // class member
void MainFormLoad(object sender, EventArgs e)
{
this.Width=600;
this.Height=400;
this.FormBorderStyle= FormBorderStyle.FixedDialog;
TextBox t=new TextBox();
myBox = t; // keep it for future reference
// rest of your code
}
void Myb_Clicked(object sender, EventArgs e) {
if (myBox !=null)
{
myBox.Text= "Clicked!";
}
MessageBox.Show();
}
}

Dynamically create an ImageButton

I’m trying to dynamically declare an ImageButton.
I declare it and assign an ID and Image to it as follows:
ImageButton btn = new ImageButton();
btn.ImageUrl = "img/Delete.png";
btn.ID = oa1[i] + "_" + i;
btn.OnClick = "someMethod";
But when I try to assign an OnClick handler for the button it throws the following exception:
System.Web.UI.WebControls.ImageButton.OnClick is inaccessible due to protection level
You couldn't assign a value to a method like that, even if it were accessible. You need to subscribe to the event:
btn.Click += ClickHandlingMethod;
Take a look at this answer, it is related with dynamic controls and events
As Jon commented you cannot add a string to the event, in this case you need to add a handler for the event:
protected void Page_Init(object sender, EventArgs e)
{
var i = new ImageButton();
i.Click += new ImageClickEventHandler(i_Click);
this.myPanel.Controls.Add(i);
}
void i_Click(object sender, ImageClickEventArgs e)
{
// do something
}
Alternativeley
protected void Page_Init(object sender, EventArgs e)
{
var i = new ImageButton();
i.Click += (source, args) =>
{
// do something
};
this.myPanel.Controls.Add(i);
}
An example:
private void CreateAButton()
{
var button = new ImageButton();
button.ImageUrl = "yourimage.png";
button.ID = "Button1";
button.Click += ButtonClick;
Page.Form.Controls.Add(button);
}
private void ButtonClick(object sender, ImageClickEventArgs e)
{
// Do stuff here
// ...
}
You can use this code (one significant change) :
private void CreateAButton()
{
var button = new ImageButton();
button.ImageUrl = "yourimage.png";
button.ID = "Button1";
button.PostBackUrl = "http://www.towi.lt";
Page.Form.Controls.Add(button);
}
Trick is in "PostBackUrl". If you write correct link it will redirects to it (as in example). In other cases this will add original server name, '/' and text you entered. For example 'xxx' will be turned to "http://yourservername/xxx". It is very useful, when you working with redirects to same ISS, but different sites and dynamically creating buttons for users.

Categories

Resources