|
Member 11367553 wrote:
if (line.Contains(":"))
proxies.Add(line);
proxyView.Items.Add(line);
I believe there's a bug here? Missing {} brackets, that is.
Best,
John
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
Is there a way to prevent a method from execution if it is in design time or the method can only be executed at runtime.
I got this problem while creating a custom control because there is a method call in the constructor that will only work at runtime.
now at design time while designing the form and use that control, then the form will generate the error. now i tried this at the constructor of the user control
public ctrl_information()
{
InitializeComponent();
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return;
SomeMethod();
}
what i want to achieve is something like this
[ExecuteOnlyAtRuntime]
public void SomeMethod()
{
}
then call it like this
public ctrl_information()
{
InitializeComponent();
SomeMethod();
}
Is it possible?
Please shed some light on this.
Thank you
|
|
|
|
|
As far as I know there is no such attribute predefined. I could be wrong, though.
You could always design your own attribute, Writing Custom Attributes[^], but I don't really see the point in this case.
What you want is for Visual Studio to recognize your method as executable only at runtime, without any extra code necessary.
However, if you write your own attribute you also need to write the code to access it, Accessing Custom Atributes[^].
That would be a lot more work than doing it like this
public void RunTimeOnlyMethod()
{
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
return;
}
This way you move the logic into the method that knows if it should be executed or not, instead of having logic in the calling method.
|
|
|
|
|
Gilbert Consellado wrote: Is there a way to prevent a method from execution if it is in design time The code does not run in the designer, it gets compiled. If the designer raises an error then you need to show us what it is.
|
|
|
|
|
I have a process inside the constructor of the usercontrol, then i get this error "The given key was not present in the dictinoary" on the designer from the form that uses the usercontrol.
It happen because at the design time the form will try to connect to the database but the connectionstring can be generated at runtime.
|
|
|
|
|
You don't need an attribute, there is a Control property which does this for you: DesignMode[^]
So all you have to do in your constructor is:
public ctrl_information()
{
InitializeComponent();
if (!DesignMode)
{
SomeMethod();
}
} Because a Form is derived from Control, this works there as well.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
unfortunately, it doesn't work on me
|
|
|
|
|
So show us exactly what code you are using (relevant bits, only, please!)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
here is the constructor of the usercontrol
public ctrl_patient_information()
{
InitializeComponent();
InitializeControl();
}
public void InitializeControl()
{
Patient = Patient ?? new Patient();
PatientContact = Patient.GetPatientContact();
PatientImage = new Patient.PatientImage(Patient);
pictureedit_patient.Image = PatientImage.GetPatientImage();
PopulateCombos();
InitValidation();
Patient.PropertyChanged += Patient_PropertyChanged;
InitControlBinding();
}
private void PopulateCombos()
{
combo_titles.Properties.Items.AddRange(new PatientTitle[] { PatientTitle.MR, PatientTitle.MS });
combo_titles.SelectedIndex = -1;
combo_titles.Select();
combo_gender.Properties.Items.AddRange(new Gender[] { Gender.Male, Gender.Female });
combo_gender.SelectedIndex = -1;
combo_religion.Properties.Items.AddRange(Religion.GetReligions(false));
combo_religion.Leave += Religion.ValidateControlReligionItem;
combo_marital_status.Properties.Items.AddRange(
new MaritalStatus[]
{
MaritalStatus.Single,
MaritalStatus.Married,
MaritalStatus.Widow,
MaritalStatus.Seperated
}
);
}
when the
Religion.GetReligions(false) been called it will try to connect to DB but the problem is the application is still in designmode, and the connectionstring for the DB is generated at runtime, so VS cant connect to db. then the Form that use the usercontrol will get an error at designtime.
public static BindingList<Religion> GetReligions(bool reset)
{
if (!reset && _bindingListItem.Count > 0) return _bindingListItem;
DisableRaiseChangedEvent();
_bindingListItem.Clear();
_bindingListItemPointer.Clear();
var ms = new MySQLSelect("SELECT * FROM `tbl_religion` WHERE `tracked_id` = @tid");
ms.AddParamWithValue("tid", HIMSHelper.TrackedID);
foreach (var item in ms.DataReader())
{
var r = new Religion()
{
ID = item[0].ToInt(),
ReligionName = item[2].ToString()
};
_bindingListItem.Add(r);
_bindingListItemPointer.Add(r.ID, r);
}
EnableRaiseChangedEvent();
return _bindingListItem;
}
for now i have a temporary workaround, just to make it work but still looking for the best solution, the only solution i can think is to create an attribute but as for now i dont have experience creating attribute and i am running out of time.
|
|
|
|
|
for now my workaround, instead having a condition
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return;
I just make
InitializeControl() public then call it separately on the Form that will use the control.
|
|
|
|
|
That's not a good idea - since your control relies on it in order to work at all, it kinda breaks OOPs to make the container remember to call it.
I'd probably just change it to this:
public void InitializeControl()
{
if (!DesignMode)
{
Patient = Patient ?? new Patient();
PatientContact = Patient.GetPatientContact();
PatientImage = new Patient.PatientImage(Patient);
pictureedit_patient.Image = PatientImage.GetPatientImage();
PopulateCombos();
InitValidation();
Patient.PropertyChanged += Patient_PropertyChanged;
InitControlBinding();
}
}
So that the actual content didn't get filled in except in run mode.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I dont know why but DesignMode is not working, its still execute the whole block inside the "if" statment.
and it give me this messages when opening the form.
Exception has been thrown by the target of an invocation.
The variable 'ctrl_patient_information' is either undeclared or was never assigned.
I think i am gonna use LicenseManager.UsageMode for now.
BTW thank you for your help
|
|
|
|
|
Assuming that you mean Debug and Release, you can use pre-processor directives to do what you want:
public ctrl_information()
{
InitializeComponent();
#if(!DEBUG)
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return;
#endif
SomeMethod();
}
|
|
|
|
|
Rectangle a=new Rectangle(); Arraylist b=new Arraylist; b.add(a); a.X=1;
When i debug these codes, i found b[0] is not a 's reference, they are not same, b[0].x is 0, not 1.
I am fuzzy to gdi+, i need put all shapes in a Arraylist, when they are update(move or change shapes),i can re-draw all of it in a canvas.
So how can i do it?
|
|
|
|
|
The problem is caused by the Rectangle being a structure (i.e. a Value type). So when you add it to the ArrayList it stores the values of the variable rather than a reference.
|
|
|
|
|
Then how can i do if i want add all different shapes in a list? i need to update properties of them.
|
|
|
|
|
See George's suggestion below.
|
|
|
|
|
First of all ArrayList is not recommended as the performance is not the best. See the Remarks section in ArrayList Class[^]
That said, as you cannot change the values as the Rectangle is a struct and not a class, it might be better if you implement an interface and/or an abstract base class, and then a class for each shape you want to have, Rectangle, Circle, Triangle etc. (Or whatever it is you want)
The specific shape classes all inherit the base class.
The benefit of this approach is that you can share common properties, such as Position and Size but also implement shape specific a Paint method for each class.
public abstract class Base
{
public Base()
{
location = new Point();
}
public abstract void Paint(Graphics g);
public string ShapeName { get; protected set; }
private Point location;
public Point Location
{
get
{
return location;
}
set
{
location = value;
}
}
private Size size;
public Size Size
{
get
{
return size;
}
set
{
size = value;
}
}
public int X
{
get
{
return location.X;
}
set
{
location.X = value;
}
}
}
public class Square : Base
{
public Square(Point location, Size size)
: base()
{
ShapeName = "Square";
Location = location;
Size = size;
}
public override void Paint(Graphics g)
{
}
}
Then
List<Base> shapes = new List<Base>();
shapes.Add(new Square(new Point(), new Size(30, 30)));
shapes[0].X = 10;
|
|
|
|
|
Thank you very much.
I can't understand very well, you mean that the best method is i need new a rectangle class inherit from BaseShape ,use location and Size property, and no need to use rectangle struct ,is it right?
|
|
|
|
|
You can also use a Rectangle struct internally inside the base class.
I just made a quick example to show the concept.
|
|
|
|
|
Hello,
I am looking to be able to control the child form that is created from showDialog(), basically I am trying to make a program similar to powerpoint, where you can control what is being shown on a child form. I have no code as I have not yet figured out how to do this, I am looking for any tutorials, lessons, or advise on how best to do it, or if it is even possible.
The other option I have is to use a chat server and manipulate it to do what I need, but that seems a bit over bearing for just some controls..
Thank you for any help
|
|
|
|
|
The only way to do this is to show the child form non-modally so that your calling thread remains available to process messages in the controller form. So you'd have to use the Show() method instead of ShowDialog().
(I think that's what it's called. )
A simple way to accomplish this is to make public methods on your child form class that are called from the controlling form. The controller can pass any data that way to the child form.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
This helped, Thank you
|
|
|
|
|
Actually, I have a follow up question if you could help a bit more,
I am using showDialog, but then sending data to the child through the public methods, as per your suggestion, but I was wondering, right before I close the child form, is there a way to send any info back to the parent form? (without causing an infinite loop?)
Thank you once again
|
|
|
|
|
If you are using ShowDialog(), which launches the child form modally, then yes, you could expose a public method on the parent form and call it from the child form as long as the function does not try to re-launch the child form.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|