|
You are getting the cross thread application basically because of the nature of windows controls. Windows form controls are not Thread safe meaning if you instantiate a contol in a thread (main thread) and try to access the data from other thread it won't allow you to do that way.
Now in order to access the other control you have to use Invoke method for the control.
Have a look at the following articles.
http://www.codeproject.com/csharp/threadsafeforms.asp
http://msdn2.microsoft.com/en-us/library/ms171728.aspx
|
|
|
|
|
hi everyi,
i don know how to explain this with words...but below is the result that i wan to get.
for example:
display = 4 rows and coloums
display in #
Result:
####
####
####
####
hope u understand..
|
|
|
|
|
This is like a school project.
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
Console.Write("#");
}
Console.WriteLine();
}
|
|
|
|
|
This is in C# yeah? Is this a console app?
Don't completely understand what you want, the first thing that comes to mind is to create an array, fill it up with #'s (or you can code it so they enter in the output) and iterate that array on the page.
Hope this gives you some idea to the desired solution!
|
|
|
|
|
Well, the most straightforward solution would be:
Console.Write("####\r\n####\r\n####\r\n####\r\n");
However, that is probably not the solution that you need to complete the assignment. Why don't you just paste the assignment and explain what you are having problems with?
---
single minded; short sighted; long gone;
|
|
|
|
|
Hi Everyone,
I've got a ASP.NET C# website but I have an issues with the standard printing functionality that Internet Explorer offers you. If the person doesn't choose to print the webpage with the setting "To Fit" but leaves it set to 100% then the printer ends up chopping off some of the words at the end of some lines. I've decided to make my own printing page, (which is no problem) but the problem I do have is that I don't want to make a seperate printing page for all pages on my website.
In whole, does anyone know a way of constructing one page that can possibly handle all the printing of the website. In other words when people press my custom print button on the page, it redirects them to my one page.
I have the feeling I'm really bad at explaining this, and I have the feeling it's impossible. Hope there is something though!
Cheers,
Michael
|
|
|
|
|
You could perhaps create a master page that defines the printing format ? If you can do that, then you can change the master page programatically, I think. Make all your pages derive from a new base class, which is able to change the master page to the one that formats the page for printing.
This is all off the top of my head, it may not work....
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Na Na Na, thanks for the suggestion. It certainly makes sense to me and I'll look into it. I think this will be tricky either way but I'll give it a shot and if you want let you know the solution when I get there!
Thanks for your help!
Michael
|
|
|
|
|
Hi,
Im using the following methods to Serialize/Deserialize objects and it works perfectly:
-----------------------------------------------------------------------------------------
public static byte[] SerializeObject(object instance)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(); // Stream
bf.Serialize(ms, instance); // "Save" object state
ms.Position = 0;
byte[] serializedData; serializedData = new byte[ms.Length];
ms.Read(serializedData, 0, serializedData.Length);
ms.Dispose(); // Cleanup the MemoryStream resource
return serializedData;
}
public static object DeSerializeObject(byte[] msg)
{
MemoryStream ms = new MemoryStream(msg);
BinaryFormatter bf = new BinaryFormatter();
ms.Position = 0;
object tmpObj = bf.Deserialize(ms);
ms.Dispose(); // Cleanup the MemoryStream resource
return tmpObj;
}
-----------------------------------------------------------------------------------------
...but when I try to Deserialize the following object I get an exception. However, If I remove the XML tags from the file it works. How can I do this without removing the XML tags?
public class PlayerDeck
{
[XmlArray("PlayerDeck")]
[XmlArrayItem("Card", typeof(BoardCard))]
public List<boardcard> MyBoardCards
{
get { return m_BoardCards; }
set { m_BoardCards = value; }
}
}
|
|
|
|
|
Look into XML serialization.
|
|
|
|
|
I'm wondering if the array is containing xml data?
An alternative to what you are doing is to go whole hog with xml attributes and use the xmlserializer to Serialize and Deserialize. It would also use much less code.
public class MyClass
{
variables assumed to save typing time
[XmlElement]
public string CurrentPlayer{get;set;}
[XmlAttribute]
public int NumberOfPlaysMade{get;set;}
[XmlAttribute]
public int CurrentTimeSpent{get;set;}
[XmlArray]
public Board PlayingBoard{get;set;}
}
public class Game
{
using ( FileStream stream = File.Open(path))
{
XmlSerializer ser = new XmlSerializer(typeof(MyClass));
ser.Serialize(stream, currentGame);
}
}
|
|
|
|
|
Hello,
Im trying to workout how to dynamically add tab pages with RichTextBox inside them. So far:
1. I have a form with a input field to enter how many tab pages to create
2. A button that calls the method to create tab page when clicked
3. a tabcontrol with no pages on it
Here is a demo - http://img525.imageshack.us/img525/4519/untitledxv6.jpg[^]
This is the code i have on form1.cs
static int maxTabs = 20;<br />
TabPage[] tabPages = new TabPage[maxTabs];<br />
SyntaxHighlightingTextBox[] shtb;<br />
private void AddTab(int tabNumber)<br />
{<br />
for (int i = 0; i < tabNumber; i++)<br />
{<br />
tabPages[tabNumber] = new TabPage();<br />
tabPages[tabNumber].Location = new System.Drawing.Point(4, 22);<br />
tabPages[tabNumber].Name = "tabPage" + tabNumber.ToString();<br />
tabPages[tabNumber].Padding = new System.Windows.Forms.Padding(3);<br />
tabPages[tabNumber].Size = new System.Drawing.Size(403, 386);<br />
tabPages[tabNumber].TabIndex = tabNumber;<br />
tabPages[tabNumber].Text = "tabPage " + tabNumber.ToString();<br />
tabPages[tabNumber].UseVisualStyleBackColor = true;<br />
<br />
shtb[tabNumber] = new SyntaxHighlightingTextBox();<br />
shtb[tabNumber].Name = "shtb" + tabNumber.ToString();<br />
shtb[tabNumber].Location = new Point(0, 0);<br />
shtb[tabNumber].Dock = DockStyle.Fill;<br />
<br />
tabPages[tabNumber].Controls.Add(shtb[tabNumber]);<br />
tabControl.Controls.Add(tabPages[tabNumber]);<br />
<br />
tabPages[tabNumber].SuspendLayout();<br />
tabPages[tabNumber].ResumeLayout(false);<br />
int counter = tabControl.Controls.Count;<br />
tabControl.SelectedIndex = counter - 1;<br />
this.Text = "TabText1 - " + tabPages[tabNumber].Text;<br />
}<br />
}<br />
<br />
private void AddButton_Click(object sender, EventArgs e)<br />
{<br />
try<br />
{<br />
AddTab(int.Parse(tabNumberTextBox.Text));<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show("Following Error Occured\r\n\r\n" + ex.Message);<br />
}<br />
}
When i build this solution, i get no error at all, but when i enter 1 into the input field and click the add button, i get the following exception error message:
object reference not set to an instance of an object
What does this mean? I've never seen this error before, so i not sure how to fix this. Can someone point me in the right direction on how to fix this error...? It builds correctly, so i assumed i wrote the program correctly.
|
|
|
|
|
I suspect this: SyntaxHighlightingTextBox[] shtb; it's not initialized.
I also wonder why you have arrays of TabPage s and SyntaxHighlightingTextBox es.
|
|
|
|
|
If its not getting initialized,
should i move the code inside:
public Form1()
{
InitializeComponent();
}
I have an array of tabpages and shtbs because, i want to be able to many many instances of tabs with richtextboxes inside them, dynamically on demand.
|
|
|
|
|
Note the difference between these two statements:
TabPage[] tabPages <big>= new TabPage[maxTabs];</big><br />
SyntaxHighlightingTextBox[] shtb;
|
|
|
|
|
I saw the difference in the code, however, when i changed the code like this (before when i was doing some trial and error):
TabPage[] tabPages = new TabPage[maxTabs];<br />
SyntaxHighlightingTextBox[] shtb = new SyntaxHighlightingTextBox();
I started to get this error message:
Cannot implicitly convert type 'SynHighLib.SyntaxHighlightingTextBox' to 'SynHighLib.SyntaxHighlightingTextBox[]'
|
|
|
|
|
As you should, try
SyntaxHighlightingTextBox[] shtb = new SyntaxHighlightingTextBox<big>[somevalue]</big>;
|
|
|
|
|
OMG... that worked O_O;
Thank you very much
|
|
|
|
|
Of course it does. Now donate a pint of blood to the Red Cross.
|
|
|
|
|
I recommend if you're coding blind and trying things to see if they work, you buy a C# book and work through it.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Latheesan wrote: I have an array of tabpages and shtbs because, i want to be able to many many instances of tabs with richtextboxes inside them, dynamically on demand.
You don't need arrays for that, the TabControl and TabPage have Collections for them.
|
|
|
|
|
Oh? i didnt know that,
Is there any example on how to use this? I haven't seen any good tutorials on tab pages anywhere yet. I did do a bit research tho.
|
|
|
|
|
Here's my version: (I used a RichTextBox because I don't know what that other thing is.)
private void AddTabs ( int tabNumber )
{
System.Windows.Forms.TabPage tabPage = null ;
System.Windows.Forms.RichTextBox shtb;
tabControl.SuspendLayout() ;
tabControl.Controls.Clear() ;
for (int i = 0; i < tabNumber; i++)
{
tabPage = new System.Windows.Forms.TabPage();
tabPage.Location = new System.Drawing.Point(4, 22);
tabPage.Name = "tabPage" + i.ToString();
tabPage.Padding = new System.Windows.Forms.Padding(3);
tabPage.Size = new System.Drawing.Size(403, 386);
tabPage.TabIndex = i;
tabPage.Text = "tabPage " + i.ToString();
tabPage.UseVisualStyleBackColor = true;
shtb = new System.Windows.Forms.RichTextBox();
shtb.Name = "shtb" + i.ToString();
shtb.Location = new System.Drawing.Point(0, 0);
shtb.Dock = System.Windows.Forms.DockStyle.Fill;
tabPage.Controls.Add(shtb);
tabControl.Controls.Add(tabPage);
tabPage.SuspendLayout();
tabPage.ResumeLayout(false);
}
this.Text = "TabText1 - " + tabPage.Text;
tabControl.SelectedIndex = tabControl.Controls.Count - 1;
tabControl.ResumeLayout() ;
}
|
|
|
|
|
I tried entering 0 into the input box and try to add a tab, this time no exception error, but instead, nothing happens.
Where im i going wrong? Why wont this work?
|
|
|
|
|
Okay, i made a silly mistake in the for loop, but here's the fixed version of the for loop code:
for (int i = 0; i < tabNumber; i++)<br />
{<br />
tabPages[i] = new TabPage();<br />
tabPages[i].Location = new System.Drawing.Point(4, 22);<br />
tabPages[i].Name = "tabPage" + i.ToString();<br />
tabPages[i].Padding = new System.Windows.Forms.Padding(3);<br />
tabPages[i].Size = new System.Drawing.Size(403, 386);<br />
tabPages[i].TabIndex = i;<br />
tabPages[i].Text = "tabPage " + i.ToString();<br />
tabPages[i].UseVisualStyleBackColor = true;<br />
<br />
shtb[i] = new SyntaxHighlightingTextBox();<br />
shtb[i].Name = "shtb" + i.ToString();<br />
shtb[i].Location = new Point(0, 0);<br />
shtb[i].Dock = DockStyle.Fill;<br />
<br />
tabPages[i].Controls.Add(shtb[i]);<br />
tabControl.Controls.Add(tabPages[i]);<br />
<br />
tabPages[i].SuspendLayout();<br />
tabPages[i].ResumeLayout(false);<br />
int counter = tabControl.Controls.Count;<br />
tabControl.SelectedIndex = counter - 1;<br />
this.Text = "TabText1 - " + tabPages[i].Text;<br />
}
This compiles correctly, yet, when i run it, i get the exception error again...
|
|
|
|