using events for passing parameters - c#

In my code,I have a button and a textbox. I want by clicking on the button, the value of the variable k to be sent to the textbox. but when I click on the button nothing happens what's wrong with my code.
here's my code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate string fac(int x);
public static fac intostring = factory;
public static string inst= null;
public static string factory(int x)
{
inst = x.ToString();
return inst;
}
public delegate void myeventhandler(string newvalue);
public class EventExample
{
private string thevalue;
public event myeventhandler Valuechanged;
public string val
{
set
{
this.thevalue = value;
this.Valuechanged(thevalue);
}
}
}
public void uu(string newvalue)
{
this.textBox1.Name = (newvalue);
}
static int k=0;
private void button1_Click(object sender, EventArgs e)
{
k = 1;
intostring(k);
}
private void Form1_Load(object sender, EventArgs e)
{
EventExample myevt = new EventExample();
myevt.Valuechanged += new myeventhandler(uu);
myevt.val = inst;
}
}
}

Simply set it inside of button1_Click
private void button1_Click(object sender, EventArgs e)
{
k = 1;
intostring(k);
this.textBox1.Text = thevalue;
}

Related

return null when use the backing field

i write this code for add in database .
public partial class Unit : UserControl
{
private ProductUnit _pu { get; set; }
public ProductUnit PU
{
get
{
_pu.UnitNicname = TxtUnitNicName.Text;
_pu.UnitFaName = TxtUnitName.Text;
return _pu;
}
set { }
}
public Unit()
{
InitializeComponent();
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
StructureMapDefenation.Container.GetInstance<IUnitService>().Add(_pu);
}
}
i need to get textbox.text value with backing field but when i need to add value in database this _pu is null .
whats the problem ???
You never initialize _pu, which should be a field and not a property by the way:
private ProductUnit _pu = new ProductUnit();
I don't really understand the purpose of the PU property though.
You might as well create a ProductUnit directly in the event handler:
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
var pu = new ProductUnit();
pu.UnitNicname = TxtUnitNicName.Text;
pu.UnitFaName = TxtUnitName.Text;
StructureMapDefenation.Container.GetInstance<IUnitService>().Add(pu);
}
Try this:
public partial class Unit : Form // <--------- Try this
{
private ProductUnit _pu= new ProductUnit();
public ProductUnit PU
{
get {return _pu;} // read only
}
public Unit()
{
InitializeComponent();
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
_pu.UnitNicname = TxtUnitNicName.Text;
_pu.UnitFaName = TxtUnitName.Text;
StructureMapDefenation.Container.GetInstance<IUnitService>().Add(_pu);
}
}

C# UserControl Button click adds items to listbox from form1

I'm working on a C# app,that searches for song names on youtube (using youtube api) and displaying the data, I have a custom SongItem (UserControl) and I add them to a FlowLayoutControl. I added a "Favourite" button to the UserControl, and I need that to add itself to another FlowLayoutControl that's the Favourited song list, but I can't get that working, I can't have multiple base classes in the UserControl,and adding a public method to Form1, doesn't solve my issue, (it's not adding anything,tried it with listbox,but nothing).
I'm really stuck here,if someone can at least suggest something I would rly appreciate it.
Here is my SongItem
[Serializable]
public partial class SongItem : UserControl,Form
{
private String songName = "Song Name";
private String artistName = "Artist Name";
private Image thumbNail;
private String length;
private int maxLengthSongName = 25;
private int maxLengthArtistName = 25;
private Color colorHoverOn = Color.FromArgb(53,53,53);
private Color colorNormal = Color.FromArgb(53,53,53);
private SongData songData;
public SongItem()
{
InitializeComponent();
this.MouseClick += Control_MouseClick;
MouseEvents(this);
}
private void SongItem_Load(object sender, EventArgs e)
{
try
{
LoadData();
}
catch { }
LoadDataToUI();
}
#region GettersAndSetters
public int MaxLengthSongName
{
get { return maxLengthSongName; }
set { maxLengthSongName = value; }
}
public int MaxLengthArtistName
{
get { return maxLengthArtistName; }
set { maxLengthArtistName = value; }
}
public Color ColorHoverOn
{
get { return colorHoverOn; }
set { colorHoverOn = value; }
}
public Color ColorNormal
{
get { return colorNormal; }
set { colorNormal = value; }
}
public SongData SongData {
get{return songData; }
set { songData = value; }
}
#endregion
public void LoadData()
{
songName = songData.SongName;
artistName = songData.ArtistName;
thumbNail = songData.ThumbNail;
length = songData.Length;
}
void MouseEvents(Control container)
{
foreach (Control c in container.Controls)
{
c.MouseEnter += (s, e) => SongItem_MouseEnter(e);
c.MouseLeave += (s, e) => SongItem_MouseLeave(e);
c.MouseClick += Control_MouseClick;
MouseEvents(c);
};
}
private void SongNameLbl_MouseHover(object sender, EventArgs e)
{
if (songName.Length > maxLengthSongName) {
toolTip1.SetToolTip(songNameLbl, songName);
}
}
private void ArtistNameLbl_MouseHover(object sender, EventArgs e)
{
if (artistName.Length > maxLengthArtistName) {
toolTip1.SetToolTip(artistNameLbl, artistName);
}
}
private void SongNameLbl_Click(object sender, EventArgs e)
{
Clipboard.SetText(artistName + " " +songName);
}
#endregion
#region CurrentlySelected
public event EventHandler<EventArgs> WasClicked;
private void Control_MouseClick(object sender, MouseEventArgs e)
{
var wasClicked = WasClicked;
if (wasClicked != null)
{
WasClicked(this, EventArgs.Empty);
}
IsSelected = true;
}
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
this.BorderStyle = IsSelected ? BorderStyle.FixedSingle : BorderStyle.None;
}
}
#endregion
private void Fovourite_Click(object sender, EventArgs e)
{
Main newMain = new Main();
// newMain.AddSongToFavorite();
newMain.listBox1.Items.Add("Test");
}
}
}
Here is the the Form1 code
public void AddSongToFavorite() {
listBox1.Items.Add("Test");
//songList2.AddSong("Dire Straits - Sultans Of Swing");
MessageBox.Show("Hello", "Test");
// flowLayoutPanel1.Controls.Add(song);
}
Message shows up,but nothing else
I add the SongItem from another class
public partial class SongList : FlowLayoutPanel
{
public SongList()
{
InitializeComponent();
SongList_Load();
}
private void SongList_Load()
{
this.AutoScroll = true;
this.FlowDirection = FlowDirection.TopDown;
this.AutoSize = false;
this.WrapContents = false;
}
public async void AddSong(String songName) {
SongData song = await YoutubeSearch.GetSongInfo(songName);
// SongData song = XmlSerialization.ReadFromXmlFile<SongData>(Application.StartupPath + #"\test.txt");
SongItem songItem = new SongItem { SongData = song };
songItem.WasClicked += UsersGrid_WasClicked;
this.Controls.Add(songItem);
}
I fixed the problem by creating an EventHandler
In the main I created this public void
public void FavoriteWasClicked(object sender, EventArgs e) {
if (sender is SongItem)
{
songList2.AddSong(((SongItem)sender).SongData);
}
}
and on Form1 load I added (songList1 is the FlowLayoutPanel already added in the Form1 designer)
songList1.List_FavoriteWasClicked += FavoriteWasClicked;
I added this line to the FlowLayoutPanel class I created
public event EventHandler<EventArgs> List_FavoriteWasClicked;
and when creating new Controls(SongItems inside it)(Still part of FlowLayoutPanel)
public void AddSong(SongData song)
{
SongItem songItem = new SongItem { SongData = song };
songItem.FavoriteWasClicked += List_FavoriteWasClicked; ()
this.Controls.Add(songItem);
}
And now on the SongItem I created the custom event
public event EventHandler<EventArgs> FavoriteWasClicked;
and on the button I wanted to be the press to add to favorite list I added the following
FavoriteWasClicked(this, EventArgs.Empty);
Now when I press the button to add one Song to the liked/favorited list it's actually adding it.

I want to handle a clickbutton event from another page

I have two pages MainPage and PlayPage.Inside MainPage I have a frame and a textblock and inside the frame I have Playpage. When I click a button from play page I change a variable but the textblock doesn't update. How do I do that?
Here is my code:
public class Swag
{
public static int swag = 0;
public void Add(int a)
{
swag += a;
}
public void Reduce(int a)
{
swag -= a;
}
public int Get()
{
return swag;
}
}
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
MyFrame.Navigate(typeof(PlayPage));
SwagMeasurer.Text = Convert.ToString(Swag.Get());
}
}
public sealed partial class PlayPage : Page
{
public PlayPage()
{
this.InitializeComponent();
}
private void Clicker_Click(object sender, RoutedEventArgs e)
{
Swag.swag += 1;
}
}
Handling and Raising Events should be your new friend!
Swag :
public class Swag
{
private static int _swag;
public static void Add(int a)
{
_swag += a;
OnUpdate?.Invoke(new SwagEventArgs(a));
OnAddition?.Invoke(new SwagEventArgs(a));
}
public static void Reduce(int a)
{
_swag -= a;
OnUpdate?.Invoke(new SwagEventArgs(a));
OnSubtraction?.Invoke(new SwagEventArgs(a));
}
public static int Get()
{
return _swag;
}
public static event AddedValueEventHandler OnAddition;
public static event SubtractedValueEventHandler OnSubtraction;
public static event UpdatedValueEventHandler OnUpdate;
public delegate void AddedValueEventHandler(SwagEventArgs e);
public delegate void SubtractedValueEventHandler(SwagEventArgs e);
public delegate void UpdatedValueEventHandler(SwagEventArgs e);
}
Keep in mind, that privacy should be respected!
PlayPage :
public partial class PlayPage : Page
{
public PlayPage()
{
InitializeComponent();
}
private void Clicker_Sub_Click(object sender, RoutedEventArgs e)
{
Swag.Reduce(1);
}
private void Clicker_Add_Click(object sender, RoutedEventArgs e)
{
Swag.Add(1);
}
}
Please note, that I have added a subtraction Button.
MainWindow :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyFrame.Navigate(new PlayPage());
SwagMeasurer.Text = Convert.ToString(Swag.Get());
Swag.OnAddition += Swag_Addition;
Swag.OnSubtraction += Swag_OnSubtraction;
Swag.OnUpdate += Swag_OnUpdate;
}
private void Swag_OnUpdate(SwagEventArgs e)
{
SwagMeasurer.Text = Convert.ToString(Swag.Get());
}
private void Swag_OnSubtraction(SwagEventArgs e)
{
LastMode.Text = "That's a negative";
}
private void Swag_Addition(SwagEventArgs e)
{
LastMode.Text = "That's a positive";
}
}
LastMode is also a TextBlock (reprecenting if the user has dropped or raised his skill level).
SwagEventArgs :
public class SwagEventArgs : EventArgs
{
public SwagEventArgs(int value)
{
Value = value;
}
public readonly int Value;
}
SwagEventArgs is going to be used to store information as event data.

Calculator, clear window when second variable is typed after chosen operator

So I have this calculator http://gyazo.com/589156935eec141c3aedf83b9f960d29 (not enough reputation sorry)
When I type [1] and then [2] the display shows [12]
If I press a operator for example [+] the number 12 is still supposed to be shown in the display.
But, if I now start typing new numbers The old ones are supposed to be removed from the display. But i can't get this to work.
My form:
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 Miniräknare
{
public partial class Form1 : Form
{
Miniräknare miniräknare;
public Form1()
{
InitializeComponent();
miniräknare = new Miniräknare(0, 0, "", 0, false);
}
private void btnEquals_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.doEquals();
}
private void btnNum1_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("1", tbxWindow.Text);
}
private void btnNum2_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("2", tbxWindow.Text);
}
private void btnNum3_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("3", tbxWindow.Text);
}
private void btnNum4_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("4", tbxWindow.Text);
}
private void btnNum5_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("5", tbxWindow.Text);
}
private void btnNum6_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("6", tbxWindow.Text);
}
private void btnNum7_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("7", tbxWindow.Text);
}
private void btnNum8_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("8", tbxWindow.Text);
}
private void btnNum9_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("9", tbxWindow.Text);
}
private void btnNum0_Click(object sender, EventArgs e)
{
if (tbxWindow.Text != "") tbxWindow.Text = miniräknare.getOperand("0", tbxWindow.Text);
}
private void btnOperatorDivision_Click(object sender, EventArgs e)
{
}
private void btnOperatorTimes_Click(object sender, EventArgs e)
{
}
private void btnOperatorPlus_Click(object sender, EventArgs e)
{
miniräknare.Op = "+";
}
private void btnOperatorMinus_Click(object sender, EventArgs e)
{
miniräknare.Op = "-";
miniräknare.Change = true;
}
private void btnDecimal_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand(",", tbxWindow.Text);
}
private void btnClear_Click(object sender, EventArgs e)
{
}
private void btnSin_Click(object sender, EventArgs e)
{
}
private void btnCos_Click(object sender, EventArgs e)
{
}
private void btnTan_Click(object sender, EventArgs e)
{
}
private void btnSquared_Click(object sender, EventArgs e)
{
}
private void btnModulus_Click(object sender, EventArgs e)
{
}
private void btnExponential_Click(object sender, EventArgs e)
{
}
private void btnlogarithm_Click(object sender, EventArgs e)
{
}
private void btn1OverX_Click(object sender, EventArgs e)
{
}
private void btnLn_Click(object sender, EventArgs e)
{
}
private void btnPi_Click(object sender, EventArgs e)
{
}
private void btnMemoryClear_Click(object sender, EventArgs e)
{
}
private void btnMemoryRecall_Click(object sender, EventArgs e)
{
}
private void btnMemorySave_Click(object sender, EventArgs e)
{
}
}
}
My class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Miniräknare
{
class Miniräknare
{
private double first;
private double second;
private string op;
private double memory;
private bool change;
public Miniräknare(double first, double second, string op, double memory, bool change)
{
this.first = 0;
this.second = 0;
this.op = "";
this.memory = 0;
this.change = false;
}
public double First
{
get {return first; }
set { first = value; }
}
public double Second
{
get { return second; }
set { second = value; }
}
public string Op
{
get { return op; }
set { op = value; }
}
public double Memory
{
get { return memory; }
set { memory = value; }
}
public bool Change
{
get { return change; }
set { change = value; }
}
public string getOperand(string t, string textBox)
{
textBox = textBox + t;
if (t.Equals(","))
{
change = true;
second = double.Parse(textBox);
}
else if (op.Equals(""))
{
if (!change)
{
textBox = "";
change = true;
textBox = textBox + t;
}
first = double.Parse(textBox);
}
else
{
if (!change)
{
textBox = "";
change = true;
textBox = textBox + t;
}
second = double.Parse(textBox);
}
return textBox;
}
/* public string calculateAnswer()
{
} */
public string doEquals()
{
if (op == "-" ) return (first - second).ToString();
else return null;
}
}
}
In the following block after pressing "+" button "change" is true and the block is skipped when type the first digit of the second number.
else
{
if (!change)
{
textBox = "";
change = true;
textBox = textBox + t;
}
second = double.Parse(textBox);
}
since you assigned textBox at the beginning of the getOperand method, it will return the value combining what you already had on the screen with the new char.
public string getOperand(string t, string textBox)
{
textBox = textBox + t;
This should do the trick:
public string getOperand(string t, string textBox)
{
if (t.Equals(","))
{
textBox = textBox + t;
change = true;
second = double.Parse(textBox);
}
else if (Op.Equals(""))
{
textBox = textBox + t;
if (!change)
{
textBox = "";
change = true;
textBox = textBox + t;
}
first = double.Parse(textBox);
}
else
{
if (!change)
{
textBox = textBox + t;
}
else
{
textBox = t;
change = false;
}
second = double.Parse(textBox);
}
return textBox;
}
I know this is not https://codereview.stackexchange.com/ and this does not answer the question asked (which has already been answered) and might be marked as off-topic, but wanted to show the changes as suggested in the various comments in an orderly fashion, just to help you improve your coding (current and future) experience.
Changes that can be made to your Miniräknare class (Comments added to explain):
public class Miniräknare
{
public Miniräknare()
{
// Have a default constructor that sets all the default properties
First = 0;
Second = 0;
Op = "";
Memory = 0;
Change = false;
}
public Miniräknare(double first, double second, string op, double memory, bool change)
{
// If you have a constructor with parameters, use the parameters to set your properties
First = first;
Second = second;
Op = op;
Memory = memory;
Change = change;
}
// Use automatic properties, this improves readability and less confusion (As per D Stanley in comments)
public double First { get; set; }
public double Second { get; set; }
public string Op { get; set; }
public double Memory { get; set; }
public bool Change { get; set; }
public string getOperand(string t, string textBox)
{
// Apply changes as per the accepted answer
textBox = textBox + t;
if (t.Equals(","))
{
Change = true;
Second = double.Parse(textBox);
}
else if (Op.Equals(""))
{
if (!Change)
{
textBox = "";
Change = true;
textBox = textBox + t;
}
First = double.Parse(textBox);
}
else
{
if (!Change)
{
textBox = "";
Change = true;
textBox = textBox + t;
}
Second = double.Parse(textBox);
}
return textBox;
}
public string doEquals()
{
if (Op == "-") return (First - Second).ToString();
else return null;
}
}
Changes in your Form1:
Instantiate your miniräknare variable now like this, since you where setting them to the defaults with your original input parameters.
miniräknare = new Miniräknare();
Replace all your btnNum1_Click to btnNum9_Click event handlers with this single event handler to improve readability and volume of your code (Check additional comments in the code):
private void btnNumber_Click(object sender, EventArgs e)
{
// !! Remember !! to set the Tag value of each of your buttons to their corresponding values
// Then change btnNum1 to btnNum9's Click events to btnNumber_Click
// Additionally you can also just use ((Button)sender).Text if their text values will never change
// You could even do this with your operators, unless you have specific code for the button (like you have for btnNum0)
tbxWindow.Text = miniräknare.getOperand(((Button)sender).Tag as String, tbxWindow.Text);
}

Transmitting events from a Winform to another in C#

How can I click a Button(to generate a color) in one form and change text color in a RichTextBox in a another form? Thanks in advance. (Newbie trying to understand C#)
Some code:
1.WinForm
public delegate void ColorWindowEvent(Object sender, SecondrWindowEventArgs e);
public partial class ColorWindow : Form
{
public event ColorWindowEvent myEventHandler;
public ColorWindow ()
{
InitializeComponent();
}
public void MyEvent(Object sender, ColorWindowEventArgs e)
{
string s = "";
myEventHandler(this, new SecondWindowEventArgs(s));
}
private void btnRed_Click(object sender, EventArgs e)
{
Color c = Color.Red;
string s = c.ToString();
this.Close();
}
private void btnBlue_Click(object sender, EventArgs e)
{
Color c = Color.Blue;
string str = c.ToString();
this.Close();
}
private void btnGreen_Click(object sender, EventArgs e)
{
Color c = Color.Green;
string s = c.ToString();
this.Close();
}
}
public class SecondWindowEventArgs : EventArgs
{
private string s;
public SecondWindowEventArgs(string _s)
{
s = _s;
}
#region
public string S
{
get;
set;
}
#endregion
}
2.WinForm
public delegate void SecondWindowEvent(Object sender, FirstWindowEventArgs e);
public partial class SecondWindow : Form
{
public event SecondWindowEvent myEventHandler;
private string s;
public SecondWindow(String _s)
{
s = _s;
InitializeComponent();
}
public void MyEvent(Object sender, FirstWindowEventArgs e)
{
string str = rtf2.Text;
if (str != null)
{
myEventHandler(this, new FirstWindowEventArgs(str));
}
}
private void btnQuit_Click(object sender, EventArgs e)
{
this.Close();
}
private void rtf2_TextChanged(object sender, EventArgs e)
{
if (myEventHandler != null)
{
myEventHandler(this, new FirstWindowEventArgs(rtf2.Text.Substring(rtf2.Text.Length - 1)));
rtf2.ForeColor = Color.FromName(e.ToString());
}
}
private void btnClearText_Click(object sender, EventArgs e)
{
rtf2.Text = " ";
}
private void rtf2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Escape)
{
FargeVindu fargeVindu = new FargeVindu();
fargeVindu.minEventHandler += new FargeVinduEvent(fargeVindu_minEventHandler);
fargeVindu.Show();
}
else if (e.KeyData == Keys.Delete)
{
}
}
protected void ColorWindow_myEventHandler(object sender, SecondWindowEventArgs e)
{
rtf2.ForeColor = Color.FromName(s);
}
Random random = new Random();
private void SecondWindow_Load(object sender, EventArgs e)
{
lblText.ForeColor = Color.FromArgb(random.Next(255),
random.Next(255), random.Next(255));
}
public Color getColor
{
get;
set;
}
}
3.WinForm
public partial class FirstWindow : Form
{
public FirstWindow()
{
InitializeComponent();
}
private void btnQuit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnClick_Click(object sender, EventArgs e)
{
string str = " ";
SecondWindow secondWindow = new SecondWindow (str);
secondWindow.myEventHandler += new SecondWindowEvent(secondWindow_myEventHandler);
secondWindow.Show();
}
protected void secondWindow_myEventHandler(object sender, FirstWindowEventArgs e)
{
rtf1.AppendText(String.Format(e.Tekst));
}
public void btnClearText_Click(object sender, EventArgs e)
{
rtf1.Text = " ";
}
}
Clarification from comments
I would like the Form to Change the color on close after the button is clicked. This is what I tried:
private void btnRed_Click(object sender, EventArgs e)
{
Color c = Color.Red;
string s = c.ToString();
this.Close();
}
To answer your question, create a property on your Color Form that is set by your button you can then read it after the Form is closed when it returns from your ShowDialog statement.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
if(frm2.ShowDialog() == DialogResult.OK)
{
//this.BackColor=frm2.getColor; helps if I read the question more closely
richTextBox1.SelectionColor = frm2.getColor;
}
}
}
Form2
public partial class Form2 : Form
{
public Color getColor { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
getColor = Color.Red;
DialogResult = DialogResult.OK;
}
}
After playing with the code that you posted and added some missing handlers, it looks like the text of your color is coming in the Format of Color [Red] ColorFromName has no idea how to parse that so you will need to get the actual color name by using String.Split Something like this.
protected void ColorWindow_myEventHandler(object sender, SecondWindowEventArgs e)
{
rtf2.ForeColor = Color.FromName(e.S.Split(new string[]{"[","]"},StringSplitOptions.None)[1]);
}
I also noticed that you are setting your rtf2.ForeColor everytime that your text changes, I removed it and am now able to change the ForeColor of the RichText box. I would be a lot easier/cleaner IMHO if you just passed the actual Color Object instead of changing it to a string and back.
This is the modified TextChanged Method note the commented out rtf2.ForeColor statement it does not belong there.
private void rtf2_TextChanged(object sender, EventArgs e)
{
if (myEventHandler != null)
{
myEventHandler(this, new FirstWindowEventArgs(rtf2.Text.Substring(rtf2.Text.Length -1)));
// rtf2.ForeColor = Color.FromName(e.ToString());
}
}

Categories