|
-
December 1st, 2009, 02:55 PM
#1
Special COlorDialog Code
Code:
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
Code:
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.
Code:
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..
-
December 1st, 2009, 03:15 PM
#2
Re: Special COlorDialog Code
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.
-
December 1st, 2009, 03:35 PM
#3
Re: Special COlorDialog Code
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
-
December 1st, 2009, 03:43 PM
#4
Re: Special COlorDialog Code
I mean, set a breakpoint on this line and look at what is actually going on:
Code:
activeTextBox.ForeColor = colorDialog1.Color;
-
December 1st, 2009, 03:45 PM
#5
Re: Special COlorDialog Code
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?
Last edited by Mikepoulsen; December 1st, 2009 at 03:47 PM.
-
December 1st, 2009, 03:59 PM
#6
Re: Special COlorDialog Code
 Originally Posted by Mikepoulsen
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.
-
December 1st, 2009, 04:04 PM
#7
Re: Special COlorDialog Code
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.
Code:
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
-
December 1st, 2009, 04:15 PM
#8
Re: Special COlorDialog Code
This can even make it easyer for you Ed. I just want to make the ToolStrip menu work ? As simple as possible. Including Colurdialog
-
December 1st, 2009, 04:17 PM
#9
Re: Special COlorDialog Code
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:
Code:
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:
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:
Code:
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.
Code:
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.
Last edited by BigEd781; December 1st, 2009 at 04:21 PM.
-
December 1st, 2009, 04:34 PM
#10
Re: Special COlorDialog Code
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
Last edited by Mikepoulsen; December 1st, 2009 at 04:36 PM.
-
December 1st, 2009, 04:52 PM
#11
Re: Special COlorDialog Code
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.
-
December 1st, 2009, 04:56 PM
#12
Re: Special COlorDialog Code
It wont work? :/
and the reason why im a stuppido is because i copy pasted. I'll be getting my books within next week.
Code:
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
Last edited by Mikepoulsen; December 1st, 2009 at 05:01 PM.
Reason: newpost
-
December 1st, 2009, 05:09 PM
#13
Re: Special COlorDialog Code
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.
-
December 2nd, 2009, 01:16 AM
#14
Re: Special COlorDialog Code
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..
-
December 2nd, 2009, 06:00 AM
#15
Re: Special COlorDialog Code
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|