|
Go to project properties. There, under Build tab, check allow unsafe code checkbox.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
thanks alot
|
|
|
|
|
after serching in msdn and internt i couldn't find any predefined section in application config file
to save the types formats (( shor or long for dates etc..))
is there such an element or cofiguration section to save this information
or do i have to create a custom configuration section for it
thanks
|
|
|
|
|
You can add a key under appsettings for the date format and then access it in the code using ConfigurationSettings.AppSettings["dateFormatKey"].ToString() .
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Hi I have the following code that is updating an dropdown box with information Active Directory information - this below will show Domain Controllers btw.
But I am trying unsuccessfully to cancel an existing running thread if one exist already. this is mainly because sometimes a thread can be running indefinitely and need to abort that, or if another thread is called that need to update the same dropdownbox. Anyway are there any out here who could explain how I could abort an running thread?
private void ThreadDC()
{
Thread TRDomainC = new Thread(ThreadingDC);
if (TRDomainC.IsAlive)
{
TRDomainC.Abort();
}
TRDomainC.SetApartmentState(ApartmentState.STA);
TRDomainC.Start();
}
private void ThreadingDC()
{
comboBox1.Items.Clear();
try
{
if (comboBoxDomains.SelectedIndex >= 0 && comboBoxDomains.SelectedIndex != -1)
{
foreach (string dc in domainInfoComponent[comboBoxDomains.SelectedItem.ToString()])
{
comboBox1.Items.Add(dc);
}
lastIndex = comboBoxDomains.SelectedIndex;
comboBox1.SelectedIndex = 0;
}
}
catch
{
lastIndex = 0;
comboBox1.SelectedIndex = -1;
}
}
|
|
|
|
|
Don't use Thread.Abort - it's evil!
If you want to end a thread, do it cooperatively - make the thread return from it's main method and it will be cleared up properly.
Also, you cannot touch UI controls from any thread other than your main UI thread. Control.Invoke is helpful here.
Here is a good introduction to threading: http://www.albahari.com/threading/[^]
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Not really an option waiting. So need to kill the thread one way or another.
I know about Control.Invoke, but well, dosn't really help me on ending the thread before it is completed.
Ronni
|
|
|
|
|
As Nick said; this[^] may help also.
|
|
|
|
|
Nope, not really. Well I could move to backgroundworker... But there should(right?) be a way to abort the thread without getting into the deep end.
|
|
|
|
|
As Nick and Albahari say, you should not even want to abort a thread, aborting it will leave a lot of objects in an unknown state, putting the app at the risk of a deadlock; how about things that may be locked and never will be unlocked again?
You really need the cooperative approach for thread control.
And a healthy way to access GUI Controls, hence the reference to my article.
|
|
|
|
|
I wrote small application - and i want to add some new feature that in case the user double click on the picture then the default edit application ( for example paint.exe application ) will be open the current picture.
My question are:
1. how can i know what is the default edit picture application that appear on current machine ( how can i know it in run-time ? )
2. how to open the picture in this application in run-time ?
Thanks.
|
|
|
|
|
Hi,
if you execute Process.Start(filename) you get the same result as when the file got double-clicked in Windows Explorer, so any document would get opened by its default application based on file extension associations (e.g. "demo.txt" would be opened by Notepad). So your app doesn't have to know a thing about default apps.
|
|
|
|
|
Hi there
Using Visual Studio 2008 Standard.
Was wondering if anybody could recommend an add-on to do UML diagrams as well as one for unit testing? Preferably freeware.
|
|
|
|
|
VS 2010 have both included
I use MS Visio for UML diagrams and Resharper for unit testing
|
|
|
|
|
Hello, I like to create a Label control with an property in which I can select the available controls on the Form (except for the Label controls).
One the respective control is set in Design time, this control will get the focus in Runtime when the user moves with the mouse over the Label control
I'm fairly new with creating Usercontrols. Can anyone help me?
Kind regards Arjen
|
|
|
|
|
This might be helpful to you:
Custom Controls in Visual C# .NET
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
Hello,
Just came across your interesting question.
If I got it write, I think you need to deal with inherited controls and a customzied property editor.
I will try to give you an idea of how you can solve your problem, but will not sneak into the time costing details (I will mark them for you).
ArjenGroeneveld wrote: like to create a Label control
What you need first is a custom label which derives from Forms.Label .
public class LabelExtended : System.Windows.Forms.Label
{
...
}
ArjenGroeneveld wrote: One the respective control is set in Design time,
To make it easy (for me), I will use a string property to hold the name of this selected control (how we do this will be next).
private string selectedControlName = string.Empty;
[
CategoryAttribute("Arjens Properties"),
DescriptionAttribute("Holds the name of the selected control."),
DefaultValueAttribute(""),
EditorAttribute(typeof(SelectControlEditor), typeof(UITypeEditor)) // this is the key
]
public string SelectedControlName
{
get { return selectedControlName; }
set { selectedControlName = value; }
}
ArjenGroeneveld wrote: an property in which I can select the available controls on the Form (except for the Label controls).
We just added an EditorAttribute to the property. This is a type of SelectControlEditor and derives from UITypeEditor , which we have to create now.
using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.Drawing.Design;
...
public class SelectControlEditor : UITypeEditor
{
...
}
What I want to show you is a DropDown editor, where you are able to select a control out of a list of control names.
First we give the editor class the information about the DropDown , by overriding the GetEditStyle method like this:
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if( context != null ) return UITypeEditorEditStyle.DropDown;
return base.GetEditStyle(context);
}
Now we need to tell the editor how to edit your property, by overriding the EditValue method.
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if ((context != null) && (provider != null))
{
IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (editorService != null)
{
Control owner = context.Instance as Control;
if (owner != null)
{
SelectControl selectControlEditor = new SelectControl(editorService, owner);
selectControlEditor.SelectedControl = (string)value;
editorService.DropDownControl(selectControlEditor);
if (selectControlEditor.Valid)
{
return selectControlEditor.SelectedControl;
}
else
{
return value;
}
}
}
}
return base.EditValue(context, provider, value);
}
Now we create an UserControl named SelectControl, which inherits Forms.UserControl .
On this UserControl we add a ComboBox named "cbSelectControls", and a Button named "bOk" (for the button we handle the Click Event).
I will make additional commends in the code:
public partial class SelectControl : UserControl
{
IWindowsFormsEditorService editorService = null;
public SelectControl(IWindowsFormsEditorService editorService, Control ownerControl)
{
InitializeComponent();
this.editorService = editorService;
if (ownerControl != null)
{
Control parentControl = ownerControl.Parent;
if (parentControl != null)
{
foreach (Control c in parentControl.Controls)
{
if (!c is LabelExtended)
{
cbSelectControls.Items.Add(c.Name);
}
}
}
}
}
public string SelectedControl
{
get { return cbSelectControls.Text; }
set { cbSelectControls.Text = value;}
}
bool valid = false;
public bool Valid
{
get { return valid; }
set { valid = value; }
}
private void bOk_Click(object sender, EventArgs e)
{
Valid = true;
editorService.CloseDropDown();
}
}
What we now have done is:
If you select your label in the designer and edit the SelectedControlName over the "..." button, an dropdown will be shown, with your UserControl on it.
On this UserControl you will hopefully find a combobox filled with the controls names, which are in the same collection as your label is.
If you now select a controls Name and click the ok Button, it should appear at the SelectedControlName property of your label.
ArjenGroeneveld wrote: this control will get the focus in Runtime when the user moves with the mouse over the Label control
For this task we could override the OnMouseEnter Method of your LabelExtended class, like this:
protected override void OnMouseEnter(EventArgs e)
{
if (!DesignMode)
{
if (!string.IsNullOrEmpty(SelectedControlName))
{
Control parentControl = this.Parent;
if (parentControl != null)
{
foreach (Control c in parentControl.Controls)
{
if (c.Name.Equals(SelectedControlName))
{
c.Focus();
}
}
}
}
}
base.OnMouseEnter(e);
}
Please note:
.)I haven't fully tested the code and didi some changes during editing with no compilation run.
.)The code as it is will only take care of controls which have the same parent Control than your label has. So some (not too hard I guess) work is still open. Don't forget to create unique names if you consider to look in subcontainer.
Please also note:
.) I had great fun looking for a solution for your problem, so thank you for asking!
I hope it helps you. (Would be nice to get a feeback)
All the best,
Martin
|
|
|
|
|
I wrote a word in the RichTextBox, this word represent an object. I want to click this object.
For example; I wrote following text to RichTextBox;
Hello codeproject members.
I want to "codeproject" word represent an object. May be get ID when click to this word.
How can I do? Thanks...
|
|
|
|
|
Hi,
Try like this may be its not correct approach. But it solves your problem.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string s1 = this.richTextBox1.Text;
sample o1 = new sample();
o1.sobject = s1;
}
}
public class sample
{
public string sobject;
}
To invent something, you need a mountain of junk in your mind.
---------------------Thomas alva edison
|
|
|
|
|
Program how to interact when cursor on this word?
|
|
|
|
|
i want to display a user control in place of default tooltip displayed.
Can smebody help me with the code????
thanks in advance....
|
|
|
|
|
For which control you want to do that ? You can trap Mouse move event and show some user control on mouse move.
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
i want to do this for picture box.
can u plz help wid the code snipet????
thanks....
|
|
|
|
|
I have a process which can be run from any number of servers. All of them access the same database. How do I synchronize the severs data access?
Here are the details.
My common database server has a table T1. It has columns C1, C2 and C3.
My process has the following steps.
1. Read T1 table.
2. If there is any row with value V1 for C1, wait in a loop and go to step-1.
3. If there are no such rows, insert a row with V1 for C1 and appropriate values for the rest of the columns.
4. Start my business logic.
5. Once the business logic is complete, delete the row entered in step-3.
Now the same process runs from different servers. So there is a chance that 2 or more processes will do steps 1 and 2 simultaneously and might end up creating 2 or more rows with V1 value for C1. I don't want that to happen. How will I make sure only once process can perform steps 1 and 2 at a time? I can not use lock and monitor to synchronize the access as I am running from different process spaces.
How do I do it then?
|
|
|
|
|
One way would be to create a separate table in the DB for locks (or just reuse the existing one). Before a process starts step 3 (or whatever the crucial step is) it checks that table for a well known value, if it exists then it knows that some other process has started business logic and it should wait. If the lock value isn't found then it can create it and continue on. The lock value that it created will signal other processes that it is currently executing business logic and they should wait.
This will work if the DB column that you're using for a lock is properly setup with a primary key constraint. If two processes try to write the same value into a PK column at (nearly) the same time the first will succeed and the other will throw an exception.
Another "Outside the Database" solution would be to make all of the processes communicate with each other via some other mechanism (sockets, MSMQ, File IO, etc) and work out that way which one should be doing what.
|
|
|
|