Word.ParagraphFormat properties working incorrectly - c#

I've got a bit of an issue where the Word.ParagraphFormat properties are not working. Here is my code:
private void ThisDocument_Startup(object sender, EventArgs e)
{
Word.ParagraphFormat format = new Word.ParagraphFormat();
format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
// text is as if it were center alignment; behaves like ".wdAlighnParagraphCenter"
format.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText;
// text boundaries around every section after line return rather than the entire page text and header/footer; behaves like ".wdOutlineLevelX" (X for 1-9)
Word.Style stl = Styles[Word.WdBuiltinStyle.wdStyleNormalTable] as Word.Style;
stl.UnhideWhenUsed = true;
stl.ParagraphFormat = paragraphFormat;
stl.NoSpaceBetweenParagraphsOfSameStyle = true;
}
Is there something I need to add or take away? Is there something else that needs to be set or assigned? Or, is there something over writing it later?

The Office object models work differently than the .NET object models - the technology is over twenty years old and the way people thought was different. In the Office object models you need to first create the object. Only then can you define its properties. (As opposed to that, in the .NET world you can define properties, then create the object and assign the properties.)
So for defining a Style you go about it more like this:
private void ThisDocument_Startup(object sender, EventArgs e)
{
Word.Style Stl = Styles[Word.WdBuiltinStyle.wdStyleHeading1] as Word.Style;
Stl.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
Stl.ParagraphFormat.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText;
Stl.UnhideWhenUsed = true;
Stl.NoSpaceBetweenParagraphsOfSameStyle = true;
}
Also please note that you can't set the above properties for a Table style, as in your question's sample code. These settings are only appropriate for paragraph (or linked - although I strongly advise against using linked) styles.

Related

XamlBindingHelper Class

Can somebody provide an overview for use of the XamlBindingHelper class with examples? Specifically the GetDataTemplateComponent and SetDataTemplateComponent method.
In the official document, it says
This class is for use in code that is generated by the XAML compiler.
This tells me that I should be able to find some reference of it in code-generated classes (.g.cs) by x:Bind, given there's not a single thread on the Internet that explains what exactly it does.
So I created a test UWP project with a ListView, and inside its ItemTemplate I threw in some x:Bind with x:Phase. After I compiled the project, I found some of its methods used inside my MainPage.g.cs -
XamlBindingHelper.ConvertValue
public static void Set_Windows_UI_Xaml_Controls_ItemsControl_ItemsSource(global::Windows.UI.Xaml.Controls.ItemsControl obj, global::System.Object value, string targetNullValue)
{
if (value == null && targetNullValue != null)
{
value = (global::System.Object) global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof(global::System.Object), targetNullValue);
}
obj.ItemsSource = value;
}
Apparently the XamlBindingHelper.ConvertValue method is for converting values. I knew this already, as I used it in one of my recent answers on SO.
XamlBindingHelper.SuspendRendering & XamlBindingHelper.ResumeRendering
public int ProcessBindings(global::Windows.UI.Xaml.Controls.ContainerContentChangingEventArgs args)
{
int nextPhase = -1;
switch(args.Phase)
{
case 0:
nextPhase = 1;
this.SetDataRoot(args.Item);
if (!removedDataContextHandler)
{
removedDataContextHandler = true;
((global::Windows.UI.Xaml.Controls.StackPanel)args.ItemContainer.ContentTemplateRoot).DataContextChanged -= this.DataContextChangedHandler;
}
this.initialized = true;
break;
case 1:
global::Windows.UI.Xaml.Markup.XamlBindingHelper.ResumeRendering(this.obj4);
nextPhase = -1;
break;
}
this.Update_((global::System.String) args.Item, 1 << (int)args.Phase);
return nextPhase;
}
public void ResetTemplate()
{
this.bindingsTracking.ReleaseAllListeners();
global::Windows.UI.Xaml.Markup.XamlBindingHelper.SuspendRendering(this.obj4);
}
XamlBindingHelper.SuspendRendering & XamlBindingHelper.ResumeRendering look very interesting. They seem to be the key functions to enable ListView/GridView's incremental item rendering which helps improve the overall panning/scrolling experience.
So apart from x:DeferLoadingStrategy and x:Load(Creators Update), they are something else that could be used to improve your app performance.
IDataTemplateComponent & IDataTemplateExtension
However, I couldn't find anything related to GetDataTemplateComponent and SetDataTemplateComponent. I even tried to manually set this attached property in XAML but the get method always returned null.
And here's the interesting bit. I later found this piece of code in the generated class.
case 2: // MainPage.xaml line 13
{
global::Windows.UI.Xaml.Controls.Grid element2 = (global::Windows.UI.Xaml.Controls.Grid)target;
MainPage_obj2_Bindings bindings = new MainPage_obj2_Bindings();
returnValue = bindings;
bindings.SetDataRoot(element2.DataContext);
element2.DataContextChanged += bindings.DataContextChangedHandler;
global::Windows.UI.Xaml.DataTemplate.SetExtensionInstance(element2, bindings);
}
break;
The method DataTemplate.SetExtensionInstance looks very similar to XamlBindingHelper.SetDataTemplateComponent. It takes element2 which is the root Grid inside the ItemTemplate of my ListView, and an IDataTemplateExtension; where the latter takes an element and an IDataTemplateComponent. If you have a look at their definitions, their functionalities are very similar, which makes me think if DataTemplate.SetExtensionInstance is the replacement of XamlBindingHelper.SetDataTemplateComponent? I'd love to know if otherwise.
Unlike IDataTemplateComponent, you can get an instance of the IDataTemplateExtension in your code -
var firstItemContainer = (ListViewItem)MyListView.ContainerFromIndex(0);
var rootGrid = (Grid)firstItemContainer?.ContentTemplateRoot;
var dataTemplateEx = DataTemplate.GetExtensionInstance(rootGrid);
In my case, the dataTemplateEx is an instance of another generated class called MainPage_obj2_Bindings, where you have access to methods like ResetTemplate and ProcessBindings.
I assume they could be helpful if you were to build your own custom list controls, but other than that I just can't see why you would ever need them.

Generic list of Objects, passed through a HTML text writer class to output html. Now trying to implement paging

I have a list of objects which have been taken form a stored procedure. I then take this list pass it through a method I created which wraps it in HTML and then out puts to the webform.
I'm looking to create paging for this list. I have stored the list in a session and have two buttons ( next and previous ) I'm using LINQ to skip and take form the list on each button click.
Button Click
protected void lnkNext_Click(object sender, EventArgs e)
{
ListOfAdvertAndUsers = (List<Advert_User>)Session["list"];
var list = from item in ListOfAdvertAndUsers select item;
var pgNo = 1;
var pgRec = 6;
list = list.Skip(pgRec * pgNo).Take(pgRec).ToList();
ListOfAdvertAndUsers = list.ToList();
PopulateData(ListOfAdvertAndUsers);
}
I don't have enough rep to make a comment so I will just post my comments as an answer.
I presume that you are using Asp.Net WebForms, so how are you outputting the final html, Response.Write or setting Text on a Literal?
But if you are using WebForms, would it not be easier to use some of the builtin Controls. I don´t exactly know how your data looks like, but maybe a GridView would be a good solution as it has builtin support for paging. Here is an example from MS on how it can be used: http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/sorting-paging-and-filtering-data
Not a direct answer, but you're overwriting queries and lists, which seem to convolute the code unnecessarily. Your method could be simplified to:
protected void lnkNext_Click(object sender, EventArgs e)
{
ListOfAdvertAndUsers = (List<Advert_User>)Session["list"];
var pgNo = 1; // don't you need to increment the page number somewhere?
var pgRec = 6; // should this be defined somewhere other than this method?
var page = ListOfAdvertAndUsers.Skip(pgRec * pgNo).Take(pgRec);
PopulateData(page);
}
Perhaps that will help you determine what the real problem is...

form data and class objects

EDIT::
This might be simple for some but right now it has me confused. I am working on a project and when the submit button is pressed,create either a person or an employee based on the above checkbox. Put all of the form data into the class object using either the constructor or properties. Display all of the class information using the ToString method and a messagebox.
My question is::
When it is asking when the submit button is pressed, create a person or an employee based on the above checkbox.Would I use what is above the checkbox or below.
Also to put all of the form data into the class object using either the constructors or properties. I'm Not to sure how to do this.
Display all of the class information using a ToString and a messagebox. I understand how to do a messagebox but not with a ToString.
Now I already have two classes and those names are Members and Employees. Under the Members I have Name, Age, and COB. Under the employees I have Salary and JobTitle. The only time that the salary and jobtitle comes up if the user check the checkbox that says is person employee.
I am sorry if I confuse people I myself is kinda confused with what is being asked. The software I am using is Microsoft Visual C# 2010 Expressed.
The code I have so far don't know if it is right or not:
private void button1_Click(object sender, EventArgs e)
{
Members obj = new Members(); <---This is what is I am assuming being asked when
obj.Name = ""; its says create either a person or an employee
obj.Age = ""; based on the above checkbox.
obj.COB = "";
Employess obj1 = new Employess(); <-- here I am trying to put all of the form
obj1.Salary = ""; data into the class object using either
obj1.JobTitle = ""; the constructor or properties.
Console.WriteLine(obj.ToString());<--- this is the messagebox I am being asked to do its not all the way done.
}
From what I get from your code is you have taken two classes for employees and members and you want to store their information in objects of respective classes based upon your checkbox selection. I suppose you are working in windows forms because you have specified the button_click event.
If that's the case:
private void button1_Click(object sender, EventArgs e)
{
if(checkbox1_Member.Checked==true)
{
Members obj = new Members();
obj.Name = "";
obj.Age = "";
obj.COB = "";
MessageBox.Show(obj.Name+ " :: " +"obj.Age.ToString());
}
else if(checkbox2_Employee.Checked==true)
{
Employees obj1 = new Employees();
obj1.Salary = "";
obj1.JobTitle = "";
MessageBox.Show (obj1.Salary.ToString()+ " ::"+obj.JobTitle.ToString());
}
}
Your question is quite vague but let's make a ton of assumptions:
You have a checkbox you've renamed to 'checkEmployess'. (I'm not sure if you meant 'employee' but let's just go with it.)
You have text boxes on your form for all of the stuff the user has entered with sensible names.
All the input is in text at the moment.
So, you need to check whether the checkbox is ticked/checked with an if statement and create the correct sort of object:
private void button1_Click(object sender, EventArgs e)
{
object dude; // if you use inheritance then this could be of the base class's type
if (this.checkEmployess.Checked)
{
// it's an employess
Employess employee = new Employess();
employess.Salary = textSalary.Text; // this copies the value of the control into your object
employess.JobTitle = textJobTitle.Text; // however for this example we've assumed every control is a text control and your object has only string properties
dude = employess;
}
else
{
// it's a member
Members member = new Members();
member.Name = textName.Text;
member.Age = textAge.Text; // this in particular should be made numeric
member.COB = textCOB.Text;
dude = member;
}
MessageBox.Show(dude);
}
That's the basic object creation done. You could add a ToString method to these two classes to format their properties for display. (This approach is a bit crude but will work so stick with this for now.)
Improvements you can then make are:
Use input controls that are more suitable for the type of input. E.g. numeric controls for numbers, &c.
Use object initializers for constructing these objects.
Use methods to correctly format these two types of objects for display).

WinForms Dynamic Label

I am creating dynamic labels and letting users change attributes of the labes like backcolor and so by sending unicode. However I don't know how to check if the label exists therefore I can't manipulate the dynamicly created label. below is my code:
if ((InputBox.Text.StartsWith("π")) && (InputBox.Text.EndsWith("}")))// only process if the message starts with π and ends with }
{
string Message = InputBox.Text;
InputBox.Text = "";// Clear the box when done.
// Butt1 message line
if (Message.StartsWith("πlabelt1"))
{
if (Message.StartsWith("πlabelt1_BackColor"))
{
Message = Message.Substring(19);
//labelt1.BackColor = System.Drawing.Color.FromName(Message.Replace("}", ""));
}
}
private void ImageBox_DragDrop(object sender, DragEventArgs e)
{
//Graphics g = ImageBox.CreateGraphics();
//g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap),
//new Point(e.X - this.Left, e.Y - this.Top - 150));
Point p2 = PointToClient(Cursor.Position);
Label buttlbl_ = new Label();
labelCount++;
buttlbl_.Name = "labelt" + labelCount.ToString();
buttlbl_.Location = new Point(p2.X, p2.Y);
buttlbl_.Size = new System.Drawing.Size(37, 37);
buttlbl_.BackColor = System.Drawing.Color.DarkGray;
this.Controls.Add(buttlbl_);
buttlbl_.BringToFront();
ImageBox.Invalidate();
}
}
Any suggestions?
you may set buttlbl_ as a class member so you can check if it is created
before creation you can find it in this.Controls collection (by id)
I think you've approached this problem incorrectly. You are apparently trying to offer the user an opportunity to edit these textboxes using a language-based interface. You either need to build a full parser to help you here or to look at an alternative paradigm, perhaps following the same approach that VS uses to allow you to create and edit labels via a GUI-type interface. That way you can maintain a tighter control over the actions that can be completed without the complexity of 'natural' language parsing.

Why isnt Properties.Settings.Default being saved?

I wrote this to quickly test
Why arent my settings being saved? The first time i run this i have 3(old)/3(current) elements. The second time i get 3(old)/5(current), third time 5(old)/5(current).
When i close the app the settings completely disappear. Its 3 again when i run it. I made no changes to the app. Why arent my settings being saved
private void button2_Click(object sender, EventArgs e)
{
MyApp.Properties.Settings.Default.Reload();
var saveDataold = MyApp.Properties.Settings.Default.Context;
var saveData = MyApp.Properties.Settings.Default.Context;
saveData["user"] = textBox1.Text;
saveData["pass"] = textBox2.Text;
MyApp.Properties.Settings.Default.Save();
}
You should be using the exposed properties instead of putting your data in the context:
var saveData = MyApp.Properties.Settings.Default;
saveData.user = textBox1.Text;
saveData.pass = textBox2.Text;
The context
provides contextual information that
the provider can use when persisting
settings
and is in my understanding not used to store the actual setting values.
Update: if you don't want to use the Settings editor in Visual Studio to generate the strong-typed properties you can code it yourself. The code generated by VS have a structure like this:
[UserScopedSetting]
[DebuggerNonUserCode]
[DefaultSettingValue("")]
public string SettingName
{
get { return ((string)(this["SettingName"])); }
set { this["SettingName"] = value; }
}
You can easily add more properties by editing the Settings.Designer.cs file.
If you don't want to use the strong-typed properties you can use the this[name] indexer directly. Then your example will look like this:
var saveData = MyApp.Properties.Settings.Default;
saveData["user"] = textBox1.Text;
saveData["pass"] = textBox2.Text;

Categories