|
If the buttons etc. are part of the base class, then you can't move them in the designer by editing the derived class - if you think about it that's the only sensible way to go, as your base class could be in a compiled DLL instead of being available via source code (and thus can't be changed anyway!)
You should see a little padlock at the top left of base class controls indicating they can't be moved or deleted.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Yes I suppose that's true - basically what I'm doing is dynamically loading the user controls into tab pages at runtime - some of them require slight modifications to the layout depending which page they are part off - I'll have to think of another way of achieving it - thanks for replying
We can’t stop here, this is bat country - Hunter S Thompson RIP
|
|
|
|
|
You're welcome!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You can manipulate the Controls in sub-classed Controls (or Forms) at run time.
If you're really interested in "getting deep," I'll show you how it is done ... and I do not refer to the hack of changing the Designer 'cs file access type to 'public for UserControls.
Let me know if you want to go further: I'm burned out on posting code here that gets no response.
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Definitely interested Bill
We can’t stop here, this is bat country - Hunter S Thompson RIP
|
|
|
|
|
Okay, keeping in mind what OriginalGriff said about not being able to move sub-classed Controls at design-time ... or edit any of their properties ...
By "publishing" a data structure that exposes the run-time created Controls: you can modify them any way you like. I prefer to create a Dictionary<string, Control> ... and, here's how I do that: note the code you see here uses Extension methods in the 'ControlExtensions class appended to this post ... depending on task at hand, I may want only to expose the top-level Controls, or I may want to recurse to get nested Controls.
Example: In the sub-classed UserControl Load EventHandler:
public Dictionary<string, Control> ControlDict;
private void SubClassOfBaseUserControl_Load(object sender, t[EventArgs e)
{
ControlDict = this.Controls.FindAll();
ControlDict["button1"].Text - "changed."
} The limits of this are, of course, that you can't access the unique properties of Controls ... those which are not inherited from Control ... unless you up-cast back to their native Type. That's as simple as using 'as, or as complex as using reflection when you don't know the Type to up-cast to.
Hope this helps, and I;m not saying you should do things this way. cheers, Bill
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace YourNameSpace
{
public static class ControlExtensions
{
public static Dictionary<string, Control> FindAll(this Control.ControlCollection controls, bool recurse = true)
{
if (recurse)
{
return GetAllControls(controls).ToDictionary(c => c.Name, c => c); ;
}
else
{
return controls.Cast<Control>()
.ToDictionary(c => c.Name, c => c);
}
}
public static IEnumerable<Control> GetAllControls(Control.ControlCollection controls)
{
Stack<Control> stack = new Stack<Control>(controls.Cast<Control>().ToArray());
while (stack.Any())
{
var nextControl = stack.Pop();
foreach (Control childControl in nextControl.Controls)
{
stack.Push(childControl);
}
yield return nextControl;
}
}
}
}
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Whoah interesting stuff Bill, did you ever find a way of visually removing controls ?
We can’t stop here, this is bat country - Hunter S Thompson RIP
|
|
|
|
|
pkfox wrote: did you ever find a way of visually removing controls Hi, do you mean something more than setting their 'Visible property to 'false ? ummm ... when you got a reference, you can do whatever:
ControlDict = this.Controls.FindAll();
Control theButton = ControlDict["button1"];
theButton.BackColor = Color.FromArgb(255, 230, 0);
theButton.Location = new Point(0, 0);
theButton.Hide();
ControlDict.Remove(theButton.Text);
theButton.Parent.Controls.Remove(theButton); To pursue this further and compare these rather hackish caniptions with another WinForm app strategy, it would interest me to have a clearer idea of what you want to achieve ... the other strategy would be using what I call the "switchboard model," with a static class handling all inter-form/control interactions and visual state changes... a poor-boy's MVC, perhaps.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
modified 2-Dec-18 11:33am.
|
|
|
|
|
Hi Bill, thank you for your time and effort on this. What I have is a Winform with a custom TabControl which loads custom TabPages at runtime , the TabPages in turn load their respective UserControls. The user controls are for the most part the same and as they implement an Interface have guaranteed data properties and methods. Recently a requirement arose which entailed removing and adding controls ( which is when I discovered this can't be done visually) The application is a front end for a report generator which basically passes parameters to stored procedures and returns data in the form of Excel spreadsheets. I think I can achieve this using your method but if you know of another way please let me know, once again thank you for your time.
We can’t stop here, this is bat country - Hunter S Thompson RIP
|
|
|
|
|
Okay, first take a look at this: [^] and this: [^] ... a custom Designer, and the right Attributes on the UserControl class definition, can enable a UserControl to have Controls drag-drop added at design-time.
It's been a while since I have messed with doing this, and I don't think that design-time facility will extend to sub-classed UserControls ... but, I'm not sure.
A custom designer (one I actually wrote) might look like this:
using System.ComponentModel;
using System.Windows.Forms.Design;
namespace YourNameSpace
{
public class TestControlDesigner : ParentControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
if (Control is InheritedPanel)
EnableDesignMode((InheritedPanel) Control, Control.Name);
else if (Control is InheritedPanelHost) EnableDesignMode((InheritedPanelHost) Control, Control.Name);
}
}
} Note the reference to 'InheritedPanel and 'InheritedPanelHost above: Classes I created:
using System.ComponentModel;
using System.Windows.Forms;
namespace YourNameSpace
{
[Designer(typeof(TestControlDesigner))]
public partial class InheritedPanel : UserControl
{
public InheritedPanel()
{
InitializeComponent();
}
}
} Do familiarize yourself with Attributes that enable design-time property-grid editing as shown in use here;
using System.ComponentModel;
using System.Windows.Forms;
namespace YourNameSpace
{
[Designer(typeof(TestControlDesigner))]
public partial class PanelBase : UserControl
{
public PanelBase()
{
InitializeComponent();
}
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TreeView TV { get; private set; }
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Panel TopPanel { get; private set; }
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Panel LowerPanel { get; private set; }
public void setPanelLabel(string s)
{
label1.Text = s;
}
private void PanelBase_Load(object sender, EventArgs e)
{
}
}
} cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
modified 2-Dec-18 13:45pm.
|
|
|
|
|
@pkfox ... follow-up:Quote: a custom TabControl which loads custom TabPages Is the TabControl a sub-classed WinForm TabControl, or a 3rd. party commercial Control, or one you built "from scratch" ? Same question about the TabPage.
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
They're both sub classes of the Windows.Forms classes TabControl and TabPage
We can’t stop here, this is bat country - Hunter S Thompson RIP
|
|
|
|
|
I wanted to mimic .net core and use an appSettings.Json file, well it didn't work out. Guess .Net Core Extensions won't work in my project. So I wrote some code using Newtonsoft, could not read the file and parse out pure JSON. Well I was able to read the file but not able to deserialize it. Searched the internet and found a couple of solutions that didn't pan out as well. So I searching for a NuGet package and found one, but no documentation.
Anybody used or found a package that they like?
I just want to read and write my MongoDB connection string.
Side Note,
really liking Mongo for this project!
except for this part. But I got some automated code to load and program Mongo going now, just need to finish this up.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Newtonsoft works for me:
string strConnect = "my connection string";
string json = Newtonsoft.Json.JsonConvert.SerializeObject(strConnect);
...
string readback = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(json); It doesn't have to be a string, it can be any class instance! So a "Settings" class that includes your connection information maybe? Or a list of Settings objects?
List<Setting> readback = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Setting>>(json);
The world is the mollusc of your choice!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I've been working with Newtonsoft this morning and still can't deserialize. hmm...
I'll post my code
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I'm not sure this will help, but why not use the built-in JSON package in .NET-framework 4.0 or later?
For instance try this:
1) Create a new console application.
2) Add a reference to System.Web.Extensions.dll.
3) Paste the following code:
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var list = new List<Person>();
list.Add(new Person() { Name = "Dude", Age = 21 });
list.Add(new Person() { Name = "Chap", Age = 28 });
var json = new JavaScriptSerializer();
var result = json.Serialize(list);
var listFromJson = json.Deserialize(result, typeof(List<Person>)) as List<Person>;
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
} Just debug and step through the code to verify it works
I have serialized and deserialized a List<t> with a custom class.
You can of course serialize/deserialize whatever you want.
Best regards,
/Steffe
|
|
|
|
|
I saw a couple examples of that while searching, but passed on it because I had already written code.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
|
I took a look at the link and gave it a try, and was able to load the example, but not able to figure out how to use the object.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I'm completely missing something here. This should work! But var settings is always null.
Settings Wireless.Models.Settings
DbConnection = null
SmtpConnection = null
My json file: IN the project folder, marked as Content, Copy if newer
{
"Settings": {
"DbConnection": {
"Connection": "mongodb://admin:Password@localhost",
"Database": "database"
},
"SmtpConnection": {
"Server": "smtp.gmail.com",
"User": "emailAddress",
"Pass": "password",
"Port": "587",
"SSL": "True"
}
}
}
My Model, I double checked it to make sure it matches
public class Settings
{
public MongoDbConfig DbConnection { get; set; }
public SmtpConfig SmtpConnection { get; set; }
}
public class MongoDbConfig
{
public string Connection { get; set; }
public string Database { get; set; }
}
public class SmtpConfig
{
public string Server { get; set; }
public string User { get; set; }
public string Pass { get; set; }
public int Port { get; set; }
public bool SSL { get; set; }
}
My function to read the file, I verified the file in jsonText
public static void GetSettings()
{
var appPath = Path.Combine(Application.StartupPath, "appSettings.json");
var jsonText = File.ReadAllText(appPath);
var settings = JsonConvert.DeserializeObject<Settings>(jsonText);
Console.Write(appPath);
}
jsonText output
jsonText = "{\r\n \"Settings\": {\r\n \"DbConnection\": {\r\n \"Connection\": \"mongodb://admin:Password@localhost\",\r\n \"Database\": \"wirelessDb\"\r\n },\r\n \"SmtpConnection\": {\r\n \"Server\": \"smtp.gmail.com\",\r\n
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Searched for complex deserialization, and ran across this website
json2csharp - generate c# classes from json
and code to deserialize
New Model
public class DbConnection
{
public string Connection { get; set; }
public string Database { get; set; }
}
public class SmtpConnection
{
public string Server { get; set; }
public string User { get; set; }
public string Pass { get; set; }
public string Port { get; set; }
public string SSL { get; set; }
}
public class Settings
{
public DbConnection DbConnection { get; set; }
public SmtpConnection SmtpConnection { get; set; }
}
public class RootObject
{
public Settings Settings { get; set; }
}
Code to deserialize
JavaScriptSerializer oJS = new JavaScriptSerializer();
RootObject oRootObject = new RootObject();
oRootObject = oJS.Deserialize<RootObject>(jsonText);
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I want to get an array of all digits that make up a binary number.
So, since in binary we have 1, 2, 4, 8
then to get
6 you have 2 + 4
7 you have 1 + 2 + 4
9 you have 1 + 8
I need a way to populate an array with the numbers required to make the binary value.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Simple: create a mask, starting at one.
Binary AND the "target" with the mask. If the result is non-zero, add the mask to your output array
Either way, shift the mask one place left (same thing as multiplying by two, but a shift operator << is clearer in context)
Repeat.
If you copy the target into a temporary variable, you can use right shift and a while loop to decide if there are more bits to check.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I don't think I understand. Is this something simple enough that you can demonstrate here?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I'll see what I can do - I'm on a tablet, so code is difficult (as is spelling, typing quickly, ...)
Numbers aren't stored in base ten - you know that - they are stored in binary, with each bit in the number representing a power of 2:
... 24 23 22 21 20
... 16 8 4 2 1
So, if you start with a mask of 1 that checks just the bottom bit. Shift the mask left one place, and then it equals 2 and checks just the second bit. Shift it again - 4 - and it checks just the third bit. Again - 8 - and it's the fourth.
So if the mask checks just one bit and the mask is the binary value of that bit the AND will check the bits for the values you are looking for.
9 == 1001 binary
mask 0001 - 1 - yes, add one to your array
mask 0010 - 2 - no, don't add it.
mask 0100 - 4 - no, don't add it.
mask 1000 - 8 - yes, add eight to your array
No bits left, so you're done.
1 + 8 == 9
Try it on paper, and you'll see what I mean.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|