|
I got the solution.
rtbox2.SelectionStart = rtbox2.rtf.length; ( or rtbox2.text.length )
rtbox2.SelectedRtf = rtbox1.Rtf;
But now, i got another problem.
In the rtbox2 (a rich text box) as the strings are added to it.
The vertical scroll bar is moving up ( means always firstline is shown ).
Can i see the latest contents in the rtbox2, as and when the content is added to it, without manually moving the scrollbar.
I tried with rtbox2.scrolltocaret() ( even after selecting the last character ) . But it is not working.
Can any one solve the problem
M.V.Ravikumar.
|
|
|
|
|
First, you have to put the caret at the end of the text. Then you have to make sure that the RichTextBox has the focus, otherwise, ScrollToCaret will not work.
rtb2.SelectionLength = 0
rtb2.SelectionStart = rtb2.Text.Length()
rtb2.Focus()
rtb2.ScrollToCaret()
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
how to get sqlserver names in local net,thank you
|
|
|
|
|
This will do it
private const short SQL_HANDLE_ENV = 1;
private const short SQL_HANDLE_DBC = 2;
private const int SQL_ATTR_ODBC_VERSION = 200;
private const int SQL_OV_ODBC3 = 3;
private const short SQL_SUCCESS = 0;
private const short SQL_NEED_DATA = 99;
private const short DEFAULT_RESULT_SIZE = 1024;
private const string SQL_DRIVER_STR = "DRIVER=SQL SERVER";
[DllImport("odbc32.dll")]
private static extern short SQLAllocHandle(short hType, IntPtr inputHandle, out IntPtr outputHandle);
[DllImport("odbc32.dll")]
private static extern short SQLSetEnvAttr(IntPtr henv, int attribute, IntPtr valuePtr, int strLength);
[DllImport("odbc32.dll")]
private static extern short SQLFreeHandle(short hType, IntPtr handle);
[DllImport("odbc32.dll",CharSet=CharSet.Ansi)]
private static extern short SQLBrowseConnect(IntPtr hconn, StringBuilder inString,
short inStringLength, StringBuilder outString, short outStringLength,
out short outLengthNeeded);
static public string[] GetSQLServers()
{
string[] retval = null;
string txt = string.Empty;
IntPtr henv = IntPtr.Zero;
IntPtr hconn = IntPtr.Zero;
StringBuilder inString = new StringBuilder(SQL_DRIVER_STR);
StringBuilder outString = new StringBuilder(DEFAULT_RESULT_SIZE);
short inStringLength = (short) inString.Length;
short lenNeeded = 0;
try
{
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_ENV, henv, out henv))
{
if (SQL_SUCCESS == SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(IntPtr)SQL_OV_ODBC3,0))
{
if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_DBC, henv, out hconn))
{
if (SQL_NEED_DATA == SQLBrowseConnect(hconn, inString, inStringLength, outString,
DEFAULT_RESULT_SIZE, out lenNeeded))
{
if (DEFAULT_RESULT_SIZE < lenNeeded)
{
outString.Capacity = lenNeeded;
if (SQL_NEED_DATA != SQLBrowseConnect(hconn, inString, inStringLength, outString,
lenNeeded,out lenNeeded))
{
throw new ApplicationException("Unabled to aquire SQL Servers from ODBC driver.");
}
}
txt = outString.ToString();
int start = txt.IndexOf("{") + 1;
int len = txt.IndexOf("}") - start;
if ((start > 0) && (len > 0))
{
txt = txt.Substring(start,len);
}
else
{
txt = string.Empty;
}
}
}
}
}
}
catch (Exception)
{
txt = string.Empty;
}
finally
{
if (hconn != IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_DBC,hconn);
}
if (henv != IntPtr.Zero)
{
SQLFreeHandle(SQL_HANDLE_ENV,hconn);
}
}
if (txt.Length > 0)
{
retval = txt.Split(",".ToCharArray());
}
return retval;
}
|
|
|
|
|
The simplest way is to use SQL-DMO. Right-click on your project and select Add Reference. Click the COM tab and find "Microsoft SQLDMO ..." in the list. Double-click to add it.
Your code is then as easy as this:
SQLDMO.Application app = new SQLDMO.ApplicationClass();
SQLDMO.NameList servers = app.ListAvailableSQLServers();
if (servers != null)
foreach (string server in servers)
Console.WriteLine(server); You can also P/Invoke dbserverenum , though this is rumored to be unsupported in future versions (it's from an old C library). If you use SQLDMO, you'll need to redistribute the interop assembly that VS.NET will create when you add the COM reference to the SQL-DMO library. SQLDMO must be installed, however, on the target machine (this is done by SQL Server and the MSDE).
If you don't want to rely on such client requirements, you'll have to google the net to see if you can find the protocol used to detect SQL Servers on the network.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heyas all,
I'm comming from a unix/c background here and trying to learn .NET. I'm attempting to make a simple cd-database program. What would be the best way to implement the data storage portion of the program (ie. sql database, text file, access database, etc.) The user would have complete control over the datbase create new ones ect.
Thanks all!
kha0s
"There are 10 types of people in this world; Those that know binary and those that do not."
|
|
|
|
|
What do you mean by "cd-database program"? You are going to run it from CD, or you're going to install it from the CD?
If you are installing to the HD you could look at MSDE which is the Desktop Edition of SQL Server and is redistributable.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
My guess is he probably means a cd-collection-catalog. Am I right?
|
|
|
|
|
Hi!
Sorry for the confusion. What I am writing is a program that simply catologs a persons cd's. they enter the information in manually like name, author, etc etc THe user can search using the same fields for a particular cd entered in the catalog. they can add or remove entries as well.
kha0s
"There are 10 types of people in this world; Those that know binary and those that do not."
|
|
|
|
|
I would go with using MSDE as your database engine as you can redistribute it. You can access it via the classes in the System.Data.SqlClient namespace.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
Heya,
I've decided to hgo with the MSDE route. However, I've installed the package MSDE2000A.exe. The package extract fine and go to MSDE\ and run setup SAPWD="whatever". THe process compeltes successfully however after rebooting, there is no MSSQLSERVER service listed under services on my machine. I'm running XP with all available updates. Anyone have a clue as to what's going on. Sorry for such a newb question but this is really frustrating me.
Also, I have SQL Server 2000 available to me as well. Would I get the same functionality from that program? Should I use that if I have it instead?
Thanks All.
kha0s
"There are 10 types of people in this world; Those that know binary and those that do not."
|
|
|
|
|
Using DataSet would be an easy solution because you can create those on the fly, from XML or from any database and save changes to those sources.
You can have all the data pre-loaded and have parent/child relations. You can search, sort, filter without depending on the database. Can save to local storage, access database, etc.
In other words, most flexible solution.
|
|
|
|
|
and I have also written something to what you want to write:
http://www.adersoftware.net/?page=oyster
You can download it for free, and if you want to look at the source code, let me know and I can send it to you.
|
|
|
|
|
Hi all,
i would like to set up a logic like this:
A Collection class School which holds different class-members like:
* student
* teacher
* table
* chair
* classroom
* stairs
Member classes have different values (and methods), like:
- student has: name, age and classroom <small>(link to classroom id?)</small>
- teacher has: name
- table has: pointF position, color and tabletype <small>(an enum)</small>- classroom has: pointF[] polyline, ID, classroomtype and MaxPersons
- stairs has: pointF startpoint and pointF startpoint
I would like to get information for all elements in the main class collection using the foreach statement.
Then using it's type i can act on it. For a classroom i could show the position PointF[] on an image and for the students i could show only the students which are in that particular classroom.
Pseudo code:
Foreach element in MainSchoolCollection
If element.type is typeClassroom
PointF[] thePoints = element.vertices[]
element.DrawMyClassroom(thePoints)
int ClassID = element.ID
Foreach element in MainSchoolCollection
If element.type is typeStudent
MessageBox.Show(element.name)
EndIf
EndForeach
EndIf
If element.type is typeStairs
element.StairsToLine(element.startpoint, element.endpoint)
EndIf
EndForeach
So the main issue is that each element in the collectionclass has different properties and methods. When looping through the main collection i would like to use/modify the element's property based on it's type.
Can anyone point me in the right direction here?
Thanks,
Me.
|
|
|
|
|
first, you have to define element and give it a common description, i.e., something that goes for all items. Then, you can implement the IEnumerable interface in all items, so you can use an enumerator.
You might even use an emty interface, which can be used as element in the foreach loop, and then cast the element to the containing type, just as you did.
|
|
|
|
|
Hoi Rakker,
klink Nederlands,... )
Do you happen to know if this subject is explained in a tutorial somewhere,... i'm relative new to interfaces.
Thanks.
|
|
|
|
|
This is one thing that object-oriented programming solves. Define an abstract base class with all the properties and methods that are shared by every derivative class. Some of these properties may even be abstract themselves, declaring that derivative class must override them (perhaps to provide a friendly name of the object, rather than just returning the class name).
A simple hierarchy of classes I see from your list is like so:
abstract SchoolObject
+abstract Person
|+Student
|+Teacher
+abstract Item
|+Table
|+Chair
|+Classroom
|+Staircase So the abstract SchoolObject would define the properties you need for everything. Your SchoolCollection accepts types of SchoolObject s, which can be an instance of any derivative class. As you're enumerating your collection, you can access the shared properties of SchoolObject , which - if virtual - the derivative classes can override to return different results.
This is really a core concept of object-oriented programming. If you're new to object-oriented programming or programming in general, I recommend you pick up a good book or two on object-oriented programming with .NET, such as Learn to Program with C#[^] (top-rating from a quick Amazon.com search). Object-oriented programming is also taught in most college curriculums, and can be picked pretty easily by reading through the documentation and looking at examples (CodeProject has thousands of examples).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath,
thanks for the reply and info.
Comming from a VB background and using collection classes there i thought things would be similar, but i could not get the properties of the elements in my collection.
In VB i used like:
collection class:
Private mCol As Collection
Public Function AddClassRoom(ByVal Name As String, ByVal MyCenter As Coord) As ClassRoom
Public Function AddStudent(ByVal FirstName As String, ByVal Lastname As String) As Student
Both Student and ClassRoom are seperate classes with different properties and methods.
Problem is that there seems no simple translating to the Private mCol As Collection part.
Reading to do for me,...
Thanks again.
|
|
|
|
|
If you really want arbitrary objects to be in your collection you could simply use the base collection classes from System.Collections.
They usually have object derived items, so you can stuff in what you like.
When you're enumerating these collections you can determine the type of each item by something similar to (in C#):
ArrayList myCollection = new ArrayList();
[...].
foreach (object o in myCollection)
{
if (o is Teacher)
{
Teacher t = o as Teacher;
t.DoSomethingOnlyTeachersDo();
}
if (o is Chair)
{
Chair c = o as Chair;
c.StandThere();
}
}
mav
|
|
|
|
|
Hi Mav,
Thanks, that did the trick !
I have the setup correct in general:
collection class with elements called obj which is an interface and all objects i store in the collection 'derive' from obj.
So, using class objects like:
public class objTeacher : obj
public class objStudent : obj
My problem (major error ) was that i did not 'copy' the properties of a 'general' object to the object i need like your example:
if (o is Teacher)
{
Teacher t = o as Teacher;
string test = t.TeacherName;
t.DoSomethingOnlyTeachersDo();
}
in my case i could not access the propertie TeacherName because it is an object/class specific property.
Casting it as a Teacher object did the trick !
How live can be sweet and simple sometimes
So to understand the logic here, the Teacher is a derivate of the obj interface.
Is this the propper structure to enumerate and use different objects in 1 collection, or am i on a dangerous path here?
Me.
|
|
|
|
|
I think it's the easiest way to go.
In fact you wouldn't even have to derive from obj , because all classes in .NET implicitely inherit object , which is the item type of most base collections.
Using an obj interface might be the cleaner way if you want to ensure type safety at compile time. But then you have to make the collections typesafe (for example by deriving from CollectionBase and overriding the required methods). And find a meaningful interface.
I usually doubt if an empty marker-interface/base class (I think somebody suggested this) is a good idea from an object oriented view. By defining an interface you extract all common properties and behaviour into the interface, but if you can't find anything the different classes implementing your interface have in common then why should you create an interface?
One caveat remains, though. Using object derived items will allow you to add anything to your collection at compile time, even if this might not be desireable. You will not be able to check this until run time.
Regards,
Mav
|
|
|
|
|
I now removed the interface and use object to enumerate,... works perfectly.
Thanks !
|
|
|
|
|
Despite what Microsoft marketing tries to tell you, VB6 is nothing like VB.NET (which is just a language which targets the CLR and uses a common class library). Collections are objects all by their lonesome and you can extend CollectionBase if you want to avoid a lot of the mess but still provide typed parameters (you could override Add , for example, and provide a parameter type as the base class).
You'll still want to go with an abstract object model as everyone here so far has told you. This is something VB couldn't do and is very powerful and leads to clearner designs than prototyping functions like you did in your snippet (having one method to add the base class defined on the collection itself).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks Heath and Mav,
yes VB is a whole different cookie to eat then C#,...noticed )
Will do some reading/investigating regarding the abstract model or go for the mav-version [ArrayList-logic].
Base, Abstract, Virtual, Interface and Derivative classes.
A class collection with a collection of 1 class i got covered.
Mostly i work of examples / snippets but did not find anything regarding various calsses in one collection.
So, welcoming myself to the new world )
|
|
|
|
|
Mav's way, IIRC, uses interfaces. Interfaces and abstract classes differ mostly in the fact that abstract classes can at least contain some functionality. Take the top-most class from which all classes derive: System.Object . It's ToString method is available on any class with default behavior to return the namespace-qualified type name, but classes can override it to return something else. With an interface, each implementing class would have to override it; they don't have a choice. Also, a class can implement multiple interfaces, but can only extend one class.
pxp wrote:
yes VB is a whole different cookie to eat then C#,...noticed
Actually, even VB.NET is different. The syntax is mostly the same, yes, but the output is very different. VB.NET, C#, MC++, and any other managed languages can use assemblies written in any other language because the language compiler for each language compiles to Intermediate Language (IL). Some languages may not support all the features of the Common Language Infrastructure (CLI) and, in many cases, would only be able to use CLS-compliant (Common Language System) types. JScript.NET is a good example.
pxp wrote:
Mostly i work of examples / snippets but did not find anything regarding various calsses in one collection.
Don't think of it as multiple classes but as one class - the abstract class. Sure, in actually the collection contains multiple instances but since each one of those classes derives from the base class (and your collection's methods and properties use that base class).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|