|
I had to do something similar to this myself. I used reflection. Here's kinda what I would do:
char endChar = cb.Name[cb.Name.Length - 1];
FieldInfo fiNumL = this.GetType().GetField("numericUpDown" + endChar + "L");
FieldInfo fiNumR = this.GetType().GetField("numericUpDown" + endChar + "R");
FieldInfo fiMath = this.GetType().GetField("cbMath" + endChar);
Control cNumL = fiNumL.GetValue(this) as Control;
Control cNumR = fiNumR.GetValue(this) as Control;
Control cMath = fiMath.GetValue(this) as Control;
cNumR.Visible = (cNumL.Visible && cMath.Text == "Range");
It's not complete, but hopefully it makes sense. If you're not comfortable with reflection, just shorten your if statements:
this.numericUpDownGR.Visible = this.numericUpDownGL.Visible && this.cbMathG.Text == "Range";
Logifusion[^]
|
|
|
|
|
Hmmm I like the suggestion. I'll look into these possiblites. Thanks!
|
|
|
|
|
The 2nd suggestion made by LogiFusion makes more sense to me. Its much straightforward. It helped to shrink the switch statement from 151 lines to 46 lines.
Thanks a heap!
|
|
|
|
|
Nothing like a challange. 36 lines.
private void do(Contol upDown, Control upDown2, string math, Control picker, Control picker2) {
upDown2.Visible = (upDown.Visible && math == "Range");
picker2.Visible = (picker.Visible && math == "Range")
}
private void cbMath_IndexChanged(object sender, System.EventArgs e) {
switch (((ComboBox)sender).Name) {
case "cbMathA":
do(this.numericUpDownAL, this.numericUpDownAR, this.cbMathA.Text, this.dateTimePickerAL, this.dateTimePickerAR);
break;
case "cbMathB":
do(this.numericUpDownBL, this.numericUpDownBR, this.cbMathB.Text, this.dateTimePickerBL, this.dateTimePickerBR);
break;
case "cbMathC":
do(this.numericUpDownCL, this.numericUpDownCR, this.cbMathC.Text, this.dateTimePickerCL, this.dateTimePickerCR);
break;
case "cbMathD":
do(this.numericUpDownDL, this.numericUpDownDR, this.cbMathD.Text, this.dateTimePickerDL, this.dateTimePickerDR);
break;
case "cbMathE":
do(this.numericUpDownEL, this.numericUpDownER, this.cbMathE.Text, this.dateTimePickerEL, this.dateTimePickerER);
break;
case "cbMathF":
do (this.numericUpDownFL, this.numericUpDownFR, this.cbMathF.Text, this.dateTimePickerFL, this.dateTimePickerFR);
break;
case "cbMathG":
do (this.numericUpDownGL, this.numericUpDownGR, this.cbMathG.Text, this.dateTimePickerGL, this.dateTimePickerGR);
break;
}
}
---
b { font-weight: normal; }
|
|
|
|
|
Guffa wrote: Nothing like a challange.
Did I hear challenge?!?
cbMathA.Tag = new Infos(numericUpDownAL, numericUpDownAR, dateTimePickerAL, dateTimePickerAR);
cbMathB.Tag = new Infos(numericUpDownBL, numericUpDownBR, dateTimePickerBL, dateTimePickerBR);
cbMathC.Tag = new Infos(numericUpDownCL, numericUpDownCR, dateTimePickerCL, dateTimePickerCR);
cbMathD.Tag = new Infos(numericUpDownDL, numericUpDownDR, dateTimePickerDL, dateTimePickerDR);
cbMathE.Tag = new Infos(numericUpDownEL, numericUpDownER, dateTimePickerEL, dateTimePickerER);
cbMathF.Tag = new Infos(numericUpDownFL, numericUpDownFR, dateTimePickerFL, dateTimePickerFR);
cbMathG.Tag = new Infos(numericUpDownGL, numericUpDownGR, dateTimePickerGL, dateTimePickerGR);
cbMathH.Tag = new Infos(numericUpDownHL, numericUpDownHR, dateTimePickerHL, dateTimePickerHR);
public struct Infos {
public Control UpDown;
public Control UpDown2;
public Control Picker;
public Control Picker2;
public Infos(Control upDown, Control upDown2, Control picker, Control picker2)
{
UpDown = upDown;
UpDown2 = upDown2;
Picker = picker;
Picker2 = picker2;
}
}
private void cbMath_IndexChanged(object sender, System.EventArgs e) {
Infos info = (Infos)((ComboBox)sender).Tag;
info.UpDown2.Visible = (info.UpDown.Visible && ((ComboBox)sender).Text == "Range");
info.Picker2.Visible = (info.Picker.Visible && ((ComboBox)sender).Text == "Range");
}
Addition:
Just noticed that replacing the 4 Control fields with one array could safe another 6 lines but that would probably look a bit ugly.
-- modified at 15:32 Friday 16th June, 2006
|
|
|
|
|
Seems I enjoy seeing such challenge... But I'm deciding on the options as people input the ideas. I do have other areas in my code is somewhat similiar to this case and will see which will fit for few Methods I would use.
I will announce who's the "winner" for the "competition" sometime this weekend.
~RonBou (I've got 2 accounts with Codeproject.)
-- modified at 15:51 Friday 16th June, 2006
|
|
|
|
|
Robert Rohde wrote: Just noticed that replacing the 4 Control fields with one array could safe another 6 lines but that would probably look a bit ugly.
Or perhaps even replace the entire struct with an array?
cbMathA.Tag = new Control() {numericUpDownAL, numericUpDownAR, dateTimePickerAL, dateTimePickerAR};
cbMathB.Tag = new Control() {numericUpDownBL, numericUpDownBR, dateTimePickerBL, dateTimePickerBR};
cbMathC.Tag = new Control() {numericUpDownCL, numericUpDownCR, dateTimePickerCL, dateTimePickerCR};
cbMathD.Tag = new Control() {numericUpDownDL, numericUpDownDR, dateTimePickerDL, dateTimePickerDR};
cbMathE.Tag = new Control() {numericUpDownEL, numericUpDownER, dateTimePickerEL, dateTimePickerER};
cbMathF.Tag = new Control() {numericUpDownFL, numericUpDownFR, dateTimePickerFL, dateTimePickerFR};
cbMathG.Tag = new Control() {numericUpDownGL, numericUpDownGR, dateTimePickerGL, dateTimePickerGR};
cbMathH.Tag = new Control() {numericUpDownHL, numericUpDownHR, dateTimePickerHL, dateTimePickerHR};
private void cbMath_IndexChanged(object sender, System.EventArgs e) {
Control() info = (Control())((ComboBox)sender).Tag;
info(1).Visible = (info(0).Visible && ((ComboBox)sender).Text == "Range");
info(3).Visible = (info(2).Visible && ((ComboBox)sender).Text == "Range");
}
And, yes, readability and extremely short code doesn't play well together.
---
b { font-weight: normal; }
|
|
|
|
|
We could continue and probably end up in one single line... which noone could ever understand
|
|
|
|
|
hi every one
i'm working on a win application and i'm kinda stuck in security issue as i want to make my main form to get the local pc mac addres and check it with a prestored value so that it continues working or exit application.
here what i need "i need the code to get mac address and show it in a textBox in a form?" this maybe a stupied quest to some of u but i appriciate helping
Hesham Hafez
|
|
|
|
|
Does this[^] article help?
[edit]
Sorry, see this[^] article.
[/edit]
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
-- modified at 12:58 Friday 16th June, 2006
|
|
|
|
|
well thanks for helping but i don't know how to make use of a console application output to make it displayed in form applicaiton i know that sounds stupied but i'm not really good at c# so please advice
Hesham Hafez
|
|
|
|
|
The console application has nothing to do with the solution. Try this (where m_label is a label on your form):
ManagementClass myManagementClass = new ManagementClass
("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = myManagementClass.GetInstances();
m_label.Text = string.Empty;
if (moc.Count > 0)
m_label.Text = mo["MacAddress"].ToString();
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
ok i have tried this code it runs with no errors but still dosent show mac address into the textBox1
public string GetMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress=String.Empty;
foreach(ManagementObject mo in moc)
{
if(MACAddress==String.Empty) // only return MAC Address from first card
{
if((bool)mo["IPEnabled"] == true) MACAddress= mo["MacAddress"].ToString() ;
}
mo.Dispose();
}
MACAddress=MACAddress.Replace(":","");
return MACAddress;
this.textBox1.Text=MACAddress.ToString();
}
can u advice what is wrong with it?
Hesham Hafez
|
|
|
|
|
Your code could be failing for several reasons. You could start by printing the "MacAddress" property of every ManagementObject you encounter. Be sure to compare the value of the property with null before attempting to print its value.
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
well i tried this if that what u meant by checking with null but still dosn't return any thing to the textbox
if (moc.Count > 0)
{
this.textBox1.Text=MACAddress.ToString();
}
please modify my code to what u think will make it work
Hesham Hafez
|
|
|
|
|
I tried using the MAC and other various hardware signatures like the motherboard serial, bios serial, MAC Address to license my products but I found the MAC address reported is not always consistent. At first I thought it was laptop mac addresses reporting differently because of the transition to wireless/hard wired nics but I found some of the pc's were reporting different MAC addresses as well. I verified that the MAC addresses were indeed the item causing the trouble and verified that the hardware had not been changed out.
I have since ceased using the MAC address for product licensing and the problem has gone away. I would recommend using other hardware signatures and use as many as possible as some Bios serial numbers, motherboard serials, etc... still come back with empty strings or generic strings like "..Empty", "..None", "N/A", etc...
I would strongly discourage using the MAC as it only leads to embarassment down the road after your product is deployed and the customers are complaining your licenses are no longer valid.
For what it's worth...
|
|
|
|
|
well yes i agree with u that mac addess is not the securest referance that u can depend on as it changes when the local pc is connected to the internet and besides the mac adress spoofers .
can u sudgest another h/w signeture and can i programatically do this in a forms enviroment ?
Hesham Hafez
|
|
|
|
|
WMI (Windows Managment Instrumentation) will get you access to an assortment of hardware information.
To start, I would recommend
"Win32_Bios","SerialNumber"
"Win32_BaseBoard","SerialNumber"
but these are still sometimes blank even on newer boxes so I would search through the various items available. The Microsoft Platform SDK has some neat WMI browser tools. Also, MSDN has documentation in "Setup and System Administration".
moro145 wrote: programatically do this in a forms enviroment
I can't help you there as I'm a C++ programmer and I know nothing about Windows Forms programming. I was just wandering by and I thought I'd warn you about the MAC address since I got burned by it before.
Good luck!
|
|
|
|
|
yah i'm finding my way now thanks a milion
Hesham Hafez
|
|
|
|
|
Is there a way for me to create a button that isnt square or rectangular?
or maybe something similar
|
|
|
|
|
|
See this thread.
- It's easier to make than to correct a mistake.
|
|
|
|
|
1. is it possible for me to create an application in C# that will allow me to run my hyper terminal or is it possible for me touse commands in my application that will allow me to start programs on my computer????
2. is it possible for two programs to be writing to the same file at the same time?????
kenny
|
|
|
|
|
hiii
if i understood what u wana do right this code may be helpful
you can do so using the System.Diagnostics.Process.Start method as follows:
//just specifying the document name will open the appropriate app based on system settings
Process.Start("Http://msdn.microsoft.com")
//open a word doc
Process.Start(AWordDocument.doc")
Hesham Hafez
|
|
|
|
|
Thanks thats exactly what i wanted to do ...it works fine..............do u know anything about hyper terminal os rs232 connections?
kenny
|
|
|
|
|