Menu
  • HOME
  • TAGS

EventHandler inside loop for several Windows Forms

Tag: c#,winforms,loops,eventhandler

My code is creating multiple instances of a form with different controls and events. This is done while incrementing i to the total amount of forms. The aim is to add an event to a button control in the form. My forms are instances of Ticker, the subclass of Form that I have created.

The issue I have is that when I click on the button at run-time, the event handler kicks in and i is out of bounds as the loop has already finished.

A snippet of the code is below, the event handler is integrated into a loop incrementing i which also houses the switches. Any way for thisObject to be equal to the current active ticker while still keeping the event handler in the loop? EDIT: I've provided the full class for better clarity. The section in question follows through the action_set>>email>>trigger>>click cases.

   public partial class DesktopTickers : Form
    {
        public DesktopTickers(List<string> tickerArgs)
        {
            InitializeComponent();
            this.Opacity = 0;
            this.ShowInTaskbar = false;
            int totalTickers = tickerArgs.Count();

            Rectangle screenBounds = GetDpiSafeResolution();
            Size iconSizeDefault = this.Size;                                                         
            string iconDirectory = @"\\server\Tickers\";
            string iconSuffix = "_icon.png";
            string configDirectory = @"\\server\Tickers\";
            string configSuffix = ".txt";
            char paramDelimiter = ':';
            char controlDelimiter = '=';
            char[] controlSeparatorL = {'('};
            char controlSeparatorR = ')';
            char actionDelimiter = '=';
            char[] actionSeparatorL = {'('};
            char actionSeparatorR = ')';
            char propertyDelimiter = '-';
            int maxWidthDefault = iconSizeDefault.Width;                                                                             
            int maxHeightDefault = iconSizeDefault.Height;                                                                             
            Ticker[] tickers = new Ticker[tickerArgs.Count()];
            List<Control> controls = new List<Control>();

            for (int i = 0; i < tickerArgs.Count(); i++)                                                       
            {
                string tickerArg = tickerArgs[i];                                                               
                string tickerConfigPath = configDirectory + tickerArg + configSuffix;                           
                string tickerResourcePath = iconDirectory + @"\" + tickerArg + @"\";
                string tickerIconPath = tickerResourcePath + tickerArg + iconSuffix;
                tickers[i] = new Ticker(screenBounds, tickerArg, i+1, tickerArgs.Count(), iconSizeDefault, 
                                        maxHeightDefault, maxWidthDefault, tickerIconPath);
                string[] tickerConfigContents = File.ReadAllLines(tickerConfigPath);
                for (int j = 0; j < tickerConfigContents.Length; j++)               
                {
                    string thisConfigLine = tickerConfigContents[j];                                                    
                    int configParamEnd = thisConfigLine.IndexOf(paramDelimiter);                                        
                    if (configParamEnd < 0) { configParamEnd = 0; }
                    string tickerConfigParam = thisConfigLine.Substring(0, configParamEnd);                           
                    string tickerConfigValue = thisConfigLine.Substring(configParamEnd + 1);
                    switch (tickerConfigParam.ToLower())                                    
                    {   //TICKER LEVEL PARAMETERS
                        case "icon_width":
                            tickers[i].iconWidth = Convert.ToInt32(tickerConfigValue);
                            break;
                        case "icon_height":
                            tickers[i].iconHeight = Convert.ToInt32(tickerConfigValue);
                            break;
                        case "max_width":                                                    
                            tickers[i].maxWidth = Convert.ToInt32(tickerConfigValue);        
                            break;
                        case "max_height":                                                   
                            tickers[i].maxHeight = Convert.ToInt32(tickerConfigValue);  
                            break;
                        case "control_set":   
                            for (int k = j + 1; k < tickerConfigContents.Length; k++)        
                            {   //CONTROL LEVEL PARAMETERS
                                string thisControlLine = tickerConfigContents[k]; 
                                if(thisControlLine == "end") { break; }
                                int controlParamEnd = thisControlLine.IndexOf(controlDelimiter); 
                                string thisControlType = thisControlLine.Substring(0, controlParamEnd);
                                string thisControlDetails = thisControlLine.Substring(controlParamEnd+ 1);
                                thisControlDetails = thisControlDetails.Replace(controlSeparatorR.ToString(), "");
                                string[] controlProperties = thisControlDetails.Split(controlSeparatorL, StringSplitOptions.RemoveEmptyEntries); 
                                switch (thisControlType.ToLower()) 
                                {   //CONTROL TYPE LEVEL PARAMETERS
                                    case "image":                  
                                        PictureBox thisImage = new PictureBox(); 
                                        for (int l = 0; l < controlProperties.Length; l++)
                                        {
                                            string thisProperty = controlProperties[l];
                                            int propertyParamEnd = thisProperty.IndexOf(propertyDelimiter);
                                            string propertyType = thisProperty.Substring(0, propertyParamEnd - 1);
                                            string propertyValue = thisProperty.Substring(propertyParamEnd + 2);
                                            switch (propertyType.ToLower())
                                            {   //PROPERTY LEVEL PARAMETERS
                                                case "file":
                                                    try { thisImage.BackgroundImage = Image.FromFile(tickerResourcePath + propertyValue); }
                                                    catch { thisImage.BackgroundImage = thisImage.ErrorImage; }
                                                    break;
                                                case "bounds":
                                                    char[] pointDelimiter = { ',' };
                                                    string[] boundsValues = propertyValue.Split(pointDelimiter);
                                                    Rectangle bounds = new Rectangle(
                                                                            new Point(Convert.ToInt32(boundsValues[0]), Convert.ToInt32(boundsValues[1])),
                                                                            new Size(Convert.ToInt32(boundsValues[2]), Convert.ToInt32(boundsValues[3])));
                                                    thisImage.Bounds = bounds;
                                                    break;
                                                case "layout":
                                                    switch(propertyValue.ToLower())
                                                    {
                                                        case "stretch":
                                                            thisImage.BackgroundImageLayout = ImageLayout.Stretch;
                                                            break;
                                                        case "zoom":
                                                            thisImage.BackgroundImageLayout = ImageLayout.Zoom;
                                                            break;
                                                        case "tile":
                                                            thisImage.BackgroundImageLayout = ImageLayout.Tile;
                                                            break;
                                                        case "center":
                                                            thisImage.BackgroundImageLayout = ImageLayout.Center;
                                                            break;
                                                        default:
                                                            thisImage.BackgroundImageLayout = ImageLayout.None;
                                                            break;
                                                    }
                                                    break;
                                                case "id":
                                                    thisImage.Name = propertyValue;
                                                    break;
                                            }
                                            tickers[i].Controls.Add(thisImage);
                                            thisImage.Show();
                                        }
                                        break;
                                    case "label":
                                        Label thisLabel = new Label();
                                        for (int l = 0; l < controlProperties.Length; l++)
                                        {
                                            string thisProperty = controlProperties[l];
                                            int propertyParamEnd = thisProperty.IndexOf(propertyDelimiter);
                                            string propertyType = thisProperty.Substring(0, propertyParamEnd - 1);
                                            string propertyValue = thisProperty.Substring(propertyParamEnd + 2);
                                            thisLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                                            switch (propertyType.ToLower())
                                            {   //PROPERTY LEVEL PARAMETERS
                                                case "text":
                                                    thisLabel.Text = propertyValue;
                                                    break;
                                                case "font":
                                                    char fontDelimiter = ',';
                                                    int fontSplitIndex = propertyValue.IndexOf(fontDelimiter);
                                                    string fontName = propertyValue.Substring(0, fontSplitIndex);
                                                    string fontSize = propertyValue.Substring(fontSplitIndex + 1);
                                                    int fontSizeNum = int.Parse(fontSize);
                                                    thisLabel.Font = new Font(propertyValue, fontSizeNum);
                                                    break;
                                                case "bounds":
                                                    char[] pointDelimiter = {','};
                                                    string[] boundsValues = propertyValue.Split(pointDelimiter);
                                                    Rectangle bounds = new Rectangle(
                                                                            new Point(Convert.ToInt32(boundsValues[0]), Convert.ToInt32(boundsValues[1])),
                                                                            new Size(Convert.ToInt32(boundsValues[2]), Convert.ToInt32(boundsValues[3])));
                                                    thisLabel.Bounds = bounds;
                                                    break;
                                                case "id":
                                                    thisLabel.Name = propertyValue;
                                                    break;
                                            }
                                            thisLabel.Show();
                                            tickers[i].Controls.Add(thisLabel);
                                        }
                                        break;
                                    case "button":
                                        Button thisButton = new Button();
                                        for (int l = 0; l < controlProperties.Length; l++)
                                        {
                                            string thisProperty = controlProperties[l];
                                            int propertyParamEnd = thisProperty.IndexOf(propertyDelimiter);
                                            string propertyType = thisProperty.Substring(0, propertyParamEnd - 1);
                                            string propertyValue = thisProperty.Substring(propertyParamEnd + 2);
                                            switch (propertyType.ToLower())
                                            {
                                                case "text":
                                                    thisButton.Text = propertyValue;
                                                    break;
                                                case "font":
                                                    char fontDelimiter = ',';
                                                    int fontSplitIndex = propertyValue.IndexOf(fontDelimiter);
                                                    string fontName = propertyValue.Substring(0, fontSplitIndex);
                                                    string fontSize = propertyValue.Substring(fontSplitIndex + 1);
                                                    int fontSizeNum = int.Parse(fontSize);
                                                    thisButton.Font = new Font(propertyValue, fontSizeNum);
                                                    break;
                                                case "bounds":
                                                    char[] pointDelimiter = { ',' };
                                                    string[] boundsValues = propertyValue.Split(pointDelimiter);
                                                    Rectangle bounds = new Rectangle(
                                                                            new Point(Convert.ToInt32(boundsValues[0]), Convert.ToInt32(boundsValues[1])),
                                                                            new Size(Convert.ToInt32(boundsValues[2]), Convert.ToInt32(boundsValues[3])));
                                                    thisButton.Bounds = bounds;
                                                    break;
                                                case "id":
                                                    thisButton.Name = propertyValue;
                                                    break;
                                            }
                                            thisButton.Show();
                                            tickers[i].Controls.Add(thisButton);
                                        }
                                        break;
                                    case "textbox":
                                        TextBox thisTextBox = new TextBox();
                                        for (int l = 0; l < controlProperties.Length; l++)
                                        {
                                            string thisProperty = controlProperties[l];
                                            int propertyParamEnd = thisProperty.IndexOf(propertyDelimiter);
                                            string propertyType = thisProperty.Substring(0, propertyParamEnd - 1);
                                            string propertyValue = thisProperty.Substring(propertyParamEnd + 2);
                                            thisTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                                            switch (propertyType.ToLower())
                                            {   //PROPERTY LEVEL PARAMETERS
                                                case "text":
                                                    thisTextBox.Text = propertyValue;
                                                    break;
                                                case "font":
                                                    char fontDelimiter = ',';
                                                    int fontSplitIndex = propertyValue.IndexOf(fontDelimiter);
                                                    string fontName = propertyValue.Substring(0, fontSplitIndex);
                                                    string fontSize = propertyValue.Substring(fontSplitIndex + 1);
                                                    int fontSizeNum = int.Parse(fontSize);
                                                    thisTextBox.Font = new Font(propertyValue, fontSizeNum);
                                                    break;
                                                case "bounds":
                                                    char[] pointDelimiter = { ',' };
                                                    string[] boundsValues = propertyValue.Split(pointDelimiter);
                                                    Rectangle bounds = new Rectangle(
                                                                            new Point(Convert.ToInt32(boundsValues[0]), Convert.ToInt32(boundsValues[1])),
                                                                            new Size(Convert.ToInt32(boundsValues[2]), Convert.ToInt32(boundsValues[3])));
                                                    thisTextBox.Bounds = bounds;
                                                    break;
                                                case "id":
                                                    thisTextBox.Name = propertyValue;
                                                    break;
                                            }
                                            thisTextBox.Show();
                                            tickers[i].Controls.Add(thisTextBox);
                                        }
                                        break;
                                }
                            }
                            break;
                        case "action_set":
                            for (int k = j + 1; k < tickerConfigContents.Length; k++)                                        
                            {   //ACTION LEVEL PARAMETERS
                                string thisActionLine = tickerConfigContents[k];                                             
                                if (thisActionLine == "end") { break; }
                                int actionParamEnd = thisActionLine.IndexOf(actionDelimiter);       
                                string thisActionType = thisActionLine.Substring(0, actionParamEnd);
                                string thisActionDetails = thisActionLine.Substring(actionParamEnd + 1);
                                thisActionDetails = thisActionDetails.Replace(actionSeparatorR.ToString(), ""); 
                                string[] actionProperties = thisActionDetails.Split(actionSeparatorL, StringSplitOptions.RemoveEmptyEntries);
                                Control thisObject = new Control();
                                switch (thisActionType.ToLower())                                                          
                                {   //ACTION TYPE LEVEL PARAMETERS
                                    case "email":
                                        //email requires trigger, objectid, action to send email, email action params
                                        for (int l = 0; l < actionProperties.Length; l++)
                                        {
                                            string thisProperty = actionProperties[l];
                                            int propertyParamEnd = thisProperty.IndexOf(propertyDelimiter);
                                            string propertyType = thisProperty.Substring(0, propertyParamEnd - 1);
                                            string propertyValue = thisProperty.Substring(propertyParamEnd + 2);
                                            string emailDomain = "";
                                            string emailServer = "";
                                            int emailPort = 0;
                                            string emailTemplate = "";
                                            string emailRecipient = "";
                                            switch (propertyType.ToLower())
                                            {
                                                case "domain":
                                                    emailDomain = propertyValue;
                                                    break;
                                                case "server":
                                                    emailServer = propertyValue;
                                                    break;
                                                case "port":
                                                    emailPort = Convert.ToInt32(propertyValue);
                                                    break;
                                                case "file":
                                                    emailTemplate = tickerResourcePath + propertyValue;
                                                    break;
                                                case "recipient":
                                                    emailRecipient = propertyValue;
                                                    break;
                                                case "object":
                                                    thisObject = tickers[i].Controls.Find(propertyValue, false).FirstOrDefault() as Control;
                                                    //thisObject = objects[0];
                                                    break;
                                                case "trigger":
                                                    tickers[i].SetEmailProperties(emailDomain, emailServer, emailPort, emailTemplate, emailRecipient);
                                                    switch(propertyValue.ToLower())
                                                    {
                                                        case "click":
                                                            thisObject.MouseDown += new MouseEventHandler((sender, e)
                                                                => tickers[i].SendEmail_Event(sender, e));
                                                            break;
                                                    }
                                                    break;
                                            }
                                        }
                                        break;
                                }
                            }
                            break;
                    }
                }
                tickers[i].Show();
            }
        }


        private Rectangle GetDpiSafeResolution()
        {
            using (Graphics graphics = this.CreateGraphics())
            {
                return new Rectangle(new Point(0, 0), new Size((Screen.PrimaryScreen.Bounds.Width * (int)graphics.DpiX) / 96
                , (Screen.PrimaryScreen.Bounds.Height * (int)graphics.DpiY) / 96));
            }
        }

    }

Best How To :

This seems to be a capture issue. You need to capture the value of i when the form is created and keep this value when you create the event handler. This can be done simply by creating a new variable iValue or tickerInd while creating the form and using this variable rather than i in the event handler code.

I am writing this answer a little bit speculatively. Please provide a larger code snippet which will allow us to see the creation code for the form and the handler. But I believe that your current code is as follows. The part that I marked as "code based on i" is the code snippet that you have currently provided.

for (var i = 0; i < formCount; ++i)
{
    var form = new Ticker();
    form.Button.OnClicked += () =>
        {
            //code based on i
            doSomething(i);
        };
}

It needs to be as follows:

for (var i = 0; i < formCount; ++i)
{
    var formInd = i;
    var form = new Ticker();
    form.Button.OnClicked += () =>
        {
            //code based on i
            doSomething(formInd);
        };
}

Edit: You need to change the following code:

case "click":
    //replace i with tickerInd, this will capture the current value of i
    var tickerInd = i;
    thisObject.MouseDown += new MouseEventHandler((sender, e)
      => tickers[tickerInd].SendEmail_Event(sender, e));
break;

Note: Your code seems to need some refactoring. First of all, you don't really need Ticker[] tickers, you can simply do the following:

public DesktopTickers(List<string> tickerArgs)
{
    for (var i = 0; i < tickerArgs.Count; ++i)
    {
        var tickerArg = tickerArgs[i];
        var ticker = new Ticker(...);
        //your whole initialization code goes here
        //replace all "tickers[i]" with "ticker"
        ticker.Show();
    }
}

Afterwards, you can do further refactoring by moving this initialization into Ticker constructor and then further divide it into initializtion methods.

Unconstrained type parameters casting

c#,.net,types,casting

The compiler sees the T2 and T identifiers and helpfully informs you that those types seem unrelated. That's absolutely correct, as they have no relation: there are no generic constraints that would assert any relations between them (I'm not saying that would be useful here though :) ). Whether this...

Memory consumption when chaining string methods

c#,string,immutability,method-chaining

Is it true that when you chain string functions, every function instantiates a new string? In general, yes. Every function that returns a modified string does so by creating a new string object that contains the full new string which is stored separately from the original string. There are...

check if file is image

c#,asp.net,asp.net-mvc

You can't do this: string.Contains(string array) Instead you have to rewrite that line of code to this: if (file == null || formats.Any(f => file.Contains(f))) And this can be shortened down to: if (file == null || formats.Any(file.Contains)) ...

Export data from table in Pervasive

c#,pervasive

No, there is no way to export data programmatically through the "Export Data" wizard in the Pervasive Control Center. You would need to either write or have written an export module.

C# Code design / Seperate classes for each TabControl

c#,oop,architecture,software-design,code-design

Place a UserControl on each tab.

Regex that allow void fractional part of number

c#,regex

Just get the dot outside of the captruing group and then make it as optional. @"[+-]?\d+\.?\d*" Use anchors if necessary. @"^[+-]?\d+\.?\d*$" ...

C# PCL HMACSHAX with BouncyCastle-PCL

c#,bouncycastle,portable-class-library

Try like this for HmacSha256 public class HmacSha256 { private readonly HMac _hmac; public HmacSha256(byte[] key) { _hmac = new HMac(new Sha256Digest()); _hmac.Init(new KeyParameter(key)); } public byte[] ComputeHash(byte[] value) { if (value == null) throw new ArgumentNullException("value"); byte[] resBuf = new byte[_hmac.GetMacSize()]; _hmac.BlockUpdate(value, 0, value.Length); _hmac.DoFinal(resBuf, 0); return resBuf; }...

Foreign key in C#

c#,sql,sql-server,database

You want create relationship in two table Refer this link http://www.c-sharpcorner.com/Blogs/5608/create-a-relationship-between-two-dataset-tables.aspx...

How do I provide a collection of elements to a custom attached property?

c#,wpf,binding

I managed to get it working using an IMultiValueConverter like this: public class BorderCollectionConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var borderCollection = new BorderCollection(); borderCollection.AddRange(values.OfType<Border>()); return borderCollection; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new...

Aligning StackPanel to top-center in Canvas

c#,wpf,xaml,canvas

If you don't want any input or hit testing on a certain element you should set the IsHitTestVisible property to false: <Grid> <Canvas Name="Canvas" Background="#EFECCA"> <DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="{Binding ActualWidth, ElementName=Canvas}" Height="{Binding ActualHeight, ElementName=Canvas}" MouseLeftButtonDown="DockPanel_MouseLeftButtonDown" TouchDown="DockPanel_TouchDown" Panel.ZIndex="2" Background="Transparent"> </DockPanel> <Button Width="50" Height="50"...

SQL Server / C# : Filter for System.Date - results only entries at 00:00:00

c#,asp.net,sql-server,date,gridview-sorting

What happens if you change all of the filters to use 'LIKE': if (DropDownList1.SelectedValue.ToString().Equals("Start")) { FilterExpression = string.Format("Start LIKE '{0}%'", TextBox1.Text); } Then, you're not matching against an exact date (at midnight), but matching any date-times which start with that date. Update Or perhaps you could try this... if (DropDownList1.SelectedValue.ToString().Equals("Start"))...

Multiple Threads searching on same folder at same time

c#,multithreading,file-search

Instead of using ordinary foreach statement in doing your search, you should use parallel linq. Parallel linq combines the simplicity and readability of LINQ syntax with the power of parallel programming. Just like code that targets the Task Parallel Library. This will shield you from low level thread manipulation and...

How do I run C# within a Node.js server application?

c#,node.js,server

I have idea for your problem . U can write c# console app and then call it from nodejs . U can look this url Execute an exe file using node.js . after c# job writes all data to database, u can look to the this table read from it...

C# MySQL Parameters.AddWithValue

c#,mysql

You try to add all your 52 parameter and their values with one AddWithValue method. You can't do that. First of all, you need to define all your parameters in your command with your column names like; command.CommandText = "INSERT INTO tb_mitarbeiter (Vorname, id, projectnummber....) VALUES (?name, ?id, ?projektnummer....)"; Then...

Is it possible to concactenate a DataBound value with a constant string in XAML DataBinding?

c#,xaml,windows-phone

You can use a StringFormat in your binding, like so: <TextBox Text="{Binding ItemName, StringFormat={}Item: {0}}"/> That being said, it may cause some unexpected behavior when editing. For example, if the user edits only the item name (excluding the 'Item:' text), then when the TextBox loses focus, the string format will...

C# - Can't connect to remote MySQL server

c#,mysql

When connecting to a MySQL-Database I always used the MySQL Connector you can get here: https://dev.mysql.com/downloads/connector/net/6.9.html You have to import the MySQL namespaces to your project and then you can use the MySQLConnection instead of the SQLConnection that is, as far as I know, only for MSSQL servers. http://www.codeproject.com/Tips/423233/How-to-Connect-to-MySQL-Using-Csharp...

while Inherit style in WPF it affect parent style

c#,xaml,styles,wpf-controls

If you declare a Style without an x:Key, it will override the default style for that control. <Style TargetType="local:CustomControl"> So the code above will effect all CustomControl elements throughout the entire application (or within the scope). If you do not want to override the base style, you can give your...

Index was out of range. Must be non-negative or less than size of collection [duplicate]

c#

It looks like you have a typo in your loop condition: for (int index = filePaths.Count(); filePaths.Count() > 9; index--) It should be for (int index = filePaths.Count() - 1; index > 9; index--) Also note that for the first iteration of loop you're trying to access filePaths[filePaths.Count()] which is...

Update list of items in c#

c#,linq,list,updates

I would do something like this: (for ordinairy lists) // the current list var currentList = new List<Employee>(); currentList.Add(new Employee { Id = 154, Name = "George", Salary = 10000 }); currentList.Add(new Employee { Id = 233, Name = "Alice", Salary = 10000 }); // new list var newList =...

How can I determine if an object of anonymous type is empty?

c#,.net

Anonymous types do not provide operator overloads for ==, although it wouldn't matter in this case since one of the arguments is typed object. However the C# compiler does provide Equals, GetHashCode, and ToString implementations. Use the static object.Equals, method which will do the appropriate null checks and then call...

SettingsProvider class - should it be in DAL or BLL project?

c#,data-access-layer,bll

I don't agree that there is a right layer for you to put that class since you reading values from the config file based on keys provided and it can be needed by one or all of the layers. In the case of all layers using this class, you can...

Access manager information from Active Directory

c#,asp.net,active-directory

try this: var loginName = @"loginNameOfInterestedUser"; var ldap = new DirectoryEntry("LDAP://domain.something.com"); var search = new DirectorySearcher(ldap) { Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" + loginName + "))" }; var result = search.FindOne(); if (result == null) return; var fullQuery = result.Path; var user = new DirectoryEntry(fullQuery); DirectoryEntry manager; if (user.Properties.PropertyNames.OfType<string>().Contains("manager")) { var managerPath...

Catch concurrency exception in EF6 to change message to be more user friendly

c#,asp.net,.net,entity-framework,entity-framework-6

You are executing an asynchronous method. This means that any exceptions will be thrown when you call await on the returned task or when you try to retrieve the results using await myTask; You never do so, which means that the exception is thrown and caught higher up your call...

deployment of a site asp.net and iis

c#,asp.net,iis

There are several domain providers like: godaddy, name etc you can use to buy a domain name. These providers also provide you steps to map the domain name to your website. Check out this link for example. This link explains domain name configuration in details.

Regex to remove `.` from a sub-string enclosed in square brackets

c#,.net,regex,string,replace

To remove all the dots present inside the square brackets. Regex.Replace(str, @"\.(?=[^\[\]]*\])", ""); DEMO To remove dot or ?. Regex.Replace(str, @"[.?](?=[^\[\]]*\])", ""); ...

Register return Type

c#,generics,return-type

If you know the type at startup, you could just derive the class: public class UserLogin : GenericLogin<ABC01_REGISTERED_USER> { } Then use that class all along. Else, you have to supply the type name every time, since else it can't know that you want to use that type every time....

Show/hide tinymce with radio buttons

c#,asp.net,asp.net-mvc,tinymce

Your missing an @symbol for the id attribute: Modify your script as well like this: ***EDIT some thing seems off about the radio buttons only one should be checked and they should have the same name ** you can use the # to denote and ID in Jquery by the...

Validate a field only if it is populated

c#,wpf,idataerrorinfo

You can implement your OptionalPhoneAttribute based on the original PhoneAttribute: public sealed class OptionalPhoneAttribute : ValidationAttribute { public override bool IsValid(object value) { var phone = new PhoneAttribute(); //return true when the value is null or empty //return original IsValid value only when value is not null or empty return...

Collect strings after a foreach loop

c#,xml,foreach

Yep, you need to do the adding within the loop. I'd use a List<string> as it supports LINQ: XmlNodeList skillNameNodeList=SkillXML.GetElementsByTagName("name"); List<string> skills = new List<string>(); foreach (XmlNode skillNameNode in skillNameNodeList) { skills.Add(skillNameNode.Attributes["value"].Value); } ...

Call Sync method call from Async Callback?

c#,asynchronous,synchronous

Callbacks are independent as they're invoked on the thread-pools IO completion workers. If you're interested, you can see that in the source code. This particular method is for the Socket class (which TcpClient and UdpClient use internally), where overlapped IO is used to invoke the callback (see the comment on...

Marshal struct in struct from c# to c++

c#,c++,marshalling

Change this: [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 36)] private string iu; to this: [MarshalAs(UnmanagedType.LPStr)] private string iu; Note that this code is good only to pass a string in the C#->C++ direction. For the opposite direction (C++->C#) it is more complex, because C# can't easily deallocate C++ allocated memory. Other important thing:...

Convert contents of an XmlNodeList to a new XmlDocument without looping

c#,xml,xpath,xmldocument,xmlnodelist

If you're happy to convert it into LINQ to XML, it's really simple: XDocument original = ...; // However you load the original document // Separated out for clarity - could be inlined, of course string xpath = "//Person[not(PersonID = following::Person/PersonID)]" XDocument people = new XDocument( new XElement("Persons", original.XPathSelectElements(xpath) )...

C# XML: System.InvalidOperationException

c#,xml

Is "User Info" and "Course Data" is a different entity. If it is so, I think you may encapsulate them in one entity. XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.UTF8); writer.WriteStartDocument(true); writer.Formatting = Formatting.Indented; writer.Indentation = 4; writer.WriteStartElement("My Entity"); /* It is a biggest one*/ writer.WriteStartElement("User Info"); writer.WriteStartElement("Name"); writer.WriteString(userName); writer.WriteEndElement(); writer.WriteStartElement("Tutor...

Convert Date Time to IST

c#

You need to use "India Standard Time" instead of "Indian". Please refer to this link for a list of the time zone descriptions.

how can I add a column to IQueryable object and modify its values

c#,.net,linq,grid,devexpress

Simple example for using a non-anonymous class. public class MyLovelyClass { public Int32 Number { get; set; } public bool Selection { get; set; } } var packs = from r in new XPQuery<Roll>(session) select new MyLovelyClass() { Number = r.number }; gcPack.DataSource = packs; ...

Visual Studio Assembly force-installs Target Framework

c#,.net,visual-studio-2013,.net-framework-version

The targeted .NET version is the only version that the app will depend upon by default. Visual Studio will not automatically add higher and backwards compatible releases. Do this manually by adding other .NET versions to a configuration file: On the Visual Studio menu bar: Choose Project; Add New Item;...

Difference between application and module pipelines in Nancy?

c#,asp.net,nancy

The module- and application pipelines are explained in detail in the wiki. It's basically hooks which are executed before and after route execution on a global (application pipelines) and per-module basis. Here's an example: If a route is resolved to a module called FooModule, the pipelines will be invoked as...

DialogBox with value verifications

c#,.net,winforms

The problem is you're trying to enable or disable the button when checking individual textboxes and they're conflicting with each other, instead the logic needs to be at a higher level. Change your textbox validation function to return a bool, and use that in ValidateAll to determine whether or not...

How to declare var datatype in public scope in c#?

c#,linq

Declare it as a known type (not an anonymous type), like this for example: Dictionary<int, string> results = new Dictionary<int, string>(); Then you could store the results in the Dictionary: results = behzad.GAPERTitles.ToDictionary(x => x.id, x => x.gaptitle); And reference it later: private void button1_Click(object sender, EventArgs e) { //...

MvcSiteMapProvider - Enhanced bootstrap dropdown menu

c#,twitter-bootstrap,asp.net-mvc-5,mvcsitemapprovider,mvcsitemap

node.Descendants should be node.Children Learn the difference on Descendants and Children here, CSS Child vs Descendant selectors (Never mind the post beeing about CSS, this is a generic pattern)...

How to return result while applying Command query separation (CQS)

c#,design-patterns,cqrs,command-query-separation

In such scenario I usually go with generating new entity Ids on the client. Like this: public class ProductController: Controller{ private IProductCommandService commandService; private IProductQueryService queryService; private IIdGenerationService idGenerator; [HttpPost] public ActionResult Create(Product product){ var newProductId = idGenerator.NewId(); product.Id = newProductId; commandService.AddProduct(product); //TODO: add url parameter or TempData key to...

Why is the task is not cancelled when I call CancellationTokenSource's Cancel method in async method?

c#,asynchronous,task,cancellationtokensource,cancellation-token

Cancellation in .Net is cooperative. That means that the one holding the CancellationTokenSource signals cancellation and the one holding the CancellationToken needs to check whether cancellation was signaled (either by polling the CancellationToken or by registering a delegate to run when it is signaled). In your Task.Run you use the...

Error when building an XDocument

c#,xml,linq,xpath,linq-to-xml

You can ignore pretty much all your code, the issue is just this: XDocument people = new XDocument("Persons"); You can't create an XDocument containing a string, you need to add an element: XDocument people = new XDocument( new XElement("Persons", original.XPathSelectElements(xpathFilterDups))); ...

Load XML to list using LINQ [duplicate]

c#,xml,linq

Make a base class which will have id,x,y,z, and have Vendors,Bankers and Hospitals extend it. Then you can have a collection of the base class, and add to it the classes that inherit from it....

Unable to find the auto created Database

c#,asp.net,asp.net-mvc,entity-framework

If you don't specify a database name then the connection will use the default database for the user, in this case it's integrated security so it's your Windows login. As you likely have full system admin on the server the default database will be master so you will find all...

How to Customize Visual Studio Setup

c#,visual-studio,setup-project

You can use a Microsoft Setup project or WIX (easily integrate with Visual Studio). Both are free. •You can do almost all of your customization in setup project by adding custom actions. •WIX (window installer xml) is the better option. You can do a complete customization from wix but it...

System.net.http.formatting causing issues with Newtonsoft.json

c#,asp.net,asp.net-mvc,json.net

Does the assemblyBinding tag have proper xmlns schema? Check if the issue you are encountering is same as Assembly binding redirect does not work

Get object by attribute value [duplicate]

c#,reflection,custom-attributes,spring.net

If you have obtained the Assembly, you can just iterate over the types and check for your conditions: var matchingTypes = from t in asm.GetTypes() where !t.IsInterface && !t.IsAbstract where typeof(ICustomInterface).IsAssignableFrom(t) let foo = t.GetCustomAttribute<FooAttribute>() where foo != null && foo.Bar == Y select t; I am assuming you want...

How to send Ctrl+S through SendKeys.Send() method to save a file(save as dialog)

c#,.net,windows,sendkeys

I believe you need to use: SendKeys.SendWait("^(s)"); Instead of: SendKeys.SendWait("^%s?"); Have a look at https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx for more information....