|
Swelborn wrote: how I can have a async, fast read/write option for my logging?
Use Log4net[^]
|
|
|
|
|
I have a button that when clicked opens another form. I want it to check and see if the form is already open, and if it is maximize it/bring it to the front. Is this possible?
|
|
|
|
|
Hi !
Are you hiding or closing the form?
here is a method if your are closing the form:
public ParentForm : Form
{
ChildForm frm ; // I assume that your form is a member of the parent form
public ParentForm()
{
// Components
}
public DisplayAChildForm()
{
if (frm == null)
{
frm = new ChildForm();
frm.OnFormClosed += new EventHandler (frmClosed_evnt);
fmr.Show(); // you can use ShowDialog method also
}
}
private frmClosed_evnt(object sender, EventArgs e)
{
frm.OnFormClosed -= new EventHandler (frmClosed_event);
frm = null;
}
}
that's it
|
|
|
|
|
Hi,
In this way you either create or show the form without using events.
On click the ShowChild method is called.
Test for null or disposed and show it if this is the case.
If the form is ok we test if it is minimized then we restore it and then bring it to the front.
Note that Show brings the form to the front only on the first call.
Note - DO NOT CALL ShowDialog - it does not work if the form has been shown before Your application will crash with an InvalidOperationException
public partial class Form1 : Form
{
private Form childForm;
public Form1()
{
InitializeComponent();
}
private void ShowChild()
{
if (childForm == null || childForm.IsDisposed)
{
childForm = new Form2();
childForm.Show();
}
else
{
if (childForm.WindowState == FormWindowState.Minimized)
{
childForm.WindowState = FormWindowState.Normal;
}
childForm.BringToFront();
}
}
private void buttonShow_Click(object sender, EventArgs e)
{
ShowChild();
}
}
Natza Mitzi
|
|
|
|
|
Hi All,
I'm having real troubles with this. Say I've got two classes BaseClass and DerivedClass : BaseClass . How can I cast an instance of BaseClass and convert it into a type of DerivedClass . All I've done in DerivedClass is add two new properties, nothing fancy.
I've tried various methods:
BaseClass baseClass = new DerivedClass();
DerivedClass derivedClass = baseClass as DerivedClass;
DerivedClass derivedClass = (DerivedClass)baseClass;
The C# 3.0 in a Nutshell book uses this illustration which is also failing ...
DerivedClass derivedClass = new DerivedClass();
BaseClass baseClass = derivedClass;
I was under the impression that a Cast was 'workable' and legal provided there was no dataloss int to long for example. Since DerivedClass contains all of the BaseClass properties I was (obviously incorrectly) assuming that this would be an allowable cast. Is there a workaround that doesn't involve mapping each source property to the target type property?
Jammer
if(glass == Glass.Empty){GoToBarAndOrderMoreBeer();}
else {Drink();}
My Blog | Article(s)
|
|
|
|
|
Here's a sample to play around with. The derivation works fine with this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDerivation
{
class Program
{
static void Main(string[] args)
{
List<BaseClass> list = new List<BaseClass>();
list.Add(new BaseClass { Id = 1 });
list.Add(new Derived { Id = 2, Name = "Jammer" });
foreach (BaseClass item in list)
{
if (item is Derived)
{
Derived derived = item as Derived;
Console.WriteLine("{0} {1}", derived.Id, derived.Name);
}
else
{
Console.WriteLine("{0}", item.Id);
}
}
Console.Read();
}
}
public class BaseClass
{
public int Id { get; set; }
}
public class Derived : BaseClass
{
public string Name { get; set; }
}
}
|
|
|
|
|
Hi,
casting from derived class to base class is acceptable but not vice versa if the base class doesn't have all of the functionality that is defined in derived class (which is the usual situation). Consider following
class BaseClass {
public int value1;
}
class DerivedClass : BaseClass {
public int value2;
}
static void Main() {
DerivedClass dc = new DerivedClass();
BaseClass bc = dc;
BaseClass bc2 = new BaseClass();
DerivedClass dc2 = (DerivedClass)bc2;
}
Based on this your second example should be ok.
Mika
|
|
|
|
|
Thanks for this chap ...
Jammer
if(glass == Glass.Empty){GoToBarAndOrderMoreBeer();}
else {Drink();}
My Blog | Article(s)
|
|
|
|
|
So really the upshot of this is that in order to actually convert BaseClass into a DerivedClass (rather than just changing references) is by explicitly mapping all the properties from BaseClass to DerivedClass?
Jammer
if(glass == Glass.Empty){GoToBarAndOrderMoreBeer();}
else {Drink();}
My Blog | Article(s)
|
|
|
|
|
Since both objects have different signatures, as far as I know you must copy eaach property separately. This can of course be done by explicitely setting each property but also by using reflection in which case you can create a small dynamic loop to set each property.
Previously I have used both techniques a lot, but since .Net Framework 2.0, when generics were introduced, I've been able to get rid of most of the reflection loops by using generic definitions. I'm not sure if they could help you in your case, but if you're not familiar with them, I believe that it's well spent time to have a look.
Hope this helps,
Mika
|
|
|
|
|
Thanks for this info Mika.
Jammer
if(glass == Glass.Empty){GoToBarAndOrderMoreBeer();}
else {Drink();}
My Blog | Article(s)
|
|
|
|
|
You're welcome
|
|
|
|
|
I am having a combobox on my windows form. i want to display some records in it which is in the datatable. The datatable contains only 1 column
(lets say column name is products).Inside the selected index changed event of combobox I write this line of code:-
// count is no of rows in my datatable
// dt is datable.
for(i=0;i<count;i++)>
{
Combobox1.Items.Add(dt.rows[i])
}
problem is that my combobox is not populating records,but my datable has all records of products column in it.I am using connected architecture.
|
|
|
|
|
rahul21 wrote: Inside the selected index changed event of combobox I write this line of code:-
Why do you want to populate at this event? Shouldn't it be on form load.
Next, once you have the data in datatable, you can directly bind the combobox using the datasource property.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
rahul21 wrote: I am using connected architecture.
AFAIK, DataTable is not a connected architecture.
|
|
|
|
|
Hello!
Does anyone know how to send a ^ using the SendKeys class?
All other characters I tested work fine. ^ of course needs to be escaped using {^} (because ^ would press Ctrl), however when sending {^} it actually types a & ...
Best regards
Dominik
|
|
|
|
|
Hi,
noticed this on documentation:
If your application is intended for international use with a variety of keyboards, the use of Send could yield unpredictable results and should be avoided
while SendWait-method does not have this note.
Mika
|
|
|
|
|
Sorry, I should have pointed out that I'm already using the SendWait static method of the SendKeys class.
Any other idea?
Best regards
Dominik
|
|
|
|
|
If you change the keyboard layout to English, caret works just fine, but for example with Finnish layout, ampersand is sent instead caret (just like in your case). Since every other character works as expected, I would consider this as a possible bug.
Mika
|
|
|
|
|
Hi,
I'm creating a DAL for my project and I implemented IDisposable in my class because I wanted to dispose the SqlConnection object and SqlCommand object. The code goes like this:
public class CustomerDal:IDisposable
{
SqlConnection connection;
SqlCommand command;
private bool _IsDisposed = false;
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool IsDisposing)
{
if (_IsDisposed)
return;
if (IsDisposing)
{
if (connection != null)
connection.Dispose();
if (command != null)
command.Dispose();
}
_IsDisposed = true;
}
}
during compilation, I get the error:
new protected member declared in sealed class
I dont use any unmanaged objects, so I need to dispose only connection and command object in this class. Could anyone help me with this error?
Regards,
Blumen
|
|
|
|
|
Blumen wrote: new protected member declared in sealed class
I dont use any unmanaged objects, so I need to dispose only connection and command object in this class. Could anyone help me with this error?
If you seal the class then you cannot inherit from it. If you cannot inherit from it then protected is a pointless access modifier to use. Use private instead of protected .
protected means that only members of this class (and any nested classes) and any class that inherits from it. private means only this class (and any nested classes).
Also, the virtual keyword is also pointless in a sealed class.
|
|
|
|
|
Thanks alot, making it private gets rid of the error.
But dispose is a method of base class, so is it okay to remove the virtual keyword?
|
|
|
|
|
Blumen wrote: But dispose is a method of base class, so is it okay to remove the virtual keyword?
The virtual keyword is supposed to go in the base class. In the derived class you override the method.
|
|
|
|
|
Does that mean I have to override dispose on my class like:
private override void Dispose(bool IsDisposing)
and make class sealed
modified on Sunday, August 17, 2008 9:05 AM
|
|
|
|
|
Blumen wrote: Does that mean I have to override dispose on my class
If Dispose(bool) exists in the base class then yes.
Blumen wrote: and make class sealed
Sealing the class is not necessary - It was something that you wanted to do already.
Sealing a class means that nothing can inherit from the sealed class. If that is what you want then seal the class. If that is not what you want, or if you don't know whether you will want to inherit from the class then don't seal it.
|
|
|
|