Click to See Complete Forum and Search --> : Special COlorDialog Code


Mikepoulsen
December 1st, 2009, 01:55 PM
private void newToolStripButton_Click(object sender, EventArgs e)
{
mdiChild = new Form();
mdiChild.Text = "Unavngivet" + count.ToString(); +count.tostring();
mdiChild.MdiParent = this;
editTextBox = new TextBox();
editTextBox.Multiline = true;
editTextBox.Dock = DockStyle.Fill;
mdiChild.Controls.Add(editTextBox); //Giver os lov til at redigere den
mdiChild.Show();
count++;
}
Thats how a new form looks like

I want to change the target texts color
If it can help you, then my copy vode is here
private void copyToolStripButton_Click(object sender, EventArgs e)
{
Form activeChildForm = this.ActiveMdiChild;

if (activeChildForm != null)
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
if (activeTextBox != null)
{
activeTextBox.Copy();
}
}

}

And atlast my colordialogboks. It shows but wont work.
private void toolStripButton1_Click(object sender, EventArgs e)
{


Form activeChildForm = this.ActiveMdiChild;

if (activeChildForm != null)
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
if (activeTextBox != null)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() != DialogResult.Cancel)
{

activeTextBox.ForeColor = colorDialog1.Color;
}
}
}

Please help me as fast as possible. Also It is a .Txt document, if that helps..

BigEd781
December 1st, 2009, 02:15 PM
What do you mean by "it won't work"? Have you used the debugger to check what is actually being returned by colorDialog1.Color? Are you sure that you are setting the property of the correct TextBox?

Also, lines like this are not very endearing...

Please help me as fast as possible

Everyone who asks a question here wants help. Be appreciative if someone takes the time to help you at all, no one is getting paid here.

Mikepoulsen
December 1st, 2009, 02:35 PM
I dont quite understand what u mean by using the debugger?

And also my quote. yes sorry i know, just wanted to fix this tonight, since i gotta show it tomorrow...

Thanks BigEd for helping me

BigEd781
December 1st, 2009, 02:43 PM
I mean, set a breakpoint on this line and look at what is actually going on:

activeTextBox.ForeColor = colorDialog1.Color;

Mikepoulsen
December 1st, 2009, 02:45 PM
Okay i did it says something with wrong modules, and the codes dosnt work together. Ive also got this error: Error 1 The name 'activeTextBox' does not exist in the current context.
How do i make it an activ context?

BigEd781
December 1st, 2009, 02:59 PM
Okay i did it says something with wrong modules, and the codes dosnt work together.

I have no clue what that means.

Ive also got this error: Error 1 The name 'activeTextBox' does not exist in the current context.
How do i make it an activ context?

So now you are getting compilation errors? I see activeTextBox declared in both methods, so you must not have posted the relevant code.

Mikepoulsen
December 1st, 2009, 03:04 PM
Maybe not. And sorry for that.

Here is my fully code. I wrote some stuff in danish to keep myself remember what each button do.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int count; //Hver gang vi laver ny fil
Form mdiChild; //Vores Value
TextBox editTextBox; // Vores filtype

public Form1()
{
InitializeComponent();
count = 1; //Hver gang vi laver ny fil + count 1! ;)
}

private void printToolStripButton_Click(object sender, EventArgs e)
{
printDialog1.ShowDialog(); //Viser Print siden, Vi kalder på funktionen printdialog1.(Nu viser vi den)showdialog();
}

private void saveToolStripButton_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog(); //Her laver vi vores value

sfd.Title = "Save a Text File"; //Titlen på vores gemte fil
sfd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*"; //De filtyper vi kan gemme i. | er en opbremsning dvs |.html|txt. Så kan jeg gemme i html og txt. ( Bare for exempel)

DialogResult dr = sfd.ShowDialog();
if (dr == DialogResult.OK) // sammenligner.
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName); // Her skriver vi koden

Form activeChildForm = this.ActiveMdiChild; // Den "Nuværende form"

if (activeChildForm != null)
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;

if (activeTextBox != null)
{
sw.Write(activeTextBox.Text);
}

sw.Close();
}
}

}

private void openToolStripButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog(); // Foregår på nogenlunde samme måde som save, det her er bare en open..

ofd.Title = "Open a Text File";
ofd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";

DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName);

Form activeChildForm = this.ActiveMdiChild;

if (activeChildForm != null)
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;

if (activeTextBox != null)
{
activeTextBox.Text = sr.ReadToEnd();
}

sr.Close();
}
}

}

private void copyToolStripButton_Click(object sender, EventArgs e)
{
Form activeChildForm = this.ActiveMdiChild;

if (activeChildForm != null) //Foregår på samme måde som cut og paste, den her er bare Copy
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
if (activeTextBox != null)
{
activeTextBox.Copy();
}
}

}

private void newToolStripButton_Click(object sender, EventArgs e)
{
mdiChild = new Form(); //Vi laver en "Value" dvs. Vi kalder en ny form frem her
mdiChild.Text = "Unavngivet" + count.ToString(); //Her laver vi navnet på den nye form. +count.tostring(); betyder at vi sætter et 1-tal for enden når vi opretter en ny "Form"
mdiChild.MdiParent = this; //bestemmer hvordan den skal komme frem.
editTextBox = new TextBox(); //Bestemmer vi filtypen
editTextBox.Multiline = true; // Og at vi kan lave flere linier
editTextBox.Dock = DockStyle.Fill; //Og at vi kan bruge hele vores "Form"
mdiChild.Controls.Add(editTextBox); //Giver os lov til at redigere den
mdiChild.Show(); //Viser vores form
count++; // det gør at vi kan gøre det her for evight

}

private void cutToolStripButton_Click(object sender, EventArgs e)
{
Form activeChildForm = this.ActiveMdiChild; // Her sætter vi en value

if (activeChildForm != null) // Her siger vi , hvis nu at..
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
if (activeTextBox != null) //activeTextBox.Cut(); Viser at det er en cut funktion, så derfor copierer den og sletter det man kopierede"
{
activeTextBox.Cut(); // hvis du at man har markeret noget og trykker på denne knap, så cutter man
}
}

}

private void pasteToolStripButton_Click(object sender, EventArgs e)
{
Form activeChildForm = this.ActiveMdiChild;
// Her fortæller vi at hvis der er noget kopieret så skal den overskrive det. Når den ind slætter / Paster.
if (activeChildForm != null)
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
if (activeTextBox != null)
{
activeTextBox.Paste(); //Paste
}
}

}

private void toolStripButton1_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() != DialogResult.Cancel)
{
activeTextBox.ForeColor = colorDialog1.Color;
}

}
}


Maybe my whole program is wrong, in that case do you know a tutorial ? Or can u guide me. I Appriciate your help BigEd. Thank you

Mikepoulsen
December 1st, 2009, 03:15 PM
This can even make it easyer for you Ed. I just want to make the ToolStrip menu work ? As simple as possible. Including Colurdialog

BigEd781
December 1st, 2009, 03:17 PM
A couple issues...

1. You do not close your StreamWriter in saveToolStripButton_Click (or the load method) if one of the controls is null. Instead of calling close directly, wrap any object which implements IDisposable (as the StreamWriter and dialog do) in a using statement:


using( StreamWriter writer = new StreamWriter( sfd.FileName ) )
{
// code here
}
// when we get here 'writer' is automatically disposed.

This is the same as writing this code:


try
{
StreamWriter writer = new StreamWriter( sfd.FileName );
// more code here
}
finally
{
// writer will always be disposed.
writer.Dispose();
}

2. You have properly taken a reference to the active text box in every method except the method which changes the forecolor. Try this instead:


private void toolStripButton1_Click(object sender, EventArgs e)
{
Form activeChildForm = this.ActiveMdiChild;
if( activeChildForm != null )
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
if( activeTextBox != null )
{
using( ColorDialog colorDialog = new ColorDialog( ) )
{
if (colorDialog.ShowDialog() != DialogResult.Cancel)
{
activeTextBox.ForeColor = colorDialog1.Color;
}
}
}
}
}


3. You have a lot of repeated code. Instead of using the same "get active form, if active form != null, get active control, if active control != null blah blah blah", wrap that in a function instead. Now you just implement the function with that code and call GetActiveTextBox() or something in your methods.

private void GetActiveTextBox()
{
Form activeChildForm = this.ActiveMdiChild;
return ( activeChildForm == null ) ? null : ActiveChildForm.ActiveControl as TextBox;
}


4. Your methods allow the user to select a menu item even if it is not a valid action, i.e., they do not have a TextBox selected. In this case nothing happens, which is a bad design and gives no feedback to the user as to why it is not working. Instead, I would disable any controls that should not be active when a TextBox does not have focus.

Mikepoulsen
December 1st, 2009, 03:34 PM
I dont get errors anymore from ColorDialog, But i can't understand what u mean: Step 1&2.

Maybe you could try to explain me with: delete that, then paste this and copy that into this etc.

Thank you BigEd for helping me ;)

BigEd781
December 1st, 2009, 03:52 PM
Well, #2 is simple; you never declared activeTextBox as a variable in the method's scope, you just used the name.

#1 is a little more complicated, but not hard. Classes that retain references to non-managed resources implement an interface named IDisposable. If a class implements this interface it tells users of the class that they need to call the Dispose method before on an instance of the class before it goes out of scope.

Take the example of a Form. Simply put, a .NET form is a wrapper around a Win32 form. The operating system maintains a list of window handles, one per form. This is a limited resource and is not managed by the .NET runtime. So, before a form goes out of scope you need to call its Dispose method so that it releases any native resources it maintains, one of which is a window handle (commonly referred to an "HWnd"). If you do not call Dispose you at best end up with a memory leak.

So, C# provides some syntactic sugar for calling Dispose because it is such a common pattern and it is easy to forget to call the method yourself. The StreamWriter class implements IDisposable and, inside its Dispose method, calls Close. So you only need to call Dispose, not Close, and you can do so as I showed you in my example.

Mikepoulsen
December 1st, 2009, 03:56 PM
It wont work? :/

and the reason why im a stuppido is because i copy pasted. I'll be getting my books within next week.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int count; //Hver gang vi laver ny fil
Form mdiChild; //Vores Value
TextBox editTextBox; // Vores filtype

public Form1()
{
InitializeComponent();
count = 1; //Hver gang vi laver ny fil + count 1! ;)
}

private void printToolStripButton_Click(object sender, EventArgs e)
{
printDialog1.ShowDialog(); //Viser Print siden, Vi kalder på funktionen printdialog1.(Nu viser vi den)showdialog();
}

private void saveToolStripButton_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog(); //Her laver vi vores value

sfd.Title = "Save a Text File"; //Titlen på vores gemte fil
sfd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*"; //De filtyper vi kan gemme i. | er en opbremsning dvs |.html|txt. Så kan jeg gemme i html og txt. ( Bare for exempel)

DialogResult dr = sfd.ShowDialog();
if (dr == DialogResult.OK) // sammenligner.
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName); // Her skriver vi koden

using (StreamWriter writer = new StreamWriter(sfd.FileName))
{
Form activeChildForm = this.ActiveMdiChild; // Den "Nuværende form"

if (activeChildForm != null)
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;

if (activeTextBox != null)
{
sw.Write(activeTextBox.Text);
}


}
}



}

}

private void openToolStripButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog(); // Foregår på nogenlunde samme måde som save, det her er bare en open..

ofd.Title = "Open a Text File";
ofd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";

DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName);

Form activeChildForm = this.ActiveMdiChild;

if (activeChildForm != null)
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;

if (activeTextBox != null)
{
activeTextBox.Text = sr.ReadToEnd();
}

sr.Close();
}
}

}

private void copyToolStripButton_Click(object sender, EventArgs e)
{
Form activeChildForm = this.ActiveMdiChild;

if (activeChildForm != null) //Foregår på samme måde som cut og paste, den her er bare Copy
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
if (activeTextBox != null)
{
activeTextBox.Copy();
}
}

}

private void newToolStripButton_Click(object sender, EventArgs e)
{
mdiChild = new Form(); //Vi laver en "Value" dvs. Vi kalder en ny form frem her
mdiChild.Text = "Unavngivet" + count.ToString(); //Her laver vi navnet på den nye form. +count.tostring(); betyder at vi sætter et 1-tal for enden når vi opretter en ny "Form"
mdiChild.MdiParent = this; //bestemmer hvordan den skal komme frem.
editTextBox = new TextBox(); //Bestemmer vi filtypen
editTextBox.Multiline = true; // Og at vi kan lave flere linier
editTextBox.Dock = DockStyle.Fill; //Og at vi kan bruge hele vores "Form"
mdiChild.Controls.Add(editTextBox); //Giver os lov til at redigere den
mdiChild.Show(); //Viser vores form
count++; // det gør at vi kan gøre det her for evight

}

private void cutToolStripButton_Click(object sender, EventArgs e)
{
Form activeChildForm = this.ActiveMdiChild; // Her sætter vi en value

if (activeChildForm != null) // Her siger vi , hvis nu at..
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
if (activeTextBox != null) //activeTextBox.Cut(); Viser at det er en cut funktion, så derfor copierer den og sletter det man kopierede"
{
activeTextBox.Cut(); // hvis du at man har markeret noget og trykker på denne knap, så cutter man
}
}

}

private void pasteToolStripButton_Click(object sender, EventArgs e)
{
Form activeChildForm = this.ActiveMdiChild;
// Her fortæller vi at hvis der er noget kopieret så skal den overskrive det. Når den ind slætter / Paster.
if (activeChildForm != null)
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
if (activeTextBox != null)
{
activeTextBox.Paste(); //Paste
}
}

}

private void toolStripButton1_Click(object sender, EventArgs e)
{
Form activeChildForm = this.ActiveMdiChild;
if (activeChildForm != null)
{
TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
if (activeTextBox != null)
{
using (ColorDialog colorDialog = new ColorDialog())
{
if (colorDialog.ShowDialog() != DialogResult.Cancel)
{
activeTextBox.ForeColor = colorDialog1.Color;
}
}
}
}
}

private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{

}
}

}


Just so you know im trying to do my best

BigEd781
December 1st, 2009, 04:09 PM
Honestly, to help I need more than "It won't work". I really don't have time to debug your code. If you could just tell me (us) what you have tried and what, specifically, is not working I can help further.

Mikepoulsen
December 2nd, 2009, 12:16 AM
Okay sorry.
When i debugg it, the color of the current textbox wont change when i press the colordialog.
I press the button, the colordialog pops up, i change color and nothing happens. Setting breakopoint dosnt tell anything..

Mikepoulsen
December 2nd, 2009, 05:00 AM
Hello Mr BigEd!! Ive made a new thread with codes i do now understand. I wrote them myself this time ;) Thanks for your help here im learned a few things. ill bookmark it ;). But please look at my new thread.

Thanks, Mike Poulsen