|
Hi guys!
I need some help please, I have a treeview with many nodes and the nodes and sub nodes. i.e.
-Root
---Nodes
-----sub nodes
My nodes have a context menu which allows me to disable the subnodes and hence change the imageindex of the subnodes to an inactive icon. When I make this change I reload all the nodes and I would like to reload and go to the NODE on which I made the change. Basically refresh the treeview and jump to the make the selected node the node that I made the change on.
Please help!!!
sasa
|
|
|
|
|
With this c# code I try to write vb6 code. I am a newbie in C# and I think this is not a good approach to my need. I want to create a list of different objects that write the code, but list admit only the same kind of classes (type-safe, right?). I Know i can resolve this using List<object>, but your opinions are useful.
In the example below I do this.
root (form)
|
|--- eFrame
| |
| |--- eTxt (TextBox)
|
eLbl (label)
Example.
--------
public class Tester
{
static void Main()
{
FormControl root = new FormControl("Main", "frmMenu");
Element eFrm = new Element(root);
TextBoxControl txt = new TextBoxControl("txtName",false, " ");
Element eTxt = new Element(txt);
LabelControl lbl = new LabelControl("Name", " ");
Element eLbl = new Element(lbl);
FrameControl frame = new FrameControl("Frame", " ");
Element eFrame = new Element(frame);
frame.AddChild(eTxt);
root.AddChild(eLbl);
root.AddChild(eFrame);
Console.WriteLine(root.GenerateCode());
}
public class BaseControl
{
protected string name;
protected string indent = "";
protected List<element> elementsList;
public BaseControl(string name, string indent)
{
this.name = name;
this.indent = indent;
elementsList = new List<element>();
}
public string Indent
{
get { return indent; }
set { indent = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public void AddChild(Element e)
{
elementsList.Add(e);
}
}
public class FormControl : BaseControl
{
private string caption;
public FormControl(string caption, string name)
: base(name, "")
{
this.caption = caption;
}
public string GenerateCode()
{
string code = "";
code += indent + "Begin Form code\n";
foreach (Element e in elementsList) { code += e.GenerateCode(); }
code += indent + "End Form code\n";
return code;
}
}
public class FrameControl : BaseControl
{
private int index;
public FrameControl(string name, string indent)
: base(name, indent)
{
index = -1;
}
public FrameControl(string name, int index, string indent)
: base(name, indent)
{
this.index = index;
}
public int Index
{
get { return index; }
set { index = value; }
}
public string GenerateCode()
{
string code = "";
code += indent + "Begin Frame code\n";
foreach (Element e in elementsList) { code += e.GenerateCode(); }
code += indent + "End Frame code\n";
return code;
}
}
public class LabelControl : BaseControl
{
private string caption;
private static int index;
public LabelControl(string caption, string indent)
: base("", indent)
{
this.caption = caption;
index = 0;
}
public string GenerateCode()
{
string code = "";
code += indent + "Begin label code\n";
code += indent + " ....\n";
code += indent + "End lable code\n";
return code;
}
}
public class Element
{
private delegate string GenCodeDelegate();
private GenCodeDelegate doCode;
#region ' Contructor overloaded '
public Element(LabelControl lbl)
{
doCode = new GenCodeDelegate(lbl.GenerateCode);
}
public Element(TextBoxControl txt)
{
doCode = new GenCodeDelegate(txt.GenerateCode);
}
public Element(FormControl frm)
{
doCode = new GenCodeDelegate(frm.GenerateCode);
}
public Element(FrameControl frame)
{
doCode = new GenCodeDelegate(frame.GenerateCode);
}
#endregion
public string GenerateCode()
{
string code = "";
code = doCode();
return code;
}
}
Thanks in advance.
Gerard
Sorry for my english.
|
|
|
|
|
Hi,
I would define your class BaseControl as abstract (public abstract class BaseControl... ) and add a abstract method to it:
public abstract string GenerateCode();
Now every inheriting class needs to overwrite it:
public overridestring GenerateCode() {
}
Now its sufficient that your list only hold BaseControl instances and you won't need the class Element anymore.
Robert
|
|
|
|
|
Very useful, thank you!!.
|
|
|
|
|
As the subject says, how do I get a variable out of a user control?
I have a user control in my main form, which contains a text box. On pressing a button on the main form, I want a message box to pop up with whatever I put into the textbox.
So how do you do this? (I can do everything but get the text from the contrl to the messagebox in my main form)
Just a note, I need it this way, because later on the program is going to take a bunch of variables from multiple UserControls, and pass it into a global array.
- Munty
|
|
|
|
|
Hi,
define a property in your user control and access it from your main form:
public string MtyTextBoxText {
get { return myTextBox.Text; }
}
public void DoSomething() {
MessageBox.Show(myUserControl.MyTextBoxText);
}
Robert
|
|
|
|
|
That worked perfectly, thanks.
Now for another (probably silly) question.
The user control gets created by a button press. It gets added to an array.
Now I want a button in the UserControl that can remove that particular UserControl from the GUI (and stop the code from seeing it)
Now I can remove it from the GUI with the button inside the UserControl, but I'm not sure how (or if) you can remove the user control from the array in the main form, from the button in the user control.
So is this possible? (I'm assuming some sort of event handler could be used)
If not, I can work around the problem, but it would be better (much better) to remove the control from the array.
- Munty
|
|
|
|
|
Hi,
yes I would use an event in this case and also let the form remove the control instead of letting the control remove itself:
public event EventHandler RemoveButtonPressed;
if (RemoveButtonPressed != null)
RemoveButtonPressed(this, EventArgs.Empty);
myUserControl.RemoveButtonPressed += new EventHandler(OnRemoveButtonPressed);
private void OnRemoveButtonPressed(object sender, EventArgs ea) {
MyUserControl ctrl = (MyUserControl)sender;
ctrl.RemoveButtonPressed -= new EventHandler(OnRemoveButtonPressed);
}
Robert
|
|
|
|
|
Cool, thanks!
I'll try that out tommorow then.
- Munty
|
|
|
|
|
Hi All,
I am converting some of my vb.net code to c# and have run into a problem.
<br />
Select Case True<br />
Case p.ParameterType Is GetType(String)<br />
Response.Write("paramater is a string")<br />
End Select<br />
the code above works but when I try to convert to c# I get the error: A constant value is expected.
<br />
switch (true)<br />
{<br />
case pInfo.ParameterType == typeof(String): <br />
Response.Write("is type of string");<br />
break;<br />
<br />
}<br />
Is there any way to do this in c#?
thanks
tom
|
|
|
|
|
I'm not sure this has to do anything with a switch statement, but try this one:
if(p.ParameterType is string) Response.Write("is type of string");
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
The variable goes in the switch part, the constants go in the case part.
switch(pInfo.ParameterType)
{
case typeof(string):
break;
}
Although I'm not sure how it handles Type s. I've only ever used a switch statement for numbers, strings and enumerators.
|
|
|
|
|
Thanks for the reply. I tried that but then I get the error:
A value of an integral type expected
this is the only work around that I can think of, but it isnt really an ideal solution:
switch (pInfo.ParameterType.Name)
{
case "String":
Response.Write("is type of string");
break;
}
I cant believe you cant do this in c#!
thanks
|
|
|
|
|
mcd2424 wrote: I cant believe you cant do this in c#!
I would imagine that C# is enforcing some additional restrictions in order to produce optimised code - something VB is trading for ease of coding.
|
|
|
|
|
Hi,
the switch statement isn't designed for such a thing. Use if-then-else-if instead:
if (pInfo.ParameterType == typeof(String)) {
Response.Write("is type of string");
} else if (...) {
} else if (...) {
}
Robert
|
|
|
|
|
Robert Rohde wrote: the switch statement isn't designed for such a thing.
Please explain
only two letters away from being an asset
|
|
|
|
|
Hi,
this simply means it can only work with values which are internally represented by integers. This applies to integer types, boolean and enumeration values. Don't ask me why MS didn't extend its capabilities - its just a fact.
Robert
|
|
|
|
|
Robert Rohde wrote: can only work with values which are internally represented by integers
Then I must be doing something wrong for this to work
string MyString = "Foo";<br />
switch(MyString)<br />
{<br />
case "Foo":<br />
break;<br />
case "Bar":<br />
break;<br />
default:<br />
break;<br />
}
only two letters away from being an asset
|
|
|
|
|
Hi,
as usual string seems to get a special treatment . Probably there are some more case where switch works, but the fact remains that its rather limited (compared to some other languages).
Robert
|
|
|
|
|
I have been playing with Access and starting to understand the relationship within tables. In a C# project, there are two ways to get access to a database. For example, I can use SQL statement to create a table; I can also use SQL statement to read value from a table.
Another way is to use the database wizard to create a table. One good thing with the database wizard is that you get access to the field directly by intellicense and the fields are easier to bind with the controls. I am just starting learning database. The problem I see with the wizard, if your database changes. For example, right now I am using an access file about if that change to oracle or any other type. How can I approach that? Will that be easier to modify. I want to know if scripting is better or using the wizard. What I like with the wizard is getting access to the field directly by intellicense and the easy biding. With the scripting, you can simply couple of lines of code. I also see more books and articles uses scripting instead of the wizard; maybe because the wizard is new. I want to know the benefit between scripting and the wizard witch gives direct access or creating to the database.
Another question I have is naming the fields of the database. For instance, which one is better or preferable? [Last Name] or [LastName], [Employee Number] or [EmployeeNumber], [Total Cost] or [TotalCost]; I prefer the single name rather than using space. So which one is better to use? I was looking at the Access Database Wizard, it uses single name rather than names with space. So I assume naming convention without space is better.
|
|
|
|
|
mfcuser wrote: question I have is naming the fields of the database. For instance, which one is better or preferable? [Last Name] or [LastName], [Employee Number] or [EmployeeNumber], [Total Cost] or [TotalCost]; I prefer the single name rather than using space.
I personally prefer no spaces, it saves me from headaches later on
Some people have a memory and an attention span, you should try them out one day. - Jeremy Falcon
|
|
|
|
|
Thank you I prefer that as well. About the wizard vs the script?
|
|
|
|
|
mfcuser wrote: About the wizard vs the script?
I rarely work with Access anymore. Wizard is fine, but if you want to port the tables to other databases, scripts are helpful. You'll just need to make any necessary minor modifications depending on what database you are migrating to
Some people have a memory and an attention span, you should try them out one day. - Jeremy Falcon
|
|
|
|
|
This is the way I look at it. Assume that I have an application and I created my table on access or sql. That means, my tables are access or sql. Now, assume some users want to use that application but with oracle or some other databases. I assume it would be better to use scripting.
Another way again, assume that I use access database, but some users want to use sql. So it will be changing some few lines of code to do that.
I also want to know if the wizard provides options for interchangeability. Basically, I want my application to have a wizard where user can choose the type of database to use for example, a user can choose access for local, sql for server or oracle for server etc. Assume that I use the wizard with access, will it be possible for me later to change that or have option to change it to sql, oracle etc.
|
|
|
|
|
mfcuser wrote: I also want to know if the wizard provides options for interchangeability. Basically, I want my application to have a wizard where user can choose the type of database to use
You want to read up on Data Abstraction Layers. The idea is that you have a variety of classes with the same interface, however, your application instantiates a specific class depending on the database to use. For the rest of the application all it sees is the interface, it doesn't care or need to know anything about the database. Only the DAL classes need to know about differnet databases.
|
|
|
|